How do I print a variable?

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
idoblenderstuffs
Prole
Posts: 3
Joined: Sun Nov 13, 2022 7:10 pm

How do I print a variable?

Post by idoblenderstuffs »

Sorry for stupid, I basically start learning LOVE about 10 minutes ago and was very surprised with how nice this engine is, although now I cannot find any solution on the internet telling me how to print a variable.

My code looks like this:

function love.load()
local px = 200
end

function love.update(dt)
px = love.mouse.getX
end

function love.draw()
love.graphics.print(px, 0, 0)
end

In other engines, usually I can just set a variable and then print it. But I get an error saying "String expected, got function".
I don't even know what it means by a function variable.

How can I set a value that I can print?
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: How do I print a variable?

Post by GVovkiv »

idoblenderstuffs wrote: Sun Nov 13, 2022 7:14 pm Sorry for stupid, I basically start learning LOVE about 10 minutes ago and was very surprised with how nice this engine is, although now I cannot find any solution on the internet telling me how to print a variable.

My code looks like this:

function love.load()
local px = 200
end

function love.update(dt)
px = love.mouse.getX
end

function love.draw()
love.graphics.print(px, 0, 0)
end

In other engines, usually I can just set a variable and then print it. But I get an error saying "String expected, got function".
I don't even know what it means by a function variable.

How can I set a value that I can print?
as error says: you passed function to print, while expected string, because with

Code: Select all

px = love.mouse.getX
you assigned function

Code: Select all

love.mouse.getX
to px variable, while you need to get data from function and pass to print, as

Code: Select all

px = love.mouse.getX()
In short:

Code: Select all

a = someFunction() -- call function (and maybe get value from it)
a = someFunction -- assign "someFunction" to variable "a"
KalevTait
Prole
Posts: 7
Joined: Sun Aug 19, 2018 10:28 am

Re: How do I print a variable?

Post by KalevTait »

Two issues, one which is causing your code to break, and one which is just going to cause your code to act unexpectedly.

The first is that instead of:
px = love.mouse.getX
you want to use:
px = love.mouse.getX()
The former is making px into a pointer to the getX function (so you could later call px() instead of love.mouse.getX()), while the later is assigning the result of getX() to px (which is what you want)

The second issue is that your very first assignment to px is to a different px than the one you are using in the rest of your code. The 'local' before your assignment means that the px created in love.load is deleted when it goes out of scope (when the function exits). You are then creating a new global variable called px in love.update() - in the case of your code it doesn't matter, but if you were conditionally setting px then you could have times when the global px was never assigned to by the time you used it in love.draw(). Basically, any time you want a variable to be used outside the function in which you initialise it, don't use local.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How do I print a variable?

Post by zorg »

KalevTait wrote: Sun Nov 13, 2022 9:52 pm Basically, any time you want a variable to be used outside the function in which you initialise it, don't use local.
Better advice is to always use locals, and know how to scope said local variables; in this case, make the variable local to the file, by writing "local px" above all functions that would use them (or at the top of the file, if you like K&R C style...)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests