Gui.set_text() and concatenating strings (SOLVED)

Hello, it’s me again (yeah i’m not very good in programmation i think)

Today i’m trying to add a HUD wich will show the level and the HP of both of my player, it look like this :

The gui script is simple :

function init(self)
	self.p1_life = 120
	self.p1_niv = 1
	self.p2_life = 100
	self.p2_niv = 1
end

function update(self, dt)
	gui.set_text(gui.get_node("p1_lvl"), self.p1_niv)
	gui.set_text(gui.get_node("p1_hp"), self.p1_life"/"math.floor(120 * 1.309 ^ (self.p1_niv - 1)))
	gui.set_text(gui.get_node("p2_lvl"), self.p2_niv)
	gui.set_text(gui.get_node("p2_hp"), self.p2_life"/"math.floor(100 * 1.302 ^ (self.p2_niv - 1)))
end

function on_message(self, message_id, message, sender)
	if message_id == hash("p1") then
		self.p1_life = message.life
		self.p1_niv = message.niveau
	elseif message_id == hash("p2") then
		self.p2_life = message.life
		self.p2_niv = message.niveau
	end
end

but my problem is, when i try to set the p1_hp and p2_hp text, it doesn’t work. I know it’s because it’s not just a simple text or just a variable but both at the same time. What can i do so the final result look something like this :
120/120
(self.life = 120 (it’s the actual life) and math.floor(120 * 1.309 ^ (self.p1_niv - 1))) = 120 (it’s the max life depending on the level))

(I looked in the forum and I did not see any topic that answered this question.)

I think your question is about string concatenation. Just rewrite it as self.p1_live .. "/" .. math.floor(120 * 1.309 ^ (self.p1_niv - 1)) These 2 dots are the string concatenation operator in Lua.

6 Likes

Thanks that’s what i was looking for !

1 Like