How to make global constants and variables?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By start123

I want all the variables in one script to be used in other scripts. How should I do it?

:bust_in_silhouette: Reply From: jgodfrey

The easiest way is to create a singleton script. The items in that script can be directly accessed from other scripts.

  • Create the script with the “global” variables
  • Save it (for example, assume it’s called Globals.gd)
  • Project | Project Settings | AutoLoad (tab)
  • Add the script to the list (ensure Singleton is checked)

Now, to access anything in it (say a playerSpeed variable), just preface the reference with the script name. For example:

var mySpeed = Globals.playerSpeed

Also, note you can have multiple singleton scripts if you want.

You can also add functions and use the _ready() function of the Singleton.

deaton64 | 2020-05-15 14:23

How can I make such a command with an image or a number.

start123 | 2020-05-19 11:44

Not quite sure what you’re asking.

But you can use the Global variables from anywhere in your code and set the values to what you want.

deaton64 | 2020-05-19 20:11