Order of execution for event functions


When the physics part will be executed?
I didn’t find that in Defold application lifecycle manual.
Is there a more detailed diagram like this Unity - Manual: Order of execution for event functions?

I just run a simple demo to test when the fixed_updatewill be executed. I set display.update_frequency to 1 in game.project.


As the result show, update is executed before the fixed_update, is that ture?

1 Like

When in doubt, check the source code (it is not as daunting as it may seem!).

engine.cpp

In engine.cpp we have a Step() function which steps the engine one or more frames:

As you can see the StepFrame() function is getting called from Step(). Here’s StepFrame():

If you follow StepFrame() you see a few different updates being called, such as dmHID::Update(), dmJobThread::Update(), dmSound::Update() and also dmGameObject::Update() where we also set fixed update frequency, dt, accumulated time etc:

gameobject.cpp

This is where game object and component processing happens! Let’s look at the Update() function which we called from StepFrame() in engine.cpp:

In this function we iterate over all component types and call the update function of the component type:

When all components have been updated we deal with the fixed update of each component type (not every component has a fixed update function):

comp_collision_object.cpp

Each component type is in it’s own cpp file, named like comp_foobar.cpp. There is a comp_collision_object.cpp for the collision object component:

If you follow the calls in comp_collision_object.cpp you see that it branches to a comp_collision_object_box2d.cpp and comp_collision_object_bullet.cpp

What does this mean?

Component types are updated one by one. Sprites, Sounds, Scripts, Collision objects are components. Each component type will have it’s Update function called, including the Script components. Once all components have their Update function called each component type will have their FixedUpdate called, including the Script component.

The order in which component types get updated is not guaranteed, but it is also not frequently changed. You can check gamesys.cpp to see which priority each component type is assigned.

3 Likes