[SOLVED]Remove an object from 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.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Remove an object from a table.

Post by Plu »

Add this in your creation function, after you make the fixture.

Code: Select all

coin.fixture.setUserData( coin )
It will store a reference to the coin inside the fixture. Now, when you resolve the collision, you should be able to get to the fixture, and then you can recover the coin by doing this:

Code: Select all

fixture.getUserData()
You can then match the coin you get to the objects inside the coins table by just comparing them, and then remove it.
that's a problem because all of the objects in the table are exactly the same
This isn't really true; no two objects are the same unless they are literally references to the very same object. These two values below will be considered different:

Code: Select all

a = { x = 10, y = 100 }
b = { x = 10, y = 100 }

a == b -- false!
However, if you have two references to the same table, they will match. But since they match the same thing, if you change one, you also change the other:

Code: Select all

a = { x = 10, y = 100 }
b = a -- now you have a reference to the same table a

a == b -- now true!
a.x = 100 -- b.x is now also 100, because b is just a link to the same table that a goes to.
User avatar
Dr.Tyler O.
Citizen
Posts: 58
Joined: Tue Jul 29, 2014 9:17 am
Location: United States

Re: Remove an object from a table.

Post by Dr.Tyler O. »

Plu wrote:This isn't really true; no two objects are the same unless they are literally references to the very same object.
Thank you so much for the explanation. I can't believe I didn't realize that before.
Any topic I have ever posted that I felt did not require a .love file, although they were requested, never required one so I would appreciate if you did not ask me to supply one unless you think that I am ignorant for not doing so.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests