Message evaluation order

Is there a way to force message evaluation order? Consider the following:

init(self)
  startgame()
end

function startgame()	
	msg.post(".", "hideUpperMenu")
	--msg.post(".", "hideButton", menu.mainButton)
	--msg.post(".", "hideButton", menu.dataButton)
	--msg.post(".", "hideButton", menu.statsButton)
	--msg.post(".", "hideButton", menu.levelButton)
	--msg.post(".", "hideButton", menu.perksButton)
	--msg.post(".", "hideButton", menu.appearanceButton)
	msg.post(".", "showButton", menu.mainButton)
	msg.post(".", "showButton", menu.dataButton)
end

function on_message(self, message_id, message, sender)

-- Hide a button
	if (message_id == hash("hideButton")) then		
		gui.set_enabled(gui.get_node(message.node), false)			
	end
	
-- Reveal a button
	if (message_id == hash("showButton")) then				
		gui.set_enabled(gui.get_node(message.node), true)		
	end
	
-- Hide upper menu
	if (message_id == hash("hideUpperMenu")) then
		msg.post(".", "hideButton", menu.mainButton)
		msg.post(".", "hideButton", menu.dataButton)
		msg.post(".", "hideButton", menu.statsButton)
		msg.post(".", "hideButton", menu.levelButton)
		msg.post(".", "hideButton", menu.perksButton)
		msg.post(".", "hideButton", menu.appearanceButton)
	end
	
end 

If I run this, I get enabled/disabled problems, but if I comment out the message post to hideUpperMenu and uncomment the rest of the code in startgame, it ends up working correctly. It’s as if my showButton and hideUpperMenu message calls are happening stimultaneously.

I’d like to use hideUpperMenu instead. How can I force it to finish before calling showButton?

It appears the easier way, for now, may be to separate these out into local functions and see if I run into the same problems when I’m calling them through the messenger from other scripts. Deciding when to use messaging or global functions or modules is a little murky. More excuses to experiment!

You should not rely on msg.post() order. msg.post() is asynchronous and should not be confused with the properties of making a function call.

I don’t really see the point of calling msg.post(".", "showButton", menu.mainButton) when gui.set_enabled(gui.get_node(menu.mainButton.node), true) would suffice. What’s the purpose of that extra message passing when you really only want to call a function?

Edit: Thank you @sicher for the correction regarding msg.post retaining order.

1 Like

msg.post does not send messages immediately but posts them to a message queue that is delivered at the end of the update-loop. So the execution continues immediately (that’s why it’s called “msg.post” as opposed to “msg.send”). The posted messages will be delivered in the order they were posted though.

4 Likes