"Variable nil" error

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
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

"Variable nil" error

Post by Refpeuk »

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?
Attachments
spacerpg.love
(40.09 KiB) Downloaded 62 times
It was the best of times, it was the worst of times . . .
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: "Variable nil" error

Post by kraftman »

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
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: "Variable nil" error

Post by Refpeuk »

Oh, I see. I thought it worked the same was as assigning multiple variables. Eg:

Code: Select all

local wmx,wmy = mouseworld(love.mouse.getPosition())
Thanks for that!
It was the best of times, it was the worst of times . . .
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: "Variable nil" error

Post by kraftman »

Refpeuk wrote:Oh, I see. I thought it worked the same was as assigning multiple variables. Eg:

Code: Select all

local wmx,wmy = mouseworld(love.mouse.getPosition())
Thanks for that!
no problem, to be honest ive never tried doing something like that so im not sure how it does work. Oh well!
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: "Variable nil" error

Post by Refpeuk »

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 . . .
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: "Variable nil" error

Post by Robin »

So, what is going on here?

As you know, in Lua, functions can return multiple values

Code: Select all

function t1()
   return 1, 2, 3
end
And you can pass them to other functions:

Code: Select all

function t2(a, b, c)
   return a + b + c
end

print(t2(t1())) -- prints 6
However, that only works if no other arguments follow it. So:

Code: Select all

print(t2(t1(), 10, 20)) -- prints 31
Why does it do that?

If other arguments follow the function call, it only uses the first return value:

Code: Select all

print(t2(1, 10, 20))
So in your case, you call

Code: Select all

character:rotate(mouseworld(love.mouse.getPosition()), characterdat[num].shape:center())
which means the arguments to character:rotate() become:

Code: Select all

local wmx = mouseworld(love.mouse.getPosition()) -- only the first return value
local wmy, px = characterdat[num].shape:center()
local py = nil
Does that make it clear?
Help us help you: attach a .love.
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: "Variable nil" error

Post by Refpeuk »

It does, thanks!
It was the best of times, it was the worst of times . . .
Post Reply

Who is online

Users browsing this forum: Semrush [Bot], zorg and 6 guests