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?
How do I print a variable?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: How do I print a variable?
as error says: you passed function to print, while expected string, because withidoblenderstuffs 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?
Code: Select all
px = love.mouse.getX
Code: Select all
love.mouse.getX
Code: Select all
px = love.mouse.getX()
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?
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:
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.
The first is that instead of:
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)px = love.mouse.getX()
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.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: How do I print a variable?
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 True 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.
Who is online
Users browsing this forum: No registered users and 2 guests