I’m trying to create two health bar for two player characters which use the same collection, the hp value is stored inside the collection, as well as each individual ID
The issue is that for me to scale the hp bar, I am going to need the values of the hp and id in the gui object, however I have not found a way to do this
both these values are stored as self. values due the characters using the same code
I’ve tried using global functions and msg.post. But I can’t access self with the former and can’t return a value with the later.
You could store the values in a module or use messages.
It’s a one-way data flow (game object → GUI script) so you don’t need a return value. Something like:
-- In player script
local function take_damage(self, amount)
self.health = self.health - amount
msg.post("hud", "set_health", { id = self.id, health = self.health })
end
-- In HUD script
function on_message(self, message_id, message, sender)
if message_id == hash("set_health") then
if message.id == "player1" then
scale_player1_health(message.health)
elseif message.id == "player2" then
scale_player2_health(message.health)
end
end
end