The message system uses a FIFO queue (first in, first out).
When we flush the queue, we pick the first message, dispatch that to the correct handler (e.g. your script).
The handler might push other messages onto the queue, and these will end up last in the queue.
We dispatch messages several times each frame (one tick of the game update loop), once after each component type.
When you add the first two messages, the queue will look like this:
msgs= [("/map", "createMap"), ("/ai", "startBattle")]
After the createMap
is received in on_message
, you push two more messages:
msgs= [("/ai", "startBattle"), ("/units", "placeMobs"), ("/units", "placeCharacters")]
At this point, it’s clear that the call to startBattle
will come before placeMobs
/placeCharacters
What you are doing is called asynchronous programming, and that usually needs some kind of synchronization to make sure things happen in the right sequence. E.g. make sure to not post the startBattle
until after the map is actually created.
Another way of solving your problem, is to use Lua modules with functionality to manipulate your data. E.g.
local level = require("scripts/level")
function createMap(self)
level.createMobs(self)
level.createCharacters(self)
...
end