Ecrin
Ecrin

Reputation: 246

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

Upvotes: 1

Views: 3454

Answers (3)

TurtleZero
TurtleZero

Reputation: 1064

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 ]

Upvotes: 4

Nicolas Payette
Nicolas Payette

Reputation: 14972

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 ]

Upvotes: 3

maialithar
maialithar

Reputation: 3123

try setting your color names to number values, according to this site

Upvotes: 1

Related Questions