Question about posting messages to GUI script

Hello again Defold forum! I’m having trouble updating my GUI script with the players HP. I load the GUI with a collection proxy when the first level starts. I then have this in update() of the player script:

msg.post("hud:/go#hud",self.hp)

In the hud gui script I have:

function on_message(self, message_id, message, sender)
	if message.other_id == hash("/player") then
		print("message recieved:", message_id)
		gui.set_text("number", message.id)
	end
end

Here is a screenshot of the properties for the hud collection proxy:


I’m not sure what the problem is because I am pretty sure I am referencing the URL correctly. I keep getting this error message:

Any help is appreciated!

You forgot to specify the message_id.

msg.post(receiver, message_id, [message])

So you’re giving ‘self.hp’ as the message_id and not providing any data with the message.

You need to re-read the documentation on how these functions work, you’ve also got some issues in your on_message function. You’re referencing ‘message.other_id’, but that doesn’t exist because you never sent a message with a table with the key: ‘other_id’! Perhaps you meant to use ‘sender’? Then you’re using ‘message.id’ with gui.set_text, which, like ‘message.other_id’, does not exist because you never set it, possibly it’s a typo and you meant to put ‘message_id’? But the ‘message_id’ will always be a hash, not a string, so it won’t work with gui.set_text.

Doc for msg.post: API reference (msg)

Doc for on_message: API reference (go)


[Edit] Sorry, I got a bit sidetracked from the main question there. Probably your URLs are wrong. You’re using the socket “hud”, which I doubt exists.

From the Addressing manual:

"A URL is an object, usually written as specially formatted strings. A generic URL consists of three parts:

[socket:][path][#fragment]

socket

  • Identifies the game world of the target. This is important when working with Collection Proxies and is then used to identify the dynamically loaded collection."

If you specify a different socket, it means you’re addressing something in a completely different game world, so unless you’re using Collection Proxies, then you never need it (almost never).

I can’t tell you what the correct URL would be without knowing how your collections are set up. Where is your HUD compared to your Player?

2 Likes

Please note that message.other_id is specified in physics response messages such as collision_response. It is not part of all messages that are sent.

You can however check who the sender of a message was by looking at the sender argument to on_message.

2 Likes

Yeah I was loading it as a collection proxy before but now I attached the hud script to the player which seems more straight forward. I’m not getting errors anymore but I am having trouble getting the gui script to actually receive the message now. Here is what I have for the update method in the hud.gui_script:

function on_message(self, message_id, message, sender)
	if message_id == hash("update_hp") then
		print("update_HP message recieved:", sender, message.hp)
		gui.set_text("number", message.hp)
	end
end

And here is the message that I am sending from the player script:

msg.post("#hud", "update_hp", {hp = self.hp})

I read the on_message reference you referred me too and I thought I implemented it correctly but nothing is updating and the print statement doesn’t print anything to console.

Cool, much better. Hmm. Try double-checking the basic stuff: do you have the gui_script set in the gui component? Are you defining multiple on_message functions? Does the gui_script get init? Does the gui_script get any messages at all?

3 Likes

Thank you so much I got it working now!! I can’t believe I didn’t attach the script to the actual GUI… also when I was trying to set text I wasn’t referencing a node.

3 Likes