Is there any way to turn a Text into a normal string?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Breaking_Lead
Prole
Posts: 2
Joined: Tue Nov 29, 2022 1:15 pm

Is there any way to turn a Text into a normal string?

Post 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?
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Is there any way to turn a Text into a normal string?

Post 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.
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: Is there any way to turn a Text into a normal string?

Post 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)
Breaking_Lead
Prole
Posts: 2
Joined: Tue Nov 29, 2022 1:15 pm

Re: Is there any way to turn a Text into a normal string?

Post 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.
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: Is there any way to turn a Text into a normal string?

Post 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.
User avatar
slime
Solid Snayke
Posts: 3129
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?

Post by slime »

People have suggested renaming the object for love 12.0, it might be a good idea - maybe TextBatch or something?
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Is there any way to turn a Text into a normal string?

Post 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.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
slime
Solid Snayke
Posts: 3129
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?

Post by slime »

I already changed it after that post. :nyu:
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Is there any way to turn a Text into a normal string?

Post by Gunroar:Cannon() »

:crazy:
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 20 guests