# A Question about tables

**URL:** <https://forums.solar2d.com/t/a-question-about-tables/311348>\
**Category:** Uncategorized\
**Created:** [March 11, 2013, 11:02am UTC](https://forums.solar2d.com/t/a-question-about-tables/311348 "2013-03-11T11:02:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Appletreeman](https://forums.solar2d.com/user_avatar/forums.solar2d.com/appletreeman/32/130_2.png) [@Appletreeman](https://forums.solar2d.com/u/Appletreeman)\
**Post date:** [March 11, 2013, 11:02am UTC](https://forums.solar2d.com/t/a-question-about-tables/311348/1 "2013-03-11T11:02:04Z")

</div>

OK, so pretty much all the tutorials I’ve read have said that when writing a game it makes sense to add the enemies/bullets/whatever into a table so that they can be easily referenced. However, what I’m stuck on is the best way to remove the entities from the table once they have been killed.

This is the code that I have for the bombs so far…  
(A new bomb is generated every three quarters of a second)  
(bmbText is just a text field I put in to easily keep track of how many bombs are in the table)

[lua]  
local bombs={}

function onLocalCollision(self, event)  
if self.name==“bomb” and event.other.name==“ground” then  
print (“BANG!!”)  
local dead=table.indexOf(bombs, self)  
bombs[dead]:removeSelf()  
–bombs[dead]=nil  
bmbText.text="BOMBS: " … #bombs  
end  
end  
function dropBomb()  
local foo=\_m(1,#enemies)  
local nb= #bombs+1  
bombs[nb]=display.newCircle(bullets,0,0,2)  
bombs[nb].x=enemies[foo].x; bombs[nb].y=enemies[foo].y  
bombs[nb]:setFillColor(0,255,255)  
physics.addBody(bombs[nb], “dynamic”, {bounce=0, density=.25})  
bombs[nb].name=“bomb”  
bombs[nb].collision=onLocalCollision  
bombs[nb]:addEventListener(“collision”, bombs[nb])  
bmbText.text="BOMBS: " … #bombs  
end  
[/lua]

As it stands at the moment the bombs drop down and disappear when they hit the ground as they should but they are not being removed from the table correctly as #bombs indicates.  
However, if I uncomment the [lua]–bombs[dead]=nil[/lua] line then it seems to work most of the time but every so often one of the bombs is not removed from either the table or the display.

I’m sure my code isn’t very efficient and there’s probably a much better way of doing this so I would be eternally grateful if anyone could point me in the right direction.

Cheers  
Chris

[import]uid: 7841 topic\_id: 37468 reply\_id: 67468[/import]

---

<div class="post-metadata">

**Author:** ![richard9](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@richard9](https://forums.solar2d.com/u/richard9)\
**Post date:** [March 11, 2013, 5:30pm UTC](https://forums.solar2d.com/t/a-question-about-tables/311348/2 "2013-03-11T17:30:58Z")

</div>

What happens if you try `table.remove()` instead of `:removeSelf()`?

Also, not really a physics guy but would you have to disable/remove physics on the object first? [import]uid: 41884 topic\_id: 37468 reply\_id: 145670[/import]

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [March 11, 2013, 11:30pm UTC](https://forums.solar2d.com/t/a-question-about-tables/311348/3 "2013-03-11T23:30:59Z")

</div>

When you have a numerically indexed table (acting like an array) and you make one of the elements nil, then it causes using the #table method of getting the table length to end at the nil:

```auto
myTable = {}  
myTable[1] = 'one'  
myTable[2] = 'two'  
myTable[3] = 'three'  
myTable[4] = 'four'  
myTable[5] = 'five'  
  
print(#myTable) -- output's 5  
  
myTable[3] = nil  
  
print(#myTable) -- output's 2  
print(table.maxn(myTable)) -- output's 5  
```

But if you do a:

```auto
myTable[3] = nil  
table.remove(myTable, 3)  
print(#myTable) -- output's 4  
```

The only thing I’m not sure about is how that impact’s garbage collection by removing the table row so quickly. But regardless, the object needs removed using display.remove() or :removeSelf()

[import]uid: 199310 topic\_id: 37468 reply\_id: 145706[/import]

---

<div class="post-metadata">

**Author:** ![Appletreeman](https://forums.solar2d.com/user_avatar/forums.solar2d.com/appletreeman/32/130_2.png) [@Appletreeman](https://forums.solar2d.com/u/Appletreeman)\
**Post date:** [March 12, 2013, 12:56pm UTC](https://forums.solar2d.com/t/a-question-about-tables/311348/4 "2013-03-12T12:56:15Z")

</div>

Cheers guys,  
That’s now working a lot better, but every so often one of the bombs will stay on the ground and not be removed. When this happens I get the following error in the terminal:  
[text]

Runtime error  
/Users/home/Desktop/Projects/vader/main.lua:70: attempt to index field ‘?’ (a nil value)  
stack traceback:  
[C]: ?  
/Users/home/Desktop/Projects/vader/main.lua:70: in function  
?: in function \<?:229\>  
[/text]  
Line 70 is the one that reads [lua]local dead=table.indexOf(bombs, self)[/lua]  
[import]uid: 7841 topic\_id: 37468 reply\_id: 145767[/import]

---

<div class="post-metadata">

**Author:** ![richard9](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@richard9](https://forums.solar2d.com/u/richard9)\
**Post date:** [March 11, 2013, 5:30pm UTC](https://forums.solar2d.com/t/a-question-about-tables/311348/5 "2013-03-11T17:30:58Z")

</div>

What happens if you try `table.remove()` instead of `:removeSelf()`?

Also, not really a physics guy but would you have to disable/remove physics on the object first? [import]uid: 41884 topic\_id: 37468 reply\_id: 145670[/import]

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [March 11, 2013, 11:30pm UTC](https://forums.solar2d.com/t/a-question-about-tables/311348/6 "2013-03-11T23:30:59Z")

</div>

When you have a numerically indexed table (acting like an array) and you make one of the elements nil, then it causes using the #table method of getting the table length to end at the nil:

```auto
myTable = {}  
myTable[1] = 'one'  
myTable[2] = 'two'  
myTable[3] = 'three'  
myTable[4] = 'four'  
myTable[5] = 'five'  
  
print(#myTable) -- output's 5  
  
myTable[3] = nil  
  
print(#myTable) -- output's 2  
print(table.maxn(myTable)) -- output's 5  
```

But if you do a:

```auto
myTable[3] = nil  
table.remove(myTable, 3)  
print(#myTable) -- output's 4  
```

The only thing I’m not sure about is how that impact’s garbage collection by removing the table row so quickly. But regardless, the object needs removed using display.remove() or :removeSelf()

[import]uid: 199310 topic\_id: 37468 reply\_id: 145706[/import]

---

<div class="post-metadata">

**Author:** ![Appletreeman](https://forums.solar2d.com/user_avatar/forums.solar2d.com/appletreeman/32/130_2.png) [@Appletreeman](https://forums.solar2d.com/u/Appletreeman)\
**Post date:** [March 12, 2013, 12:56pm UTC](https://forums.solar2d.com/t/a-question-about-tables/311348/7 "2013-03-12T12:56:15Z")

</div>

Cheers guys,  
That’s now working a lot better, but every so often one of the bombs will stay on the ground and not be removed. When this happens I get the following error in the terminal:  
[text]

Runtime error  
/Users/home/Desktop/Projects/vader/main.lua:70: attempt to index field ‘?’ (a nil value)  
stack traceback:  
[C]: ?  
/Users/home/Desktop/Projects/vader/main.lua:70: in function  
?: in function \<?:229\>  
[/text]  
Line 70 is the one that reads [lua]local dead=table.indexOf(bombs, self)[/lua]  
[import]uid: 7841 topic\_id: 37468 reply\_id: 145767[/import]
