I’m using an injection framework (bearcat) into a PlayCanvas project.
I would like to be able to load the framework with its script dependency before others script execute.
I am using the script priority feature to load the framework first, but it will load others dependency while PlayCanvas start initializing others scripts.
I can have a different scene dedicated to load the framework and then load the main scene.
But is there a way to pause PlayCanvas initialization and resume it while other libraries are ready?
I’m not sure I completely understand the problem. But the way playcanvas loads scripts is this:
It builds a list of all scripts to load, this list starts with the priority scripts then includes all scripts that are referenced by script components in the scene.
These scripts are all loaded using script tags (and hence are executed when they load).
I have Script 1 and Script 2 in this order in my Script Priority list.
Script 1: Call an asynchronous service to get DATA1.
Script 2: Needs DATA1 to initialize.
Entity Script Component 1: Needs to access DATA1 in initialize()
Right know, I can’t do that because Script 2 and the scene will execute and start before Script 1 finishes its asynchronous call.
What I did is have an empty scene that is called first that is responsible for doing asynchronous call, once everything is ready I load the actual scene.
The thing I don’t like is that my scene can’t be launched independently and I can’t live edit them in the editor…
var async = function (callback) {
// load data
callback(data);
};
async(function (data) {
// initialize SCRIPT 2 here
// fire loaded event for entities
var app = pc.Application.getApplication();
app.fire("dataloaded");
});
ENTITY SCRIPT
initialize: function () {
if (window.dataInitialized) {
this.dataInitialize();
} else {
app.on("dataloaded", this.dataInitialize, this);
}
},
dataInitialize: function () {
//...
}
Basically delay initialization until the data is loaded.
You can check if DATA1 is available inside of Script 2 initialize method. If it is, then you just do initialize, and mark script as “active” so that you safe to run “update” method.
If DATA1 is not available in initialize method of Script 2, then you subscribe to somewhere (I prefer to subscribe to app usually). Then once it gets available it will finish initialize and will mark script as “active”. So it will function as expected.
This is general async workflow of things.
Another option would be is to construct script object and attach it to entities that needs it only once DATA1 is available. You need to make sure that racing condition is taken in account, where you might have DATA1 get loaded before scene initializes, or can get loaded after scene initializes.