Hey Guys,
I've been staring at my code, running it through over and over, and still can't figure out why it thinks one of my local variables is nil: I assign it a value directly before I use it.
I've included the .love that crashes - the error shows exactly which variable, where, and the chain of functions that ends up calling it. (It's in the character class in the rotate function)
I'd appreciate any help; I don't know if I'm just missing something crazy obvious?
"Variable nil" error
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
"Variable nil" error
- Attachments
-
- spacerpg.love
- (40.09 KiB) Downloaded 62 times
It was the best of times, it was the worst of times . . .
Re: "Variable nil" error
You can't pass over the return values of functions like that, so as far as character:rotate is concerned it is only receiving wmy and wmy, which are actually the x values of the mouse and character:center functions
- Attachments
-
- spacerpg.love
- (85.25 KiB) Downloaded 61 times
Re: "Variable nil" error
Oh, I see. I thought it worked the same was as assigning multiple variables. Eg:
Thanks for that!
Code: Select all
local wmx,wmy = mouseworld(love.mouse.getPosition())
It was the best of times, it was the worst of times . . .
Re: "Variable nil" error
no problem, to be honest ive never tried doing something like that so im not sure how it does work. Oh well!Refpeuk wrote:Oh, I see. I thought it worked the same was as assigning multiple variables. Eg:Thanks for that!Code: Select all
local wmx,wmy = mouseworld(love.mouse.getPosition())
Re: "Variable nil" error
Oh right, I also meant to ask - if it was only recognizing two variables, why did it throw the error at py instead of px, which came first?
It was the best of times, it was the worst of times . . .
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: "Variable nil" error
So, what is going on here?
As you know, in Lua, functions can return multiple values
And you can pass them to other functions:
However, that only works if no other arguments follow it. So:
Why does it do that?
If other arguments follow the function call, it only uses the first return value:
So in your case, you call
which means the arguments to character:rotate() become:
Does that make it clear?
As you know, in Lua, functions can return multiple values
Code: Select all
function t1()
return 1, 2, 3
end
Code: Select all
function t2(a, b, c)
return a + b + c
end
print(t2(t1())) -- prints 6
Code: Select all
print(t2(t1(), 10, 20)) -- prints 31
If other arguments follow the function call, it only uses the first return value:
Code: Select all
print(t2(1, 10, 20))
Code: Select all
character:rotate(mouseworld(love.mouse.getPosition()), characterdat[num].shape:center())
Code: Select all
local wmx = mouseworld(love.mouse.getPosition()) -- only the first return value
local wmy, px = characterdat[num].shape:center()
local py = nil
Help us help you: attach a .love.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests