attempt to perform arithmitic on field 'mousex'(a nil value)

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
ETShift
Prole
Posts: 4
Joined: Tue Apr 26, 2016 3:05 pm

attempt to perform arithmitic on field 'mousex'(a nil value)

Post 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. :P
Attachments
bulletHell.love
(5.5 MiB) Downloaded 75 times
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: attempt to perform arithmitic on field 'mousex'(a nil value)

Post 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. 
ETShift
Prole
Posts: 4
Joined: Tue Apr 26, 2016 3:05 pm

Re: attempt to perform arithmitic on field 'mousex'(a nil value)

Post 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
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: attempt to perform arithmitic on field 'mousex'(a nil value)

Post 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).
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: attempt to perform arithmitic on field 'mousex'(a nil value)

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
ETShift
Prole
Posts: 4
Joined: Tue Apr 26, 2016 3:05 pm

Re: attempt to perform arithmitic on field 'mousex'(a nil value)

Post 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.
ETShift
Prole
Posts: 4
Joined: Tue Apr 26, 2016 3:05 pm

Re: attempt to perform arithmitic on field 'mousex'(a nil value)

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests