Where do you store your "global" (app-wide) variables?

There are some functions in my apps that will need to know how old the current instance of the app is. I didn’t find anything on the documentation reference, so I am planning to capture the postinitialize event and then record the current time on an app-wide variable.

  1. Where do you keep your app-wide vars?
  2. Did I miss the property of the PlayCanvas engine, in the documentation, that keeps record of when it started, or for how long it has been running?
  1. The project settings have a script loading order. Whichever script has a global var declaration should be loaded before the others, then others will have access to them.
  2. There is no public API for such thing. It sounds like a simple thing one of you scripts could track by recording the app start time.

Thank you for the answer.

But is there any script that is loaded in the PlayCanvas editor that does that (a global var declaration)? At first, I thought I should just store it in the pc variable, after checking if the variable already exists, and I implement that on every script asset that needs it, like so:

if (pc.myVar === undefined) {
    pc.myVar = new Date.now();

I know a lot goes under the hoods, and was wondering if there is a best practice for that. If you had to store a number in a variable that should be accessible from any other asset in your project, where would you put it? How would you implement it?

As mentioned, create a script that contains a global variable

var MyApp = {};
MyApp.starttime = new Date().now()

In settings, make sure this script is loaded first.

Another option is to just use window.starttime = new Date().now()

Okay thank you, I’ve created a global variable to store it.

Anyway, in the tutorial page, there is a flow that shows the high level flow of a PlayCanvas application: https://developer.playcanvas.com/img/user-manual/scripting/application-lifecycle.png

According to this, as soon as the application finishes initializing everything, it fires an event called postinitialize. I am trying to listen to it to get the exact time the application starts running, but I am having troube, since the pc object does not have the EventHandler methods. Where should I invoke this? I’ve tried this so far, to no avail:


let myHandler = new pc.EventHandler();
myHandler.on('postinitialize', (e) => {
    console.log('Captured "postinitialize".')
}); // this does not capture the event but I don't know why


/* some code */

// initialize code called once per entity
Movement.prototype.initialize = function() {
    /* code code code */

    this.on('postinitialize', (e) => {
        console.log('"postinitialize" captured successfully!');

    // some more code

/* lots of other code */

Do you think that I should just initializationTime = new Date.now() inside the pototype.initialize() of the object?

According to the engine source code, its postInitialize

or implement

Movement.prototype.postInitialize = function() {

Thank you. You are correct, I failed to notice it is ´postInitialize´ with a capital ´I´; however, fixing that didn’t make it work.

The prototype.postInitialize worked.

Should be this.app.systems.on("postInitialize"). It is preferred to use the script method, though.

1 Like