简体   繁体   中英

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. I have then 'created' the word 'potential0' by concatenating the work potential + who of the agent. Then I have asked all agents with potential0 to move and nothing happens.

Thanks,

Kevin

You will want to use runresult for this. runresult takes whatever string it is applied to and treats it as a reporter rather than a string. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM