Pigeon - easy and safe messaging library for Defold

Welcome to the Defold community! :wave: :wink:

Since you can’t set an order of init() calls if the objects are instantiated in one cycle*, such message, that I would need to send in initialization, I simply rather send from it’s own on_message, e.g:

board.script:

local pigeon = require "pigeon.pigeon"
local H = require "pigeon.hashed"

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

function on_message(self, message_id, message)
    if message_id == H.late_init then
        -- do late-initialization here
        pigeon.send("to_other_subscriber")
    end
end

As of Pigeon 1.1 (check above) you also gets false from pigeon.send() when no subscribers are subscribed to given message, so something like this should wait to send the message to the one subscriber:

while(not pigeon.send("to_subscriber")) do end
-- or:
self.limit = 0
while(not pigeon.send("to_subscriber") and self.limit < 100 ) do self.limit = self.limit + 1 end

Although I do not recommend such loop that might become infinite. It’s better to write a controlled timer or at least add some kind of limiting counter as above, if you really don’t know when other subscriber will be subscribed.

Related:

*From documentation:

The order in which game object component init() functions are called is unspecified. You should not assume that the engine initializes objects belonging to the same collection in a certain order.

2 Likes