Issue with emulating OOP

Hey. I normally wouldn’t post about syntax issues, but I’m stumped as to why this isn’t working.

function starManager:checkBounds(i) &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((self.smallStarList[i]).x \< -1080) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.smallStarList[i].x = self.smallStarList[i].x + 1080\*2; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((self.smallStarList[i]).x \> 1080) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.smallStarList[i].x = self.smallStarList[i].x - 1080\*2; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((self.smallStarList[i]).y \< 0) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.smallStarList[i].y = self.smallStarList[i].y - 1080\*2; &nbsp; &nbsp; &nbsp; &nbsp; end end function starManager:step() &nbsp; &nbsp; for i=1,6 do &nbsp; &nbsp; &nbsp; &nbsp; self.smallStarList[i]:translate(5,5); &nbsp; &nbsp; &nbsp; &nbsp; self.bigStarList[i]:translate(5,5); &nbsp; &nbsp; end &nbsp; &nbsp; self:checkBounds(i); end

Step is called in another method. It translates the stars just fine, but crashes when I call “self:checkBounds()”.

\Objects\starManager.lua:41: attempt to index field '?' (a nil value)

Lua is not my most proficient language, but I don’t see why I’m having trouble calling an object’s function inside itself. If I replace checkBounds() with a print statement, it outputs properly, so it’d being called; It’s just not getting the regular “self” variable.

Thanks.

Edit: To clarify, the checkBounds() function works if I shove it into step(), but that is obviously not desirable.

First thought: you are using the variable i outside the for loop in " self:checkBound(i) " so it’s normal that it is seen as a nil value.

Wouldn’t it pass it by value? I considered that and substituted another variable, but no dice.

  1. No, the i variable stops existing after the for loop.

  2. Does the object that is “self” in this case even HAVE the function checkBounds??? Are you using a metatable of sorts to make sure that objects instanced from starManager have the same functions?

  1. I see, but I don’t see why that makes me unable to pass the value of i to a function

  2. It definitely has checkBounds, I’ve currently settled on having my game loop call it

You can pass the value of i to a function, but in the place you use it the value of i is nil. So it might report an error due to this.

Secondly, please be specific: are you using metatables?

Oh, I see. I seem to have been so focused on that one part that I couldn’t see the forest for the trees.This is why I usually don’t post about syntax issues, but I was worried there was a fundamental flaw in my understanding of lua.

I appreciate the help.

First thought: you are using the variable i outside the for loop in " self:checkBound(i) " so it’s normal that it is seen as a nil value.

Wouldn’t it pass it by value? I considered that and substituted another variable, but no dice.

  1. No, the i variable stops existing after the for loop.

  2. Does the object that is “self” in this case even HAVE the function checkBounds??? Are you using a metatable of sorts to make sure that objects instanced from starManager have the same functions?

  1. I see, but I don’t see why that makes me unable to pass the value of i to a function

  2. It definitely has checkBounds, I’ve currently settled on having my game loop call it

You can pass the value of i to a function, but in the place you use it the value of i is nil. So it might report an error due to this.

Secondly, please be specific: are you using metatables?

Oh, I see. I seem to have been so focused on that one part that I couldn’t see the forest for the trees.This is why I usually don’t post about syntax issues, but I was worried there was a fundamental flaw in my understanding of lua.

I appreciate the help.