Global variables become nil when declared in game object files (NOT A BUG)

I don’t know if it is a bug or a property because I found nothing about it on the forum or on the internet.

  • Declare a global variable in a script located in a game object file
  • Print the global variable in annother script located in the same game object file
    :arrow_right: It works
  • Now print this variable in a gui script on the same game object or a normal script located in a different game object
    :arrow_right: The variable is nil for BOTH of the scripts

I looks strange that’s why I think it can be a bug, if global variables were only unavaliable for gui or scripts outside the game object file I could understand but it actually change it to ‘nil’

If it is normal I suggest to add something in the guide to warn users about global variables used in a different game object file (or make it more visible because I searched it and I could not find) :sweat_smile:

You can check ”shared script state” to control this behavior. Scripts and GUI scripts are in different Lua context by default.

See https://www.defold.com/manuals/project-settings/#_script

and https://www.defold.com/manuals/lua/#_lua_contexts_in_defold

1 Like

There was an issue with one of the template projects that didn’t have this setting enabled. Did you create your project from a template?

No I started from blank state

Meaning what? You started with an empty game.project in an otherwise empty folder? Or did you create the project some other way? And was the problem the “shared script state” setting or something else?

1 Like

Sorry I thought you were talking about tutorial projects, I still use the Editor 1 so I have no template (I hope I understood the question correctly)

Yes it was that

1 Like

Ah, ok, cool. You really should switch to editor 2. We’re deprecating the old one soon.

1 Like