How do you make a player health GUI using hearts or other sprites? (SOLVED)

For reference, I want a GUI that looks similar to this heart system from the original Legend of Zelda:

image
Right now I have a GUI scene with a few heart sprites in it, but I have no idea how I would hook these hearts up to the player’s current health. Any ideas?

It really depends on how you have structured things. Say your health logic is stored in your player script. Any time you take damage or heal (i.e. the health changes), you could send a message to your health GUI script to notify about the change.

msg.post("gui_address_here", "player_health_update", {new_health = health})

Then in the on_message function of your GUI script you’d have something like:

if message_id == hash("player_health_update") then
	update_health_graphic(message.new_health)
end

In this case update_health_graphic() is a function that does whatever is necessary to update the GUI graphics to whatever value is passed by message.new_health.

4 Likes

This is a great example of how to use the message passing system. You want to send messages to notify other components of changes that happen infrequently (ie not every frame). Changes to player health, score, ammo etc are all good examples.

1 Like