使用netlogo获取全局变量的颜色

使用netlogo获取全局变量的颜色,netlogo,Netlogo,我正在尝试获取全局变量的真实颜色 这是我的代码: 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

我正在尝试获取全局变量的真实颜色

这是我的代码:

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
但我得到了这个错误:

预期输入为turtle代理集或链接代理集或turtle的 或链接,但改为数字0

因此,我用以下方法对其进行了更改(并且有效):


但是还有别的方法吗?(我正在使用netlogo 6.0)

我想可能缺少一些代码,因此我无法确认,但看起来您可能没有将
BALL-OWNER设置为turtle或patch,而是直接为该变量指定了一个值
of
查询来自代理的变量(或来自代理集的变量列表),因此如果
BALL-OWNER
设置为值,则NetLogo会混淆。但是,如果您确实将代理分配给
BALL-OWNER
,您的方法应该可以正常工作。例如,请尝试运行以下代码:

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
编辑:您完全可以使用
global
来选择颜色,就像您在第二个代码块中所做的那样-我只想指出,
代理有明确的联系。如果您想在
全局
变量中存储颜色以进行比较,这是可能的,只是您的比较比使用
更简单:

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

我不确定这是否有帮助,这可能首先取决于您在
全局
中存储颜色的目的

我认为可能缺少一些代码,因此我无法确认,但看起来您可能没有将
BALL-OWNER
设置为海龟或补丁,而是直接为该变量赋值
of
查询来自代理的变量(或来自代理集的变量列表),因此如果
BALL-OWNER
设置为值,则NetLogo会混淆。但是,如果您确实将代理分配给
BALL-OWNER
,您的方法应该可以正常工作。例如,请尝试运行以下代码:

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
编辑:您完全可以使用
global
来选择颜色,就像您在第二个代码块中所做的那样-我只想指出,
代理有明确的联系。如果您想在
全局
变量中存储颜色以进行比较,这是可能的,只是您的比较比使用
更简单:

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

我不确定这是否有帮助,这可能首先取决于您在
全局
中存储颜色的目的

抱歉,没有名为“BALL-OWNER”的全局变量,我想使用全局变量INITIAL-OWNER。所以用全局变量来分配颜色是不可取的,对吧?PS:你的代码很好用,非常感谢。@LizLamperouge-明白了-我已经用一些示例代码进行了编辑,也许这会澄清我的意思!抱歉,没有名为“BALL-OWNER”的全局变量,我想使用全局变量INITIAL-OWNER。所以用全局变量来分配颜色是不可取的,对吧?PS:你的代码很好用,非常感谢。@LizLamperouge-明白了-我已经用一些示例代码进行了编辑,也许这会澄清我的意思!