Is there a "best place" to post certain messages?

Hi,

I’m following tutorials, they are excellent by the way, then this is the second time I find something like this (this is part of a GUI script):

function init(self)
    msg.post("#", "show_start")     -- [1]
    self.active = false
end

function on_message(self, message_id, message, sender)
    if message_id == hash("show_start") then      -- [2]
        msg.post("#", "enable")
        self.active = true

So in the section [1], we are sending a “show_start” messages to “#” (ourselves), to this current script.
Then in section [2] it just send another message “enable” to “#” again.

My questions are:

  • Why not sending directly the “enable” message from function init()? There is any engine rule to send these messages only from on_message function?
  • Would be the same if I create a function show_start() where I then send the “enable”/“disable” messages?

Regards.

2 Likes

Yes, it does look excessive. But this layout lets other scripts to send the ‘show_start’ message to this script. So if this message is used elsewhere, it’s ok to leave it.

Personally I’d make a dedicated function show_start(self) and call it in init() and on_message() functions.

2 Likes

I guess other scripts can also send an “enable” message to this one. Right?

Thanks for your answer!

But they won’t be able to set self.active = true variable.

1 Like

That’s a good point.

The purpose of a script sending a message to itself on init is to delay the response until all other objects’ init functions have run. I’m not sure what the broader purpose is in this case, but it will ensure that self.active = true only after all other objects are init.

It seems like a very strange workaround, but it’s very useful. You can also do it on update, etc.

3 Likes

Make sense. That’s a valuable piece of info to have. Looking into the documentation now I see how it works:

1 Like