Page 1 of 1
attempt to perform arithmitic on field 'mousex'(a nil value)
Posted: Tue Apr 26, 2016 3:56 pm
by ETShift
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.
Re: attempt to perform arithmitic on field 'mousex'(a nil value)
Posted: Wed Apr 27, 2016 2:01 am
by bobbyjones
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
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)
Posted: Wed Apr 27, 2016 11:12 pm
by ETShift
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)
Posted: Wed Apr 27, 2016 11:25 pm
by airstruck
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)
Posted: Thu Apr 28, 2016 5:39 pm
by s-ol
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
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.
Re: attempt to perform arithmitic on field 'mousex'(a nil value)
Posted: Thu Apr 28, 2016 5:42 pm
by ETShift
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).
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.
Re: attempt to perform arithmitic on field 'mousex'(a nil value)
Posted: Thu Apr 28, 2016 5:46 pm
by ETShift
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.
Ah, thanks. It works now. Or rather, for now.