Page 1 of 1

"loop in gettable" when I add 100 objects to an array

Posted: Tue Oct 21, 2014 2:25 am
by xaa3
Whenever I add 100 instances of my class to an array it breaks when calling love.draw() when it hits 100 bullet objects added. I am removing the bullets as they leave the screen so the array size is not hitting close to 100 now but even still i get the bug.

Error: functions.lua:24: loop in gettable
stack traceback:
functions.lua:24: in function 'drawObjects'
main.lua:62: in function 'draw'
[string "boot.lua"]:438: in function <[string "boot.lua"]:399>
[C]: in function 'xpcall'


Here is my class:

Code: Select all

bullet = {}
 
-- CONSTRUCTOR
function bullet:new(playerx, playery, xd, yd)
   local object = {
      x = playerx,
      y = playery,
      xd = xd,
      yd = yd,
      red = 255,
      redUp = false,
      deleteMe = false
   }
   setmetatable(object, { __index = bullet })
   return object
end


function bullet:update(dt)
	self.x = self.x + self.xd*dt
   self.y = self.y + self.yd*dt
   
   if self.redUp then
      self.red = self.red + 20
		if self.red > 230 then self.red = 230; self.redUp = false end
   else
      self.red = self.red - 20
		if self.red < 100 then self.red = 100; self.redUp = true end
   end
   
   -- CHECK BOUNDS
   if self.x < -100 or self.x > 1380 or self.y < -100 or self.y > 820 then self.deleteMe = true end
end


function bullet:draw()
   g.setColor(self.red, 100, 100, 255)
	love.graphics.circle("fill", self.x, self.y, 7, 100)
   g.setColor(255, 255, 255, 255)
end
And here is my draw function:

Code: Select all

function drawObjects(obj)
   for i=1, table.getn(obj) do
      if obj[i] then
         obj[i]:draw()
      end
   end
end
The array's name is "bullets" and so I'm using this:

Code: Select all

drawObjects(bullets)
I have made other games using this method and have put waaaay more than 100 objects into an array without any problems. Is this a problem specific to LOVE2D 0.9.1??

Any help would be greatly appreciated.

The love file included only works with an XBOX360 pad. The sticks move and the triggers fire. :o:

Re: "loop in gettable" when I add 100 objects to an array

Posted: Tue Oct 21, 2014 5:51 am
by Plu
Unfortunately I can't get the game to run due to no joysticks, but I spotted one thing that looks supsicious in your load function:

Code: Select all

 bullet = bullet:new(100, -200, 0, 300); table.insert(bullets, bullet)
You're making a variable that has the same name as your library here, and then you're setting a reference to that variable as it's metatable inside of it. Probably not what you want, but might be causing your problems.

I don't think this is a problem with the length of your loop at any rate; it's probably a problem of infinite recursion due to metatable checking somewhere.

Re: "loop in gettable" when I add 100 objects to an array

Posted: Tue Oct 21, 2014 7:01 am
by caldur
plu is right, it seems to me that your problem roots in the way of importing script files
the common practice would be:

Code: Select all

--bullet.lua
local bullet = {}
    --all the functions here
return bullet

--main.lua, or any other files using bullets
local Bullet = require("bullet")
   --blah blah
   b = Bullet.new(...)
this way not only can you avoid problems of mixing ref to libs with ref to objects, but it also makes dependencies among files very clear.

Re: "loop in gettable" when I add 100 objects to an array

Posted: Wed Oct 22, 2014 9:20 pm
by xaa3
Ok I fixed it. Changed the "bullet" to something different when making making a new instance.

Code: Select all

p1bullet= bullet:new(100, -200, 0, 300); table.insert(bullets, p1bullet)
Also removed that line from my load function (must have been there for testing).
I'll start using the method of importing scripts you suggested caldur.

Works great now I can create total bullet hell with no error! Thanks guys. :awesome:

Re: "loop in gettable" when I add 100 objects to an array

Posted: Wed Mar 23, 2022 10:57 pm
by Popolon
For information, I just had the same error message with LÖVE 11.4, when trying to instanciate a table element with a typo on the initialisation of the table like this (fo instead of foo here):

Code: Select all

fo={} -- o missing (as example)
for i=1,#elt do
  foo[i]=elt.x
end