love.graphics.print resets itself

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

love.graphics.print resets itself

Post by pgimeno »

I'm trying to redefine love.graphics.print and call back the original, but it only works for the first time, then it goes back to its original value. To be clearer, this snippet should be continuously outputting "Entered", but instead it is printed just once:

Code: Select all

local orig_print = love.graphics.print
function love.graphics.print(...)
  print("Entered")
  orig_print(...)
end

function love.draw()
  love.graphics.print("Hello, World!", 10, 10)
end
However, it works if I do it like this:

Code: Select all

local orig_print = love.graphics.print
local function new_print(...)
  print("Entered")
  orig_print(...)
  -- the previous line changes love.graphics.print - change it back
  love.graphics.print = new_print
end
love.graphics.print = new_print

function love.draw()
  love.graphics.print("Hello, World!", 10, 10)
end
Any idea why love.graphics.print is resetting itself?
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: love.graphics.print resets itself

Post by slime »

Which LÖVE version are you using? In 0.9.1 and older LÖVE will do that, but in 0.9.2 and newer it shouldn't.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: love.graphics.print resets itself

Post by bartbes »

slime wrote:In 0.9.1 and older LÖVE will do that,
And it only does it once.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: love.graphics.print resets itself

Post by pgimeno »

slime wrote:Which LÖVE version are you using? In 0.9.1 and older LÖVE will do that, but in 0.9.2 and newer it shouldn't.
Yes, 0.9.1. Thank you.

bartbes wrote:And it only does it once.
Good to know too.


EDIT

I'm calling love.graphics.printf() in main to get rid of that first time and it's not working. Does it do so elsewhere? Like in love.graphics.present()?

And another question. Is it ever set to something other than the initial value of the print function? In other words, is my second snippet safe?


Never mind. I've chosen an alternative that should be mostly safe and not too costly:

Code: Select all

local old_print = love.graphics.print
local function new_print (...)
  old_print(...)
  if love.graphics.print ~= new_print then
    -- it's overwritten and needs reassignment
    old_print = love.graphics.print
    love.graphics.print = new_print
  end
end
love.graphics.print = new_print
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: love.graphics.print resets itself

Post by bartbes »

Both print and printf will set themselves exactly once. So calling print and printf beforehand should do the trick.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: love.graphics.print resets itself

Post by pgimeno »

bartbes wrote:Both print and printf will set themselves exactly once. So calling print and printf beforehand should do the trick.
Ahh, I see the problem now. This works:

Code: Select all

love.graphics.print("")
local orig_print = love.graphics.print

function love.graphics.print(...)
  print("Entered")
  orig_print(...)
end

function love.draw()
  love.graphics.print("Hello, World!", 10, 10)
end
but this doesn't:

Code: Select all

local orig_print = love.graphics.print
love.graphics.print("")

function love.graphics.print(...)
  print("Entered")
  orig_print(...)
end

function love.draw()
  love.graphics.print("Hello, World!", 10, 10)
end
The only difference is the order in which orig_print is assigned, before or after the dummy print() call. I guess that it points to a different function after the first assignment. I was wrongly assuming it was reassigned to the same function, but that's obviously not the case. Thanks.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 49 guests