Search code examples
netlogosliders

Netlogo - Setting turtles energy with a slider


I know this is some simple thing but i cant seem to get it right and i cant seem to find this answer anywhere.

I have a 2 armies each have "turtles-own[energia base]" . I connected a slider with the global variable "energia" and it said the global variable already exists. So i changed it to nenergia and the notice went away. How do i make the slider value go to energia ?


Solution

  • When you make a slider, it automatically includes an associated global variable of the same name. You don't need to separately declare a variable.

    It isn't clear from your question what your intent in writing turtles-own [energia] was. If you use turtles-own, it isn't a global variable, it's a turtle variable, so each turtle has its own value for it. It's not like a slider, which has only a single value which is globally visible.

    Do you mean for the slider to be the initial energy value for all turtles, after which each turtle's value can vary as the simulation progresses? If so, then make a slider named something like initial-energy, and in the Code tab do something like:

    turtles-own [energy]
    
    to setup
      ...
      create-turtles 100 [ set energy initial-energy ]
      ...
    end
    

    You'll see this pattern used frequently throughout the NetLogo Models Library.