Page 1 of 1
How do I print a variable?
Posted: Sun Nov 13, 2022 7:14 pm
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?
Re: How do I print a variable?
Posted: Sun Nov 13, 2022 8:53 pm
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
you assigned function
to px variable, while you need to get data from function and pass to print, as
In short:
Code: Select all
a = someFunction() -- call function (and maybe get value from it)
a = someFunction -- assign "someFunction" to variable "a"
Re: How do I print a variable?
Posted: Sun Nov 13, 2022 9:52 pm
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.
Re: How do I print a variable?
Posted: Mon Nov 14, 2022 5:59 am
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...)