1

I get the following error when I attempt to use call a function in a box collider object I'm trying to make.

The Error:

main.lua:26: attempt to call field 'Update' ( a nil value )

Traceback

main.lua:26: in function 'update' [C]: in function 'xpcall'

The Code:

main.lua

require("BoxCollider")

local s1 = {}
s1.x = 10
s1.y = 10
s1.w = 10
s1.h = 10
s1.collider = BoxCollider.new(s1.x,s1.y,s1.w,s1.h)

test = BoxCollider.new(40, 7,15,15)

function love.load()

end

function love.update(dt)

if love.keyboard.isDown("d") then s1.x = s1.x + 100 * dt end    
---[[   
s1.collider:Update(s1.x,s1.y)

if s1.collider:CheckCollisions(test) then
    s1.x = 10
end
--]]
end

function love.draw()

love.graphics.rectangle("fill", s1.x, s1.y, s1.w, s1.h)
love.graphics.rectangle("fill", 40, 7, 15,15)

end

BoxCollider.lua

BoxCollider = {}
BoxCollider._index = BoxCollider


--x is the x point of the collider, should be equal to the object's x point if the box        is for an object
--y is the y point of the collider, should be equal to the object's x point if the box is for an object
--w is the width of the box
--h is the height of the box

function BoxCollider.new(x,y,w,h)
return setmetatable({x = x or 0, y = y or 0, w = w or 0, h = h or 0},BoxCollider)
end

function BoxCollider:CheckCollisions( v )

return self.x < (v.x + v.h) and  v.x < (self.x + self.w) and self.y  < (v.y+v.h) and v.y < (self.y+self.h)  

end

function BoxCollider:reSize(nw,nh)
self.w = nw
self.h = nh

end

function BoxCollider:Update(nx,ny)
self.x = nx
self.y = ny
end
2
  • 1
    Change s1.collider.Update to s1.collider:Update
    – hjpotter92
    Commented Mar 9, 2014 at 12:20
  • Thanks for answering, tried that, but it didn't work. I changed the code in the post though, because it should have been like that in the first place.
    – Feralspeed
    Commented Mar 9, 2014 at 20:43

1 Answer 1

0

I think you just forgot an extra underscore in the BoxCollider.__index. With only one underscore, ._index, the BoxCollider methods are not being searched when :Update is called and it isn't found.

1
  • I'm pretty sure this is correct answer, so it should be marked as such.
    – Marko
    Commented Jan 11, 2018 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.