# Script Loading Order

**URL:** <https://forum.playcanvas.com/t/script-loading-order/751>\
**Category:** Help & Support\
**Created:** [May 30, 2015, 7:23pm UTC](https://forum.playcanvas.com/t/script-loading-order/751 "2015-05-30T19:23:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/dave/32/4558_2.png) [@dave](https://forum.playcanvas.com/u/dave)\
**Post date:** [May 30, 2015, 7:23pm UTC](https://forum.playcanvas.com/t/script-loading-order/751/1 "2015-05-30T19:23:01Z")

</div>

Continuing the discussion from [PlayCanvas Editor Feedback](http://forum.playcanvas.com/t/playcanvas-editor-feedback/616/97):

> [@PlayCanvas Editor Feedback](https://forum.playcanvas.com/t/playcanvas-editor-feedback/616/97):
>
> Thanks 😄!
> 
> Quite a lot of my project broke with the update - I notice that the method I used to initialize early Javascript functions no longer works. Would be really good to be able to get either a way of running code on startup without the need to create a “script” per se or to be able to specify files to be loaded before any of the other scripts are executed.
> 
> For instance my pc.script.create(‘xxx’, function(uuu) {}) now runs before all of my JS is loaded. I’ve got a workaround - but I have a function I would prefer to execute inside that script around my script constructor to create a state machine. This no longer works so I’ve bodged it together by screwing with **proto** but ugly hack.
> 
> Before it looked like var MyScript = pc.createStateMachine(function MyScriptConstructor(entity) {});
> 
> But that no longer works as the pc.extend isn’t running before the call back to execute the pc.script.create.

Is your problem the order of script loading? The order of script loading has changed with this update. Previously we would order loading based on the order the scripts appear in the hierarchy. Now we load all scripts in a repository, so we don’t use the hierarchy order. Instead we’ve added a way to set the loading priority of your scripts.

In the main menu (click the PlayCanvas logo in the top left) select “Script Priority”. You can use this dialog to set some or all your scripts to load in a specified order.

Usually for playcanvas style scripts the order doesn’t matter but if you need to specify some scripts to load first, you should add them to this list.

Hope that solves the problem.

---

<div class="post-metadata">

**Author:** ![whydoidoit](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/whydoidoit/32/4630_2.png) [@whydoidoit](https://forum.playcanvas.com/u/whydoidoit)\
**Post date:** [May 30, 2015, 9:41pm UTC](https://forum.playcanvas.com/t/script-loading-order/751/2 "2015-05-30T21:41:13Z")

</div>

Brilliant! - looks like lmao was having the same problem

---

<div class="post-metadata">

**Author:** ![whydoidoit](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/whydoidoit/32/4630_2.png) [@whydoidoit](https://forum.playcanvas.com/u/whydoidoit)\
**Post date:** [May 30, 2015, 9:43pm UTC](https://forum.playcanvas.com/t/script-loading-order/751/3 "2015-05-30T21:43:55Z")

</div>

One slight problem with that

I have too many scripts and they don’t all fit on the screen ☹!!! Having a bit of a problem selecting the one I need which is off the top!

 ![](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/a/aeb645f077146cf557b5461181ca667dd8d6bc29.png)

---

<div class="post-metadata">

**Author:** ![dave](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/dave/32/4558_2.png) [@dave](https://forum.playcanvas.com/u/dave)\
**Post date:** [May 31, 2015, 7:56am UTC](https://forum.playcanvas.com/t/script-loading-order/751/4 "2015-05-31T07:56:13Z")

</div>

OK. We can fix that. 😄

Do you need to specify the exact loading order for all your scripts? Or is it possible just to include a few in the priority loading?

Note this list doesn’t effect “execution” order.

---

<div class="post-metadata">

**Author:** ![whydoidoit](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/whydoidoit/32/4630_2.png) [@whydoidoit](https://forum.playcanvas.com/u/whydoidoit)\
**Post date:** [May 31, 2015, 8:59am UTC](https://forum.playcanvas.com/t/script-loading-order/751/5 "2015-05-31T08:59:21Z")

</div>

It’s ok, rotating my monitor worked around it this time 😄

I just need the key libraries I use loaded before any pc.script.create calls - a couple I’ve written and d3, lodash etc. I just couldn’t get to a couple in the normal monitor orientation!

Working now though.

---

<div class="post-metadata">

**Author:** ![dave](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/dave/32/4558_2.png) [@dave](https://forum.playcanvas.com/u/dave)\
**Post date:** [May 31, 2015, 8:19pm UTC](https://forum.playcanvas.com/t/script-loading-order/751/6 "2015-05-31T20:19:25Z")

</div>

I’ve just deployed an update to the script selection. We now use the standard asset picker to select scripts. This should fix the problems when there are too many scripts to select.

---

<div class="post-metadata">

**Author:** ![Charlie\_Van\_Norman](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/c/3da27b/32.png) [@Charlie\_Van\_Norman](https://forum.playcanvas.com/u/Charlie_Van_Norman)\
**Post date:** [April 23, 2021, 7:27pm UTC](https://forum.playcanvas.com/t/script-loading-order/751/7 "2021-04-23T19:27:20Z")

</div>

I don’t see Script Loading Order option in PlayCanvas menu. Am I missing something?

 ![Screenshot (32)](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/f/f3c122485bfe303dc28cd245c09c562fd7e32d6e.png)

---

<div class="post-metadata">

**Author:** ![Charlie\_Van\_Norman](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/c/3da27b/32.png) [@Charlie\_Van\_Norman](https://forum.playcanvas.com/u/Charlie_Van_Norman)\
**Post date:** [April 23, 2021, 7:29pm UTC](https://forum.playcanvas.com/t/script-loading-order/751/8 "2021-04-23T19:29:18Z")

</div>

Update: Found it. The option has moved. click any script (so the script itself is selected, not entity), then on the inspector pane there is an option to change loading order.

 ![Screenshot (33)](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/2/250080231c1c9252431ef5652c16f6f10d6e4ee7.png)

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [April 23, 2021, 7:58pm UTC](https://forum.playcanvas.com/t/script-loading-order/751/9 "2021-04-23T19:58:10Z")

</div>

You can also get to that by clicking on the settings icon in the bottom left of the screen (the gears like icon)

---

<div class="post-metadata">

**Author:** ![Charlie\_Van\_Norman](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/c/3da27b/32.png) [@Charlie\_Van\_Norman](https://forum.playcanvas.com/u/Charlie_Van_Norman)\
**Post date:** [May 27, 2021, 7:38pm UTC](https://forum.playcanvas.com/t/script-loading-order/751/10 "2021-05-27T19:38:55Z")

</div>

Hey everyone, I noticed that PlayCanvas does not support this.initialize() order, and will load in order of which _entity_ was loaded first in the scene. I needed granular control over initalizations, so I wrote a wrapper function for this. Hope it is useful to you.

NOTE: You need the wrapper to kick off _after_ all .initialize() have been run, so I kick it off using .postInitialize() and keep an instance on an entity in the scene. So you will need to create an entity in your scene and attach this to it as a script for it to work

NOTE: SomeScript.prototype.postInitialize will ONLY be called if that script also has SomeScript.prototype.initialize on it, so you cannot remove the empty “initialize” function

```javascript
var InitQueue = pc.createScript('initQueue');

InitQueue.prototype.initialize = function(){ 
};

InitQueue.prototype.postInitialize = function() {
   InitializationQueue.RunInitializations();
   InitializationQueue.Finished = true;
};

// Force order of initializations 
// We create an array of initialization functions
// In each script/s initializatoion that we need ordered, we call this "inits" object and add to it via InitalizationQueue.Add(sourceObj,index);
// Then we use InitHook which exists on an entity in the scene to run all these intializtions in order using its postInitialization function
// This means any ORDERED init that uses this InitializationQuee will happen after ALL other "regular" prototype.initializations are run
// Note that we check for undefined in case you assign inits[0] = something and inits[2] something leaving out init[1], it should still work
// Update: Added lists so that inits will stack if they have the same index,
// for example if you add 3 inits to index 0 and 4 inits to index 1, all 3 of index 0 will finish first but that is unordered
// it will skip any empty indices so don't worry about filling them; adding one script to [5] and one script to [6] will work as expected
//   
var inits = []; // stores the whole source object, not just the "init" fn. Global var, whatever, sue me.
var InitializationQueue = {
    RunInitializations : function (){
        for(var i=0;i<inits.length;i++)
        {
            if (inits[i] !== undefined) {
                for (var j=0;j<inits[i].length;j++){
                    inits[i][j].Init();
                    // console.log("intting:"+inits[i][j]);
                    // console.log("initting "+inits[i][j]+" on queue.");
                }
            }
        }
    },
    Add : function(source,index){
        if (this.Finished === true){
            // console.log("initting:"+source+" because app already started.");
            // The game already started, so just directly initiate it.
            source.Init();
        } else {
            var i = parseInt(index);
            if (i in inits === false) inits[i] = [];
            inits[i].push(source);
        }
    },
    Finished : false
};

```

usage:

```javascript
// initialize code called once per entity
SomeScript.prototype.initialize = function() {
    InitializationQueue.Add(this,3);
};

SomeScript.prototype.Init = function(){ 
    // order dependent code here
};

```

```javascript
// initialize code called once per entity
AnotherScript.prototype.initialize = function() {
    InitializationQueue.Add(this,0);
};

AnotherScript.prototype.Init = function(){ 
    // order dependent code here
};

```

in this example AnotherScript will have its Init function called before SomeScript

Also note that if you clone an entity with a script on it calling InitializaitonQueue at run time, it will notice InitializationQueue.Finished == true and initialize immediately as expected
