Persistent "Global variable already exists" error

38 views
Skip to first unread message

‎김윤비(사범대학 과학교육학과)

unread,
Jul 14, 2025, 7:13:05 AMJul 14
to netlogo-users

Dear NetLogo User Group,

I am encountering a persistent and very frustrating error when trying to run my NetLogo model, and I would greatly appreciate any insights or solutions you might have.

Problem Description: When I click the "Setup" button in my model, I consistently receive an error message: "There is already a global variable called LAND_TURBINES_COUNT". After simplifying my code, the error sometimes shifts to "There is already a global variable called WIND_VELOCITY".

My Code & Interface (Relevant parts):

My globals block in the Code tab is as follows:

globals [
  ;; Input variables (controlled by sliders)
  land_turbines_count  ;; Number of land turbines to install
  sea_turbines_count   ;; Number of sea turbines to install
  wind_velocity        ;; Wind speed (m/s)

  ;; Output variables (monitored)
  total_generation     ;; Total power generation (kWh)
  power_loss           ;; Transmission loss (kWh)
  usable_energy        ;; Usable energy (kWh)
  total_noise          ;; Total noise level (unitless)
  cost_per_kwh         ;; Cost per kWh (KRW/kWh)
]

--------------------------------

(I can provide the full code if necessary, but the globals block is exactly as above, with no duplicates or hidden characters that I can find.)

My slider settings for land_turbines_count (and other variables like wind_velocity) precisely match the global variable names, i.e., land_turbines_count (all lowercase). I ensure that I copy-paste the variable name directly from the globals block to the slider's "Global variable" field to avoid typos or case sensitivity issues.

What I have already tried (and failed to resolve the issue):

  1. Code Review: I have meticulously checked my entire code for duplicate globals blocks or any other declaration of land_turbines_count (or wind_velocity) with different capitalization (LAND_TURBINES_COUNT, Wind_Velocity, etc.). I have found none.

  2. NetLogo Restart: I have completely closed NetLogo (including verifying no lingering processes in Task Manager/Activity Monitor) and restarted it multiple times.

  3. New Model Creation: I have created a brand new, empty NetLogo model, copied my entire code into it, saved it with a new filename, and then recreated all sliders and buttons from scratch. This also resulted in the same error.

  4. Plain Text Editor: I copied my code into a plain text editor (like Notepad), then copied it back into a new NetLogo model to eliminate any invisible unicode characters or formatting issues. The error persists.

  5. NetLogo Reinstallation: I have completely uninstalled NetLogo 6.4.0 (including deleting its installation folder and clearing any related user data/cache I could find) and then reinstalled it.

  6. Test Code: After reinstallation, I ran a very simple test code with a single global variable and a slider (globals [test-var] to setup [...] to go [...]) and it worked perfectly, suggesting the NetLogo installation itself might be okay.

My System Information:

  • Operating System: Windows 11 Home, 64 bit

  • NetLogo Version: 6.4.0 (Initially, and I've tried reinstalling it)

This issue is particularly puzzling because I have successfully created NetLogo simulations with sliders before (e.g., a simple "fish tank" model with num_turtles slider, as shown in previous screenshots, although init_day was a monitor not a slider there).

Any guidance or suggestions on what else could be causing this specific "global variable already exists" error when the code clearly shows only one declaration would be immensely helpful. Could there be an issue with deeper system configurations, or perhaps a known rare bug with this version?

Thank you for your time and assistance.

Sincerely, Yoonbi from Korea.

Aaron Andre Brandes

unread,
Jul 14, 2025, 8:09:54 AMJul 14
to ‎김윤비(사범대학 과학교육학과), netlogo-users

Hi Yoonbi,

When you use a variable name in a widget such as a slider it creates a global variable with that name. For that reason it is important to not put that variable in the globals statement. Try deleting the following lines from your code.

 

  ;; Input variables (controlled by sliders)
  land_turbines_count  ;; Number of land turbines to install
  sea_turbines_count   ;; Number of sea turbines to install
  wind_velocity        ;; Wind speed (m/s)

 

The code in the interface tab and the code tab (and any included files) all get compiled, so the second time a global variable shows it will produce a "There is already a global variable called …” message.

-- 

Aaron Brandes, Software Developer

Center for Connected Learning and Computer-Based Modeling

 

 

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
netlogo-user...@googlegroups.com.
To view this discussion visit
https://groups.google.com/d/msgid/netlogo-users/9e898210-7024-4826-89f3-c344bb5eb82cn%40googlegroups.com.

Paolo Gaudiano

unread,
Jul 15, 2025, 12:43:18 PMJul 15
to netlogo-users, ‎김윤비(사범대학 과학교육학과)
Hi Yoonbi,

When a variable is created in a widget in the interface, it needs to be commented out from the globals section. It’s a slightly annoying but documented aspect of NetLogo. Creating a widget with a given variable name is equivalent to creating a global.

It’s a bit of a pain when you are just developing a new model and trying to add and remove widgets. I have developed a workaround but it’s a bit convoluted. Basically I use a convention in which I prepend a character (in my case a $ though it could be a different character) to the variable name and declare it as globals[] in the code. For instance, $my-var. Then I have code at initialization time that uses a combination of “run” and “carefully” to check whether a variable with the same name but without the $ exists (“my-var”). If so, I set $my-var to my-var, otherwise I set $my-var to a default value. Then throughout the code I always refer to the version with the $. This way, if I want to expose a variable in the interface, all I have to do is to name it without the $ to match a global with the same name.

For smaller code I don’t necessarily recommend doing this, and simply remember to ;; the global if you decided to have it in a widget. But if anyone is interested I can share the code I developed.

Paolo
--
Reply all
Reply to author
Forward
0 new messages