Page 1 of 1
Is there any way to turn a Text into a normal string?
Posted: Tue Nov 29, 2022 1:28 pm
by Breaking_Lead
I'm not a native English speaker so I'm sorry for some weird sentences.
I used <code>love.graphics.newText()</code> to create a Text object.But I want to get the content of it for debugging and printf() function.
I have looked the wiki up but there are no functions to do this.How can I do?
Re: Is there any way to turn a Text into a normal string?
Posted: Tue Nov 29, 2022 2:42 pm
by darkfrei
Re: Is there any way to turn a Text into a normal string?
Posted: Tue Nov 29, 2022 4:06 pm
by MrFariator
I don't think there's an equivalent of Text:get() to get the string from a Text object. Only Text:set(). However, you could just store the string in a variable for later access. Like say:
Code: Select all
local textLabel = {
obj = love.graphics.newText ( font, displayString ),
str = displayString,
}
And then whenever you change the display string on textLabel.obj, you reflect the change on textLabel.str. Then whenever you need to print out the display string for debugging reasons, you just print out textLabel.str.
Re: Is there any way to turn a Text into a normal string?
Posted: Tue Nov 29, 2022 8:45 pm
by pgimeno
Here's a hack that allows you to use textobj:get() with a couple modifications.
Let's say you save this code into a file called text.lua:
Code: Select all
local mt = {}
do
local t = debug.getregistry().Text
function mt:getWidth(...) return t.getWidth(self.obj, ...) end
function mt:__tostring(...) return t.__tostring(self.obj, ...) end
function mt:set(...)
self.txt = ...
return t.set(self.obj, ...)
end
function mt:setf(...)
self.txt = ...
return t.setf(self.obj, ...)
end
function mt:addf(...) return t.addf(self.obj, ...) end
function mt:type(...) return t.type(self.obj, ...) end
function mt:typeOf(...) return t.typeOf(self.obj, ...) end
function mt:__gc(...) return t.__gc(self.obj, ...) end
function mt:__eq(...) return t.__eq(self.obj, ...) end
function mt:getHeight(...) return t.getHeight(self.obj, ...) end
function mt:add(...)
local txt = ...
if type(txt) == "string" then
self.txt = self.txt .. txt
else
assert(type(self.txt) == "table")
local nelems = #self.txt
for i = 1, #txt do
self.txt[nelems + i] = txt[i]
end
end
return t.add(self.obj, ...)
end
function mt:getDimensions(...) return t.getDimensions(self.obj, ...) end
function mt:clear(...) self.txt = nil return t.clear(self.obj, ...) end
function mt:setFont(...) return t.setFont(self.obj, ...) end
function mt:getFont(...) return t.getFont(self.obj, ...) end
mt.__index = mt
mt.__call = function (self) return self.obj end
end
function mt:get()
return self.txt
end
local oldNewText = love.graphics.newText
love.graphics.newText = function(...)
local textobj = { txt = select(2, ...) }
textobj.obj = oldNewText(...)
return setmetatable(textobj, mt)
end
Caveat emptor: not all methods are tested, and there's no tracking of coordinates in add().
(This would be a lot easier if we had set/getUserData like in some physics bodies, but as part of Object.)
Now by modifying your code as follows, you can use the get() method. First, you need to require 'text.lua' at the beginning of your code; second, in every love.graphics.draw you need to change your text object to add parentheses after it like this:
Code: Select all
love.graphics,draw(myTextObject, x, y)
-- change the above to this:
love.graphics,draw(myTextObject(), x, y)
Re: Is there any way to turn a Text into a normal string?
Posted: Wed Nov 30, 2022 4:04 am
by Breaking_Lead
Thank you very much. It does work. But I'm still wonder why there's a Text object but love.graphics.printf() can't use it as an argument. And it seems impossible to get the raw string/colored string of it without hacking.
So maybe Text is designed for drawing but not for exact format control?
BTW, I'm just adding translation for my game xD.
Re: Is there any way to turn a Text into a normal string?
Posted: Wed Nov 30, 2022 8:47 pm
by pgimeno
Breaking_Lead wrote: ↑Wed Nov 30, 2022 4:04 am
Thank you very much. It does work. But I'm still wonder why there's a Text object but love.graphics.printf() can't use it as an argument. And it seems impossible to get the raw string/colored string of it without hacking.
So maybe Text is designed for drawing but not for exact format control?
BTW, I'm just adding translation for my game xD.
A Text object is a mesh made of quads which represent letters, but despite the name, it has no idea of the text it represents. It's a graphical object.
Re: Is there any way to turn a Text into a normal string?
Posted: Wed Nov 30, 2022 9:12 pm
by slime
People have suggested renaming the object for love 12.0, it might be a good idea - maybe TextBatch or something?
Re: Is there any way to turn a Text into a normal string?
Posted: Fri Dec 16, 2022 1:18 pm
by Gunroar:Cannon()
slime wrote: ↑Wed Nov 30, 2022 9:12 pm
People have suggested renaming the object for love 12.0, it might be a good idea - maybe TextBatch or something?
Hmmm ... nah. Anyone who bothers to use it should know what it is. I didn't even really know that existed until I saw it on the wiki through this page (+it's been named that since v10 so...). But if you want you can still change it.
Re: Is there any way to turn a Text into a normal string?
Posted: Fri Dec 16, 2022 1:47 pm
by slime
I already changed it after that post.
Re: Is there any way to turn a Text into a normal string?
Posted: Fri Dec 23, 2022 5:11 pm
by Gunroar:Cannon()