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?
Is there any way to turn a Text into a normal string?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 2
- Joined: Tue Nov 29, 2022 1:15 pm
-
- Party member
- Posts: 548
- Joined: Wed Oct 05, 2016 11:53 am
Re: Is there any way to turn a Text into a normal string?
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:
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.
Code: Select all
local textLabel = {
obj = love.graphics.newText ( font, displayString ),
str = displayString,
}
Re: Is there any way to turn a Text into a normal string?
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:
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:
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
(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)
-
- Prole
- Posts: 2
- Joined: Tue Nov 29, 2022 1:15 pm
Re: Is there any way to turn a Text into a normal string?
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.
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?
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.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.
- slime
- Solid Snayke
- Posts: 3162
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Is there any way to turn a Text into a normal string?
People have suggested renaming the object for love 12.0, it might be a good idea - maybe TextBatch or something?
- Gunroar:Cannon()
- Party member
- Posts: 1141
- Joined: Thu Dec 10, 2020 1:57 am
Re: Is there any way to turn a Text into a normal string?
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.
- slime
- Solid Snayke
- Posts: 3162
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Is there any way to turn a Text into a normal string?
I already changed it after that post.
- Gunroar:Cannon()
- Party member
- Posts: 1141
- Joined: Thu Dec 10, 2020 1:57 am
Who is online
Users browsing this forum: Ahrefs [Bot] and 2 guests