[英]Netlogo: Can one access a turtle variable with a concatenated word?
I have edited the code to focus on the issue:我编辑了代码以关注这个问题:
globals [num_periods]
breed [observeds observe]
observeds-own [potential0 potential1 potential2 potential3 potential4 potential5 potential6 potential7 potential8 potential9
]
to setup
clear-all
reset-ticks
ask patches [set pcolor white]
create-observeds 1 [ setxy random-xcor random-ycor set shape "square" set color green
set potential0 random 0 set potential1 random 0 set potential2 random 0 set potential3 random 0 set potential4 random 0
set potential5 random 0 set potential6 random 0 set potential7 random 0 set potential8 random 0 set potential9 random 0
]
end
to go
ask observeds [
let test1 (word "potential" who) ;; this should output potential0 a variable
let test2 runreport [test1]
print test1 ;;prints potential0
ask observeds with [test2 = who][
move ]] ;; trying to move the turtle
end
to move
right random 360
fd 1
end
As there is only one agent it's who = 0. I have set all of the variables potentialx, x = [0,9], to zero.因为只有一个代理人,所以它是 who = 0。我已将所有变量 potentialx, x = [0,9] 设置为零。 I have then 'created' the word 'potential0' by concatenating the work potential + who of the agent.
然后,我通过连接代理人的工作潜力 + 谁来“创建”单词“potential0”。 Then I have asked all agents with potential0 to move and nothing happens.
然后我要求所有具有 potential0 的代理移动,但没有任何反应。
Thanks,谢谢,
Kevin凯文
You will want to use runresult
for this.您将需要为此使用
runresult
。 runresult
takes whatever string it is applied to and treats it as a reporter rather than a string. runresult
接受它所应用的任何字符串,并将其视为报告者而不是字符串。 If that string contains a number it returns a number.如果该字符串包含数字,则返回一个数字。 If it contains a variable name, it treats it as a variable.
如果它包含变量名,则将其视为变量。 So the following should solve your problem
所以以下应该可以解决你的问题
ask observeds with [runresult test1 = who][
move
]
Use let test1 runresult (word ("potential" who) to form the string potential0. Subsequent commands will then recognise this as a reporter使用 let test1 runresult(word ("potential" who) 组成字符串 potential0。随后的命令将识别此为报告者
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.