Extra Scene-based script preloading order

Would it be possible to set a custom Script-Loading order depending on scene?

We have one scene (Scene 1) that requires a set of scripts.

Then we have another scene (Scene 2) that requires a few of the scripts from Scene 1 but not all of them.

Some of the scripts in Scene 1 absolutely must not be loaded in Scene 2.

Another issue with preloading.

We tried creating references to the scripts as assets in a managing script. But if the script assets aren’t preloaded the assets return null. If they are preloaded they return the correct script asset. All other types return the correct asset regardless if they are preloaded or not preloaded. Maybe this is intended?

What is the reason for this? Perhaps there is another way to do this?

Why does the order matter from scene to scene?

We have different projects that use a relatively similar base. Some scripts will need to be updated for all projects derived from the original. These scripts are communication scripts with other API’s, but also graphics related, shaders and managing various components.

The reason is mainly because there is no merging of forked projects. If we fork one project and update bugs found throughout the original project other people working on the fork do not recieve the fixes unless we manually re-implement them in the forked project.

Another reason is that copying and pasting assets and level components between project is iffy at best, but it works fine between Scenes .

The Script Loading Order is what PC calls it, the order doesn’t matter. Only the selection of which scripts should be loaded. Which is AFAIK only determinable from the Script Loading Order in the Settings tab. And that loading order is updated to be the same for each Scene.

Ah, I see. So you are not really switching scenes but using scenes as a separate ‘project’ so to speak?

AFAIK, this is not possible (at the moment. This is under suggestions and feedback after all :wink: ). The build process by default, concatenate all the project scripts into one file, removing comments and loads it as part of the preloader which exists for the lifetime of the page.

I’m not sure how a scene based script loader/filter would work in practise but in this use case, a way to select which scripts/files should be included into the build at the point of publish may be better?

Edit: The script loading order doesn’t allow the user to omit scripts which doesn’t fit your use case ?

Each script has a preload field, you can set that to false for scripts you don’t want to load automatically. You will have to load them yourself manually whenever needed.

Does the script code still get concatenated as part of the build process if the Preload flag is unticked?

As far as I can remember right now no it’s not. Need to verify

Exactly! :slight_smile: Since it’s easier to copy and paste Level / Scene objects between Scenes in a project than between two projects.

Maybe, but we’d rather have a Script Loader for each Scene. Maybe it can concatenate all the Project Scripts as a project_scripts.js file and each Scene as a bunch of _scene01_scripts.js files?

In that case we can just append or load the collected scene files depending on which Scene is “active”. And the correct scripts are used when launching the specific Scene. :slight_smile:

Omitting scripts that don’t fit for the active Scene doesn’t work as it affects the other Scenes as well. Loading a script semi-manually by using a URL link causes the headache of having to specify the adress and manually download each of those scripts to the project as they won’t be included.

As far as we tested, if we make a script object with an array of script assets or a single script asset that have Preload unticked they should be included in the scene. They accompany the project on download as separate assets. Which works for publishing but doesn’t seem to work during launch from the editor as all “assets” to that script that are of the “type” script return null. Downloading the concatenated project does not include the script assets anywhere we found even though they are referenced by a Scene object and has Preload unticked.

Going around it and recreating one of those script files as a textfile and pointing at that as a resource returns the correct assets (when Preload is unticked) if type and asset is changed to something other than “script”.

Not seeing non-preloaded scripts in the launch page seems like a bug which I believe I have a fix for.

When you publish or download a project then that seems to work fine - remember when you have a script that is not preloaded you need to load it yourself like so:

var scriptAsset = this.app.assets.get(scriptAssetId);
scriptAsset.ready(function () {
     someEntity.addComponent('script'); // if script component doesn't exist
     someEntity.script.create('scriptName');
});
this.app.assets.load(scriptAsset);

We deployed a fix for non-preloaded scripts not being available in the asset registry in the launch page.

Thank you for the fix Vaios!

How is the concatenate scripts option handling non-preloaded scripts on project download after this fix? Are they concatenated as well? Or are they included as separate assets?

They are included as separate assets.