love.graphics.print troubles

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
VonMan1aC
Prole
Posts: 2
Joined: Wed Nov 18, 2020 7:16 am

love.graphics.print troubles

Post 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.
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: love.graphics.print troubles

Post by sphyrth »

Are you doing something else to "resolutionw" and/or "resolutionh" in-between their declarations and their print function?
User avatar
norubal
Party member
Posts: 137
Joined: Tue Jan 15, 2013 5:55 am

Re: love.graphics.print troubles

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

Re: love.graphics.print troubles

Post 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.
VonMan1aC
Prole
Posts: 2
Joined: Wed Nov 18, 2020 7:16 am

Re: love.graphics.print troubles

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 3 guests