attempt to perform arithmitic on field 'mousex'(a nil value)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
attempt to perform arithmitic on field 'mousex'(a nil value)
I got the code for this game to look and help me build a bullet hell. Unfortunately it was for an old version, so I fixed it in a few places. Then I decided that my code stinks and that I was just going to modify that one instead. Anyway in the section that's failing I'm trying to get it to get the distance between the mouse and the player, but it doesn't want me to do that. That code is decently long so I think I will post the love instead. Sorry for the wall of text.
- Attachments
-
- bulletHell.love
- (5.5 MiB) Downloaded 75 times
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: attempt to perform arithmitic on field 'mousex'(a nil value)
That means you don't have a mousex. Make sure you don't have a scope issue. Also check that mousex and most likely mousey are even declared. If they are not you can use love.mouse.getPosition. Unless it's in the callback. If it's in the mousepressed and mouse released callbacks just make sure that the arg names match the variable you use.
For example your issue could be this
For example your issue could be this
Code: Select all
function love.mousepressed(button, x,y)
Banana = mousex/2 --the arg name and mousex don't match. I should use x instead in this case.
end
mousex,mousey = love.mouse.getPosition() --and this is something else you can try.
Re: attempt to perform arithmitic on field 'mousex'(a nil value)
But the function just above (That was already there) worked fine:
Code: Select all
function Player:updateMouse(mousex, mousey) -- That works
self.mousex = mousex
self.mousey = mousey
Player:updateAim()
end
function Player:updateAim() -- But this has issues
--debug.debug()
self.aimdistance = ((self.mousex-self.x)^2+(self.mousey-self.y)^2)^0.5 --attempt to perform arithmitic on field 'mousex'(a nil value)
self.angle = math.atan2((self.mousey - self.y), (self.mousex - self.x))
end
Re: attempt to perform arithmitic on field 'mousex'(a nil value)
The Visibility Rules section of the reference manual explains lexical scope in Lua. Function parameters act as local variables; they are block-scoped (see Function Definitions).
Re: attempt to perform arithmitic on field 'mousex'(a nil value)
the issue is that you are doing Player:updateAim() when you want to be doing self:updateAim in Player:updateMouse. Otherwise in Player:updateAim, self refers to 'Player' which is your Class, not your Object so the .mousex and .mousey aren't set there.ETShift wrote:But the function just above (That was already there) worked fine:
Code: Select all
function Player:updateMouse(mousex, mousey) -- That works self.mousex = mousex self.mousey = mousey Player:updateAim() end function Player:updateAim() -- But this has issues --debug.debug() self.aimdistance = ((self.mousex-self.x)^2+(self.mousey-self.y)^2)^0.5 --attempt to perform arithmitic on field 'mousex'(a nil value) self.angle = math.atan2((self.mousey - self.y), (self.mousex - self.x)) end
Re: attempt to perform arithmitic on field 'mousex'(a nil value)
I'm not doing anything with the updateMouse's mousex and mousey, I'm trying to access self.mousex and self.mousey, which are defined in the Player class.airstruck wrote:The Visibility Rules section of the reference manual explains lexical scope in Lua. Function parameters act as local variables; they are block-scoped (see Function Definitions).
Re: attempt to perform arithmitic on field 'mousex'(a nil value)
Ah, thanks. It works now. Or rather, for now.s-ol wrote:the issue is that you are doing Player:updateAim() when you want to be doing self:updateAim in Player:updateMouse. Otherwise in Player:updateAim, self refers to 'Player' which is your Class, not your Object so the .mousex and .mousey aren't set there.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests