global-variablesnetlogo

How to fix NetLogo error “There is already a global variable called [ ]"


I keep getting the error There is already a global variable called “DOTTED-LINE-VISIBLE” but when I change the name it gives me the same error but with the new name. How to fix it? The global variable is related to a switch named dotted-line-visible in the interface.

Here is the code I have so far:

; Define a global variable to control the visibility of the dotted line 
globals [dotted-line-visible]
; Drawing the yellow dotted line if the switch is on (part of setup function)
if dotted-line-visible [draw-vertical-dotted-line boundary]
; Procedure to draw a vertical yellow dotted line
to draw-vertical-dotted-line [boundary]
    create-turtles 1 [
        hide-turtle
        set color yellow
        set size 0.5
        setxy boundary min-pycor
        set heading 0
        while [ycor < max-pycor] [
            pen-down
            forward 0.5
            pen-up
            forward 0.5
        ]
    ]
end

I created a switch called “dotted-line-visible” that I'd like to use to toggle on and off this dotted line in the middle of my NetLogo screen.


Solution

  • When you create a switch or other widget on the Interface, NetLogo uses that to define a global variable with the same name. Therefore, when you define the variable as a global in your code, it is a redundant definition. Thus the error you are getting.