netlogo

how to define the value of a variable in an interface slider instead of agent initialization?


I got this variable called flip-rate.

globals [
  magnetization
]

turtles-own [
  opinion
  flip-rate
  temp-opinion
]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;; AGENT INITIALIZATION ;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to initialize-hvm ;; turtle procedure
  ask turtles [
    set opinion one-of n-values number-states [ ?1 -> ?1 ]
    recolor
    set flip-rate random-float 1
  ]
  set-magnetization
end

instead of having this flip-rate variable set as a random value in agent initialization, how do I set with a slider on the interface section?

I tried moving the flip-rate variable to

globals 

but when I try to create a slider with the same name, I get an error saying "There is already a global variable called FLIP-RATE".


Solution

  • The slider itself is a global variable (see here). So you don't have to define it in the code tab. Assuming you have named your slider flip-rate-slider, you can use it to assign it's value to the turtles-own variable flip-rate:

    globals [
      magnetization
    ]
    
    turtles-own [
      opinion
      flip-rate
      temp-opinion
    ]
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;;;;;;;;; AGENT INITIALIZATION ;;;;;;;;;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    to initialize-hvm ;; turtle procedure
      ask turtles [
        set opinion one-of n-values number-states [ ?1 -> ?1 ]
        recolor
        set flip-rate flip-rate-slider
      ]
      set-magnetization
    end