GOs - Function init() - order of execution (SOLVED)

Dear,

I’m using different Defold assets. Because of that I have run into some issues related to order of execution of used Game Objects in my collection.

The question is if I have 3 objects A, B and C. How can I make sure that:
function init() of A is first to be executed
then function init() of B is Second
and init() C is third

I read the manual “core concept”, I couldn’t find something explaining the order of execution or how the engine decide which GO is first within the same GameCollection…

Thanks a lot

Regards
Riad

You cannot directly guarantee init order. You can however delay init. This is generally done by a script sending a message to itself.

function init(self)
  msg.post(".", "actual_init")
end

Now on the on_message when you get that message you could delay init again until some value in a shared module is not set.

1 Like

Thanks! your tip fixed my problem.

Too bad we don’t have a native mechanism to control the order…

Game objects in Defold are designed to work independently of each other. What is the specific use case where you need to guarantee a specific order of initialization?

it was the rendercam asset.

I put the camera with the hero collection and my controller object was supposed to make the link between the camera object and the hero object (using follow() function).

So the problem was that the controller was executing rendercam.follow() function before the init() of rendercam object… it didn’t work of course

but now I’m using msg.post in my controller as suggested by Pkeod. It’s working… following the debug I can see the message execution is done after init() :slight_smile:

1 Like

The issue would easily propagate, and someone would want a pre-pre-init :slight_smile:

3 Likes

If you have some kind of HUD in your game, you can defer initialization of your game objects until the overlay loads. This is what I do in my game to resolve these concerns. The UI has always a slight delay of loading so if you wait for it then Rendercam should always be ready to go by then.