How do you make a global constants file?


My game is going to have a number of constants, such as maxY and respawnRate. Multiple scripts will need access to all of these constants. How can I have a single js file (or whatever) that contains all of my game’s constants in one place, that can be easily accessed by any other script?


You could add a script called globals or something like that on your root Entity. Then inside that script you can define script attributes which will be visible inside the Editor so you can set their values from the Entity Inspector. Inside your script you could do something like this in the initialize method or inside the script constructor:

window.globals = this;

Then you can access the script attributes you defined like so from any script: = blah;


 blah =;

You can omit the ‘window’ variable completely and just do = blah or blah =

because the window variable is already global.

You can read more about script attributes here


It works! Thank you.

Hi I was wondering if it would be good practice to put these things under the app reference that you get from loading a script, rather than the window.

pc.script.create('gameMgr', function (app) {
    app.GAME_CONSTANTS = {};
    // ...

does that reference get messed with at all during the game’s lifecycle or any other reason not to do so?

Also wondering if doing some document inject script tags to load a 3rd party lib (like jquery, or a canvas lib) from a remote source is good / bad / indifferent. Or if it would be better to paste into a script on the root?

You can totally add global references to the app if you want to (just make sure names don’t clash). Basically we are kindof exploiting the freedom of Javascript here so whatever works for your case can be used :smile:

If you want to load a 3rd party lib it would be better to create a new script and paste all the code into it.