## Find the order of the object itself in a table?

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.
ilovelove2019
Citizen
Posts: 78
Joined: Wed Sep 11, 2019 10:38 am

### Find the order of the object itself in a table?

Hello everyone! Let's go to the main topic.
I am programming games and now I want to destroy enemies. In main.lua I had a table named listOfFrog. I added the enemy by table.insert (listOfFrog, Frog (500, 400)) ... In player.lua, when the player collided with an enemy I called: enemy: boom () (with the enemy being object created from Frog class). The code in main.lua and player.lua is okay already. And the problem starts here: In Frog.lua I have a function as follows:

Code: Select all

function Frog:boom()
self.collider:destroy()
table.remove(listOfFrog, ???)
end
I do not know what to fill in the question mark. I tried table.remove(listOfFrog) and when the player collides with a frog, it clears the remaining frogs. I tried table.remove(listOfFrog, self) and it have error. I know I must be like this: table.remove(listOfFrog, The order of this object in listOfFrog). But I don't know how to get it!

More, In player.lua, I handled the collision as follows:

Code: Select all

if self.collider:enter('Frog') then
local collision_data = self.collider:getEnterCollisionData('Frog')
local enemy = collision_data.collider:getObject()
if enemy.collider:getY() > self.collider:getY() then
self.collider:applyLinearImpulse(0, -100)
print("ouch")
enemy:boom()
end
end
(I use windfield library to handle physics).

Hope you will help me as soon as possible. Thank you so much!

monolifed
Party member
Posts: 120
Joined: Sat Feb 06, 2016 9:42 pm

### Re: Find the order of the object itself in a table?

you might set frog status to dead
then you can clean then at once (you can also copy alive ones to a new table)

Code: Select all

for i=#listofrogs, 1, -1 do
if listofrogs[i].status == "dead" then remove(listofrogs, i) end
end
or since self points to your frog you can do

Code: Select all

for k, v in ipairs(listofrogs) do
if v == self then remove(listofrogs, k) break end
end

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Find the order of the object itself in a table?

You can try this implementation of array: https://bitbucket.org/rcoaxil/lua-minil ... uarray.lua

It's tailored to store game objects such as enemies or bullets (but it can be any type of value). Specifically, it will store exactly one of each value in the array, so you can keep adding them without checking every time and it'll sort itself automatically. Then, you can remove items by value (i.e. you pass the object itself and not its index number). And finally, you can iterate over objects in a for-loop like normal. To improve performance it also does a bit of book-keeping so it keeps its length (array.len) which is faster than using LuaJIT array length operator. If you don't care about specific order in which the objects appear in a for-loop (that's usually the case) then you can use "pull" instead of "remove" and "push" instead of "insert" which will work a lot faster.

Code: Select all

uarray = require("uarray")
listOfFrog = uarray()
listOfFrog:push(Frog(500, 400))

function Frog:boom()
self.collider:destroy()
listOfFrog:pullval(self) --object is pulled from table by value (not by index)
--same as uarray.pullval(listOfFrog, self)
end

ilovelove2019
Citizen
Posts: 78
Joined: Wed Sep 11, 2019 10:38 am

### Re: Find the order of the object itself in a table?

raidho36 wrote:
Tue Oct 01, 2019 9:25 pm
You can try this implementation of array: https://bitbucket.org/rcoaxil/lua-minil ... uarray.lua

It's tailored to store game objects such as enemies or bullets (but it can be any type of value). Specifically, it will store exactly one of each value in the array, so you can keep adding them without checking every time and it'll sort itself automatically. Then, you can remove items by value (i.e. you pass the object itself and not its index number). And finally, you can iterate over objects in a for-loop like normal. To improve performance it also does a bit of book-keeping so it keeps its length (array.len) which is faster than using LuaJIT array length operator. If you don't care about specific order in which the objects appear in a for-loop (that's usually the case) then you can use "pull" instead of "remove" and "push" instead of "insert" which will work a lot faster.

Code: Select all

uarray = require("uarray")
listOfFrog = uarray()
listOfFrog:push(Frog(500, 400))

function Frog:boom()
self.collider:destroy()
listOfFrog:pullval(self) --object is pulled from table by value (not by index)
--same as uarray.pullval(listOfFrog, self)
end
OH! Raidho36! It seems a good way but when I try it i get error. Sometimes, I jump in to Frog and it like this: Error: frog.lua:44: calling 'getLinearVelocity' on bad self (Body expected, got nil) (I have previously declared the following

Code: Select all

xVel,yVel = self.collider:getLinearVelocity()
). Can you help me? Thank you

ilovelove2019
Citizen
Posts: 78
Joined: Wed Sep 11, 2019 10:38 am

### Re: Find the order of the object itself in a table?

ingsoc451 wrote:
Tue Oct 01, 2019 2:11 pm
you might set frog status to dead
then you can clean then at once (you can also copy alive ones to a new table)

Code: Select all

for i=#listofrogs, 1, -1 do
if listofrogs[i].status == "dead" then remove(listofrogs, i) end
end
or since self points to your frog you can do

Code: Select all

for k, v in ipairs(listofrogs) do
if v == self then remove(listofrogs, k) break end
end
Your solution work perfectly. I have tried it and everything so good. Thanh you

ilovelove2019
Citizen
Posts: 78
Joined: Wed Sep 11, 2019 10:38 am

### Re: Find the order of the object itself in a table?

I am really happy now. During the course of my game programming, I went through many different programming languages, game engines, and frameworks. Each has its own community. But for me, the love2d community is the friendliest. People are always kind and highly skilled. They are always ready to help me when needed. Truly awesome. Thank you very much.

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Find the order of the object itself in a table?

Not sure what went wrong when you tried to use the array, but it's good to know you've got it all worked out.

ilovelove2019
Citizen
Posts: 78
Joined: Wed Sep 11, 2019 10:38 am

### Re: Find the order of the object itself in a table?

raidho36 wrote:
Tue Oct 01, 2019 11:18 pm
Not sure what went wrong when you tried to use the array, but it's good to know you've got it all worked out.
Thank you so much!

### Who is online

Users browsing this forum: No registered users and 39 guests