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.
Where do you keep your app-wide vars?
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?
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.
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.
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?
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:
Global.js:
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
Movement.js:
/* 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?