Component async loading issue

Hi all,

I have been using Playcanvas for a while an first of all congratulations, is probably the best WebGL engine I’ve found so far.

I’m using Playcanvas but just the plain engine, I don’t use the editor (only for exporting assets). Everything is good but I have an issue with the component loading. I add components in the following way:

entity.addComponent("script", {
                scripts: [{
                        url: 'js/app/scripts/script1.js',
                        name: 'script1',
                        attributes: []
                    },{
                        url: 'js/app/scripts/script2.js',
                        name: 'script2',
                        attributes: []
                    }]
            });

But I can’t really access the scripts until a few seconds later. If I do:

entity.script.script1

right after the component loading, script1 is undefined.

I guess the components loading is done in a async way. Is there any way I can know the components have been effectively loaded and I can start using them?.

Thanks for the great work.

When running from the Editor we preload assets that are referenced in the Scene. If a script is preloaded then component creation is synchronous and your code would work as expected.

If you are running your own engine, I recommend loading all the scripts you need upfront so that you know they will be present. Easiest way to do this is to include them in your html file. Otherwise you can load them using ScriptRequests. Worth noting that ScriptRequests are being deprecated and will be replaced with the system in this branch.

Thanks Dave, that will make the trick.

Thanks a lot for your support!