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?
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.
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.