How do I get label text?(DEF-3016)(SOLVED)

I want to get text from label.

How i can do it?

You can’t. Sorry. You can get the text from a tex node in a gui, but not from a label.

2 Likes

You need to create a variable (call it self.labeltext) and simply always set the label to self.labeltext. then you can get self.labeltext whenever you need it

3 Likes

Why you develop this function ? Is it hard ? :roll_eyes:

No, it’s likely very easy to add support for label.get_text(). Am I right @Mathias_Westerdahl?

2 Likes

It is not super easy since the label is no accessed directly inside the engine - label.set_text() is an asynchronous message.

I’d have to ask what the use case is? Why do you need to get the text of the label @merakla25 ?

I have a use case that is not absolutely needed. If I could I would add it as an option for localization keys. So users can put the locale key in the label’s text, and then swap it at start based on what’s in the label, but that’s not necessary and not probably best way to set keys for label nodes.

It would probably work to keep a table mapping localization key to label component.

1 Like

One reason we cannot easily get the label text, is that it’s not implemented via the property system (since we don’t support strings there).
But, if there instead was a read only property “text_hash”, one could use that to do lookup in a localisation table for instance. I’m brainstorming here, and haven’t run it by the team yet :wink:

3 Likes

I think you can, if use set_text function engine is register with a variable to ram.

Eg.

set_text("#label",“Welcome!”)

get_text("#label") this function are getting from ram

I want to if click letter show in label.

But i solved this problem.I use dynamic variable is solve. :grinning:

local goput = require "main.goput"
local basilimi
local kontrol
local harf
local squares = {1, 4, 9, 16, 25, 36, 49, 64, 81}
local secilen_string = ""
local yazilacak_string = ""
local function handle_click(url, action_id, action)
	local abc  
	abc = string.sub(tostring(url), 12,31)
	abc = abc .. "#label"
	bu_url = string.sub(tostring(url), 31,31)
	if action_id == hash("touch") and kontrol==false then
		basilimi = true
		kontrol = true
		local xx = tostring(harf_table[tonumber(bu_url)]);
		yazilacak_string = yazilacak_string .. xx
		label.set_text("/four_boxes/go#label",yazilacak_string)
	end
	if action.released and kontrol == true then
		print("Dokundu")
		basilimi = false
		kontrol  = false
		yazilacak_string = ""
	end
end

function init(self)
	go.set(".", "scale", vmath.vector3(0.13, 0.13, 0))
	msg.post(".", "acquire_input_focus")
	--msg.post("@render:", "clear_color", { color = vmath.vector4(95 /256, 129 / 256, 161 / 256, 1 ) })
	self.tiklandimi = false
	basilimi = false
	kontrol = false
	go.set("#sprite", "scale", vmath.vector3(1.5, 1.5, 1))
	goput.add("#sprite", nil, handle_click)
end

function on_message(self, message_id, message)
	if message_id == hash("collision_response") and kontrol==true and self.tiklandimi == false then
		go.set("#sprite", "tint", vmath.vector4(0.15, 0.15, 0,3))
		self.tiklandimi = true
		kontrol = false
	end
end

function update(self, dt)
	if message_id ~= hash("collision_response") and kontrol == false and basilimi == false then
		self.tiklandimi = false
		go.set("#sprite", "tint", vmath.vector4(1, 1, 1,1))
	end
end

function final(self)
	msg.post(".", "release_input_focus")
	goput.remove("#sprite")

end

function on_input(self, action_id, action)
	return goput.on_input(action_id, action)
end

Here’s a super simple solution:

-- map label urls to text strings
local labels = {}

-- set text on a label
-- will also store the text in the lookup table
local function set_text(url, text)
	labels[url] = text
	label.set_text(url, text)
end

-- get text set on a label
-- the text must have been set using the set_text() function
local function get_text(url)
	return labels[url]
end


function init(self)
	set_text("#label", "my text")
end

function update(self, dt)
	print("The current text on label is", get_text("#label"))
end

You could also wrap it into a Lua module for easier reuse.

2 Likes

Thanks my bro.

This is perfect :slight_smile:

Solved in How do I get label text?

2 Likes