Page 1 of 1

love.graphics.print troubles

Posted: Wed Nov 18, 2020 7:39 am
by VonMan1aC
Hi all,

Having a strange problem printing variables to screen using love.graphics.print.

I want to print the current screen resolution to the screen in the settings menu,
I am getting the variables with the following
local resolutionw = love.graphics.getWidth()
local resolutionh = love.graphics.getHeight()

and am trying to print it to screen with

love.graphics.print(tostring(resolutionw)..' x '..tostring(resolutionh), WINDOW_WIDTH / 3 + BUTTON_WIDTH / 2 + margin, WINDOW_HEIGHT / 3 + (BUTTON_HEIGHT / 2 + margin) * 2)

however all I am getting is

' x '

however, just above it

local fscreen = love.window.getFullscreen()

love.graphics.print(tostring(fscreen), WINDOW_WIDTH / 3 + BUTTON_WIDTH / 2 + margin, WINDOW_HEIGHT / 3 + (BUTTON_HEIGHT / 2 + margin))

works just fine.

Printing normal text to the screen works just fine as well. It seems to be just variables.
If I start a clean project these work just fine.


If someone has ran into the same troubles, please point me in the direction of fixing it.

Re: love.graphics.print troubles

Posted: Wed Nov 18, 2020 12:15 pm
by sphyrth
Are you doing something else to "resolutionw" and/or "resolutionh" in-between their declarations and their print function?

Re: love.graphics.print troubles

Posted: Wed Nov 18, 2020 1:25 pm
by norubal
Try this

Code: Select all

resolutionw = love.graphics.getWidth()
resolutionh = love.graphics.getHeight()
Instead of this.

Code: Select all

local resolutionw = love.graphics.getWidth()
local resolutionh = love.graphics.getHeight()
http://lua-users.org/wiki/ScopeTutorial

Re: love.graphics.print troubles

Posted: Wed Nov 18, 2020 4:40 pm
by pgimeno
VonMan1aC wrote: Wed Nov 18, 2020 7:39 am and am trying to print it to screen with

love.graphics.print(tostring(resolutionw)..' x '..tostring(resolutionh), WINDOW_WIDTH / 3 + BUTTON_WIDTH / 2 + margin, WINDOW_HEIGHT / 3 + (BUTTON_HEIGHT / 2 + margin) * 2)

however all I am getting is

' x '
That's very strange. The only way of obtaining that result is if resolutionw and resolutionh are both empty strings, "". If they were unassigned, you would get something like 'nil x nil' instead.

Is it possible that you're initializing them to the empty string somewhere? Where are you setting resolutionw and resolutionh? Could you provide some more context for that code?

The best explanation for that behaviour is that you're doing something like this:

Code: Select all

local resolutionw = ""
local resolutionh = ""
...
function love.load()
  ...
  local resolutionw = love.graphics.getWidth()
  local resolutionh = love.graphics.getHeight()
  ...
end

function love.draw()
  ...
  love.graphics.print(tostring(resolutionw) .. " x " .. tostring(resolutionh), ...)
  ...
end
If that's the case, the 'local' declarations in love.load create new variables that are local to love.load, and disappear when love.load ends, therefore their value is not visible outside love.load. If that's the case, the solution is what norubal said, because that way you would be assigning the previous locals instead of creating new ones.

If not, we'll need more context in order to understand what's going on.

Re: love.graphics.print troubles

Posted: Thu Nov 19, 2020 1:08 pm
by VonMan1aC
Thank you all for the replies.
You guys are awesome.

I found the problem, and it has nothing to do with love.graphics.print failing at all.

I have a custom font setup in main.lua for all my printing needs and from all the ones I downloaded and checked as possibilities, I had to go and choose the one that had no numbers indicated in the .ttf.

Just my damn luck. Oh well, lessons learned I guess. Always check your fonts.