Comparing Turtle Color In Netlogo - netlogo

I am trying to do something I imagine is relatively simply but for some reason I am having a heck of a time figuring it out and all my searches are turning up blank.
I want to query the color of a specific turtle and check if it matches another color. I want to do something like this:
if color targetTurtleNum = red [set target-confirmed true] ;
But I keep getting an error:
IF expected this input to be a command block, but got a true/false instead.
Any ideas?

Assuming targetTurtleNum is the "who" number of the turtle you are interested in, try:
if [ color ] of turtle targetTurtleNum = red [ set target-confirmed true ]
The error you are getting is because if expects two inputs: a boolean (the condition you want to check) and a command block (what to do if the condition is true). In your version of the code, the first input thatif is getting is color, and the second one is targetTurtleNum = red, so the compiler complains about getting a boolean as the second input.
In the correct version up here, the whole [ color ] of turtle targetTurtleNum = red part is the boolean that counts as the first input, and [ set target-confirmed true ] is the command block that is expected as the second input.

Related

How to fix netlogo 6.1.1 nothing named ? has been defined

I just started to learn Netlogo. I followed the code written by Lynne Hamill and Nigel Gilbert
when I finished, it reminded me that "nothing named ? has been defined".
foreach xs [
create-traders 1 [
set shape "house"
setxy ? 0
set color red
; give each trader some knids of produce to sell
set stock n-of n-items-stocked fruit-and-veg
]
]
Any ideas?
As Jasper notes in the comments, the ? is outdated syntax. Something like this should work - instead of an implicit ?, you iterate with an explicit variable (named this-x in the example below) and use that variable name wherever relevant.
foreach xs [ this-x -> ; name the variable here and add the arrow
create-traders 1 [
set shape "house"
setxy this-x 0 ; use that variable name
set color red
; give each trader some knids of produce to sell
set stock n-of n-items-stocked fruit-and-veg
]
]
You can think about it as equivalent to the conceptual loop "for each item in the list, call it 'this-x' and do something (everything after ->), replacing the 'this-x' in the code with whatever item is in the list at the moment".

Writing an if statement depending on an attribute of an agent to command agents of a different agentset

I am trying to make an agentset do something, if an agent(of a different agentset) has a particular shape.
Here, if the shape of a particular
ghost (say Ghost 1) is circle,
then all rabbits are supposed to move forward 1. (<-This is the
intended behavior)
where
ghosts are agentset A
rabbits are agentset B
I have tried along these lines:
ask rabbits
[
if (shape ghost 1 = "circle")
[
forward 1
]
]
For this code I get,
"Expected a closing paranthesis here."
with a highlighter on ghost.
I'm aware that this code is wrong but I can't think of how else this should be written to get the desired result.
This will (I think - can't test) get the syntax correct:
ask rabbits
[
if ([shape] of ghost 1 = "circle")
[
forward 1
]
]
but you also have an ordering error and will have every rabbit check the shape of chost 1. I think what you really want is:
if ([shape] of ghost 1 = "circle")
[ ask rabbits
[ forward 1
]
]

Get a count of turtles with a combination of values

I am trying to count the number of "buyer" type turtles, which have a certain surplus (turtle variable) greater than or equal to zero, and price (another turtle variable) greater than the current turtle's price (already grabbed in local variable myprice...although there may be a more direct way to put it in)
let countup count buyers with ([surplus >= 0] and [price > myprice])
NetLogo returns
Expected a TRUE/FALSE here, rather than a list or block.
let countup count buyers with (surplus >= 0 and price > myprice) returns
WITH expected this input to be a TRUE/FALSE block, but got a TRUE/FALSE instead
Close! You're looking for:
let countput count buyers with [ surplus >= 0 and price > myprice ]
with is a report that takes two arguments, like so
<turtleset> with <report block>
where the reporter block is a clump of code surrounded by [ ] that will result in either true or false. In general [ ] is netlogo's way of grouping together code so you can doing something special with it, such as having each agent in an agentset run it. Hope that helps!
Also, I assume you've got something like let myprice price on, say, the line above this one. You can combine those lines like so (not saying this code is the right way to do it, just wanted to show another option):
let countput count buyers with [ surplus >= 0 and price > [ price ] of myself ]
Checkout the docs for (the very poorly named) myself.

Netlogo : invalid context

My first netlogo program was working well, but now is failing since 'tick' in the 'go' method is not in a valid context.
Please see the attached code, line 99, which generates:
You can't use tick in a turtle/patch context, because it is observer only.
Code is here:
http://jpark.us/temp/CSSS.v1.nlogo
Problem solved...
I was trying to 'set number.sparrows...' down in other methods, but not within a proper patches context.
So this works:
if all? patches [ eggs.laid = true ] [
ask patches [ set number.sparrows count sparrows-here ]
whereas this does not:
if all? patches [ eggs.laid = true ] [
set number.sparrows count sparrows-here

NetLogo - How to set turtle color from array

How can I set a turtle's color from an array?
Here's my code but it doesn't work:
let colors array:from-list ["red" "yellow" "blue" "pink"]
set index random 3
let c array:item colors index
set color array:item colors index
Which leads to this error:
can't set flower variable COLOR to non-number blue error while flower 101 running SET
In NetLogo color, the names of the 14 main colors, plus black and white are defined as constants, so no quotes are required. Also, since they are constants, they are treated like literal values, so you can use them in the bracketed list notation, otherwise, you'd need to use the (list . . . ) reporter to create that list.
Also, your use of an array may be more complicated than needed.
You can write:
let colors [ red green blue yellow ]
set index random 3
let c item colors index
set color c
As an extra bonus, you can use the one-of primitive to do all the above:
set color one-of [ red green blue yellow ]
The accepted answer is the correct one, but as an aside, note that the read-from-string function will interpret a basic NetLogo color name as a color value:
observer> show read-from-string "red"
observer: 15
Also useful to know about is the base-colors built-in function that reports an array of the 14 basic NetLogo colors as numeric values, allowing you to do things such as:
ask turtles [ set color one-of base-colors ]
try setting your color names to number values, according to this site