## attempt to perform arithmetic on field 'y' (a nil value)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
goldenpotato
Prole
Posts: 3
Joined: Mon Jan 25, 2021 3:39 pm

### attempt to perform arithmetic on field 'y' (a nil value)

I'm not sure if this is the right forum to post this but I recently got this issue when making a "collides" function in a game I'm working on. Before this function every call to "self.y" works just fine but the moment I enable this function, love gives me the following error:
************************
Error

RCar.lua:29: attempt to perform arithmetic on field 'y' (a nil value)

Traceback

RCar.lua:29: in function 'collides'
RCar.lua:99: in function 'update'
states/PlayState.lua:13: in function 'update'
StateMachine.lua:59: in function 'update'
main.lua:90: in function 'update'
[C]: in function 'xpcall'
*************************

This is really slowing down my progression on this project so I would love any help I could get.
Here's my code, the issue is in the collides function:

Code: Select all

RCar = Class{}

local count = 0
local groundHeight = 0

function RCar:init()
self.image = love.graphics.newImage('red_car.png')

groundHeight = VIRTUAL_HEIGHT - 37

self.x = 120
self.y = groundHeight

self.width = self.image:getWidth()
self.height = self.image:getHeight()

self.dx = 0
self.dy = 0

self.timer = 0

MOVEMENT_SPEED = 100

GRAVITY = 10
end

function RCar:collides()
if BallY >= self.y + 3 and self.x <= BallX and BallX <= self.x + 3 then --Back trunk hit
BallDy = -5
elseif BallX >= self.x + 3 and self.y + 3 <= BallY <= self.y then --Back window hit
BallDx = -BallDx - 2
elseif BallY <= self.y and self.x + 3 <= BallX <= self.x + 11 then --top of the car
BallDy = -5
elseif BallX <= self.x + 11 and self.y <= BallY <= self.y + 3 then --front window
BallDx = -BallDx + 2
elseif BallY >= self.y + 3 and self.x + 11 <= BallX <= self.x + 16 then --hood
BallDy = -5
elseif BallX <= self.x + 16 and self.y + 3 <= BallY <= self.y + 7 then --bumper
BallDx = -BallDx + 2
elseif BallY <= self.y + 7 and self.x <= BallX <= self.x + 16 then --bottom
BallDy = -BallDy
if BallDx < 0 then
BallDx = -BallDx + 2
elseif BallDx > 0 then
BallDx = BallDx + 2
end
elseif BallX > self.x and self.y + 3 <= BallY <= self.y + 7 then --back bumper
BallDx = -BallDx - 2
end
end

function RCar:update(dt)

if self.y < groundHeight then
self.dy = self.dy + GRAVITY * dt
else
self.dy = 0
end

if love.keyboard.isDown('d') then
self.dx = MOVEMENT_SPEED
self.x = self.x + self.dx * dt
end

if love.keyboard.isDown('a') then
self.dx = -MOVEMENT_SPEED
self.x = self.x + self.dx * dt
end

if love.keyboard.wasPressed('w') and count == 0 then
count = 1
self.dy = -2
end

if count == 1 then
if love.keyboard.isDown('w') then
self.dy = -2
self.timer = self.timer + dt
if self.timer > .5 then
count = 2
self.timer = 0
end
end
end

if self.y >= groundHeight then
self.y = groundHeight
count = 0
end

self.y = self.y + self.dy
RCar:collides()

end

function RCar:render()
love.graphics.draw(self.image, self.x, self.y, 0, 3, 3)
end

sphyrth
Party member
Posts: 258
Joined: Mon Jul 07, 2014 11:04 am
Contact:

### Re: attempt to perform arithmetic on field 'y' (a nil value)

I can't really see what's wrong in your code as well.

All I can think of is that you didn't call RCar:init() before using RCar:update() or something.
goldenpotato
Prole
Posts: 3
Joined: Mon Jan 25, 2021 3:39 pm

### Re: attempt to perform arithmetic on field 'y' (a nil value)

sphyrth wrote: Wed Jan 27, 2021 10:17 pm I can't really see what's wrong in your code as well.

All I can think of is that you didn't call RCar:init() before using RCar:update() or something.

This is my code for where I initialized all the Classes.

Code: Select all

PlayState = Class{__includes = BaseState}

function PlayState:init()
self.redCar = RCar()
self.blueCar = BCar()
self.ball = Ball()
self.goalr = GoalR()

end

function PlayState:update(dt)
self.redCar:update(dt)
self.blueCar:update(dt)
self.ball:update(dt)
self.goalr:update(dt)
end

function PlayState:render()
self.redCar:render()
self.blueCar:render()
self.ball:render()
self.goalr:render()
end

Xugro
Citizen
Posts: 90
Joined: Wed Sep 29, 2010 8:14 pm

### Re: attempt to perform arithmetic on field 'y' (a nil value)

Please upload a .love of your game. Then we can take a look at what is going on here. Without seeing how the different classes interact we can only guess what the problem may be.
goldenpotato
Prole
Posts: 3
Joined: Mon Jan 25, 2021 3:39 pm

### Re: attempt to perform arithmetic on field 'y' (a nil value)

Xugro wrote: Wed Jan 27, 2021 11:19 pm Please upload a .love of your game. Then we can take a look at what is going on here. Without seeing how the different classes interact we can only guess what the problem may be.
I'm unsure of how to do that. I'm pretty new to love2D and I'm still exploring many of its features.
MrFariator
Party member
Posts: 243
Joined: Wed Oct 05, 2016 11:53 am

### Re: attempt to perform arithmetic on field 'y' (a nil value)

The problem is at the end of your RCar class' update method:

Code: Select all

function RCar:update(dt)
-- ...rest of code
RCar:collides()
end
Instead calling the function with an instance of the class (eq. writing "self:collides()"), you are passing the class itself to the function. With lua's syntax, what you have written is syntactically equivalent to:

Code: Select all

RCar.collides(RCar) -- the RCar class itself becomes 'self' in the collides() function
But because the RCar class' table has no member "y" (or "x", for that matter), you're effectively trying to perform arithmetic on non-existent values, causing your crash. What you probably meant to write instead is:

Code: Select all

function RCar:update(dt)
-- ...rest of code
self:collides()
end

### Who is online

Users browsing this forum: No registered users and 22 guests