Liz Lamperouge
Liz Lamperouge

Reputation: 681

Get color of global variables with netlogo

I am trying to get the real color of my global variable.

Here my code:

breed [players player]

globals [
INITIAL-POSSESSION ;
]

to setup
  clear-all
  reset-ticks
  set-initial-possession
end

to go 
  ticks
  ask players [
  decision
  ]
end


to-report initial-possession
  report random 2
end

to set-initial-possession
  ifelse initial-possession = 1
  [ 
    set INITIAL-POSSESSION black]
  [ 
    set INITIAL-POSSESSION white]
end


to decision
 if ([color] of INITIAL-POSSESSION) = black
 [] ;;do something
if ([color] of INITIAL-POSSESSION) = white
[];;do something
end

But I get this error:

OF expected input to be a turtle agentset or link agentset or turtle or link but got the number 0 instead.

So I change it with (and it works):

to decision
 if INITIAL-POSSESSION = 0
  []
 if INITIAL-POSSESSION = 9.9
  []
end

But there is any other way to do that? (I am using netlogo 6.0)

Upvotes: 0

Views: 202

Answers (1)

Luke C
Luke C

Reputation: 10291

I think there may be some code missing so I can't confirm, but it looks like you may not have set BALL-OWNER up as a turtle or patch, and instead assigned a value directly to that variable. of queries a variable from an agent (or a list of variables from an agentset), so if BALL-OWNER is set to a value, NetLogo gets confused. If you do assign an agent to BALL-OWNER, however, your approach should work fine. For example, try running the code below:

to setup
  ca
  crt 10 [ 
    setxy random-xcor random-ycor
    set color one-of [ red blue ] 
  ]
  reset-ticks
end

to go
  let ball-owner one-of turtles

  ifelse [color] of ball-owner = red [
    print "red team has possession"
  ] [
    print "blue team has possession"
  ]
end

Edit: You can definitely use a global to pick a color just as you did in your second code block- I just wanted to point out that of is specifically tied to agents. If you want to store a color in a global variable for comparison, that's possible, it's just that your comparison is simpler than using of:

globals [ initial-possession ]

to setup
  ca
  crt 3
  set-initial-possession
  reset-ticks
end

to go 
  ask turtles [
    decision
  ]
end

to set-initial-possession
  set initial-possession ifelse-value ( random 2 = 1 ) [black] [white]
end


to decision
  ifelse initial-possession = black [
    print "I see that black has possession"
  ] [
    print "I see that white has possession"
  ]
end

I'm not sure if that helps, it may depend on your purpose of storing a color in the global in the first place!

Upvotes: 2

Related Questions