Question: Init order of a collection

Is there any guaranteed order of initialization for a factory created collection?

For example, let’s use Defold’s classic Bean collection.
The top level has a Bean GO.
Within the Bean is a Shield GO.
The shield GO has a script.

Here’s my question…
If the Bean GO were to also have a script, would the Bean’s script be initialized before the Shield’s script?
That feels like a reasonable thing for Defold to do. And it seems to work that way in my tests. However, I’ve read that script initialization has no guaranteed order.

In the case of nested scripts, however, initializing from top down would be very helpful.

Keith

Like you say, the order is not guaranteed. If you need to run code after init() has run on all scripts you can either send a message:

function init(self)
   msg.post("#", "run_after_init")
end

Or start a timer with a delay of 0:

function init(self)
    timer.delay(0, false, function ()
        print("this will run after init")
    end)
end
1 Like