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

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
User avatar
xaa3
Prole
Posts: 14
Joined: Sat Aug 31, 2013 12:36 am

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

Post 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:
Attachments
circlebattle.love
(4.24 KiB) Downloaded 190 times
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

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

Post 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.
caldur
Prole
Posts: 20
Joined: Tue Mar 13, 2012 3:30 pm

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

Post 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.
User avatar
xaa3
Prole
Posts: 14
Joined: Sat Aug 31, 2013 12:36 am

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

Post 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:
User avatar
Popolon
Prole
Posts: 25
Joined: Mon Nov 07, 2016 11:03 am
Location: France/Paris

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

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

Who is online

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