Value update with gui.set_text

Hi all,
probably a stupid question but couldn’t find an answer on the forum.
I’m starting to get familiar with GUI and have the following problem:
I have a turn counter which I would like to display on the side and update its value after every turn.
The update itself is doing well however the old value still remains in the back (the values are printed upon each other).
I don’t think I should always delete and create a new text node, so is there an easier way?

Code snippet from init():

-- turn counter value
local value_pos = vmath.vector3(pos.x, pos.y, 0)
local value_text = gui.new_text_node(value_pos, "0")
gui.set_id(value_text, "turn_counter_value")

and from update():

if message_id == UPDATE_TURN_COUNTER then
	gui.set_text(gui.get_node("turn_counter_value"), tostring(turnCounter))
end

Thanks in advance!

Are you creating a new text node every time it updates? It sounds like you have multiple nodes by mistake. You shouldn’t have to delete and create new text nodes, just do gui.set_text() on the same node every time.

If you only have one counter display (or any static number), you can place it in the editor and not create a new node from your script at all.

No, I do not create new nodes.
I have one gui.new_text_node in the init() with a starting value of 0 and try to change it in the on message part.

However in the meanwhile I managed to solve it as not creating the text node in the init() section and
checking if the node exists in the on message part with pcall and create if needed otherwise update.

if message_id == UPDATE_TURN_COUNTER then
	local exists, node = pcall(gui.get_node, "turn_counter_value")
	if exists then
		-- update value
		gui.set_text(gui.get_node("turn_counter_value"), tostring(turnCounter))
	else
		-- create node
		local pos = vmath.vector3(26 * TILE_SIZE + HALF_TILE, 12 * TILE_SIZE, 0)
		local value_pos = vmath.vector3(pos.x, pos.y - HALF_TILE, 0)
		local value_text = gui.new_text_node(value_pos, tostring(turnCounter))
		gui.set_id(value_text, "turn_counter_value")
		gui.set_scale(value_text, vmath.vector4(2))
	end
end

I’m not sure what’s the issue really. your first code should work (but can be simplified)

function init(self)
    self.text_node = gui.get_node("turn_counter_value") -- a gui text node
end

function on_message(self, message_id, message, sender)
    if message_id == UPDATE_TURN_COUNTER then
        gui.set_text(self.text_node, tostring(message.turnCounter))
    end
end
1 Like

To be honest I do not understand it either…
What I saw was that the init value got stuck somehow in the backround, I mean all other updated values worked fine (disappeared and the new value was shown).
Will leave it as it is now and will think about it later during the refactor.