[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
Dr.Tyler O.
Citizen
Posts: 58
Joined: Tue Jul 29, 2014 9:17 am
Location: United States

[SOLVED]Remove an object from a table.

Post by Dr.Tyler O. »

I have some code here that spawns a coin and adds it to a table so that all of the coins can be drawn with a for loop. When the player touches a coin it needs to delete that coin specifically and that's a problem because all of the objects in the table are exactly the same.

Code: Select all

coins = {}
function spawn_coin(x, y)
	coin = {}
	coin.body = love.physics.newBody(world, x, y, "dynamic")
	coin.shape = love.physics.newRectangleShape(20, 20)
	coin.fixture = love.physics.newFixture(coin.body, coin.shape)
	coins[#coins+1] = coin
end
Last edited by Dr.Tyler O. on Tue Sep 16, 2014 1:27 am, edited 1 time in total.
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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Remove an object from a table.

Post by davisdude »

Code: Select all

table.remove( coins, Index )
Where Index is the index of the coin in the table.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
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. »

davisdude wrote:

Code: Select all

table.remove( coins, Index )
Where Index is the index of the coin in the table.
I'm aware of table.remove() but I have to remove a specific object that depends on the order in which the player touches the coins.
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.
Psyk
Prole
Posts: 8
Joined: Thu Sep 11, 2014 10:40 pm

Re: Remove an object from a table.

Post by Psyk »

Dr.Tyler O. wrote:
davisdude wrote:

Code: Select all

table.remove( coins, Index )
Where Index is the index of the coin in the table.
I'm aware of table.remove() but I have to remove a specific object that depends on the order in which the player touches the coins.

Loop through the table and if a player collides with a coin, remove it from the table.

Code: Select all

This is what I would do.

for key, value in ipairs(table) do 
        if playerCollidesWithCoin then
              table.remove(table, key)
        end 
end
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Re: Remove an object from a table.

Post by rmcode »

I think what you are asking for can't be done, because there is no way for your code to tell which coin it should remove (and that's the problem you are facing IIUC).

You could use the x and y coordinates as indexes for the different coins (which would only work if two coins can't be in the same place at the same time). A different approach would be to store the coordinates of the coins in each of the coins and then add a check in your loop.

Something like this:

Code: Select all

   local coins = {}
    function spawn_coin(x, y)
        local coin = {};
        coin.body = love.physics.newBody(world, x, y, "dynamic");
        coin.shape = love.physics.newRectangleShape(20, 20);
        coin.fixture = love.physics.newFixture(coin.body, coin.shape);
        coin.x = x;
        coin.y = y;
        coins[#coins+1] = coin;
    end

    for i, coin in pairs(coins) do
        if playerCollidesWithCoin() and coin.x == player.x and coin.y == player.y then
            coins[i] = nil;
        end
    end
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. »

rmcode wrote:I think what you are asking for can't be done, because there is no way for your code to tell which coin it should remove (and that's the problem you are facing IIUC).
Yes, that's the problem. If it can't be solved by doing it this way, are there any other ways of doing this? Love is a game framework so there should be some way of accomplishing this in a much better fashion.
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.
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Re: Remove an object from a table.

Post by rmcode »

Dr.Tyler O. wrote:Love is a game framework so there should be some way of accomplishing this in a much better fashion.
Finding solutions to problems in your code / design is your job ;)

I gave you a possible approach above.
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. »

rmcode wrote:Finding solutions to problems in your code is your job ;)
That's true, and if I can't solve the problem myself then I ask for help from people who can help me solve those problems and learn from my mistakes.
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.
User avatar
verilog
Citizen
Posts: 97
Joined: Thu Nov 03, 2011 3:15 am
Contact:

Re: Remove an object from a table.

Post by verilog »

Dr.Tyler O. wrote:
I'm aware of table.remove() but I have to remove a specific object that depends on the order in which the player touches the coins.
There’s at least one way to accomplish this. The idea is to manage references to tables instead of indices. Indices change every time you modify the entries in a table; references stay the same if you re-order the entries (i.e., pointers). If you have a table where you store all your references, you can use a field on each table reference to remove it when you no longer need it. You’ll need some functions and tables to keep track of your data management, though. Here are some tips:

You need to use a function to create a table of references (the "master table") and store every new entry:

Code: Select all

function createEntry(masterTable)
  local i = #masterTable + 1
  print("initial entry ID: "..i)
  masterTable[i] = {} -- you will store the object here
  i = masterTable[i]
  print("ID is now a 'pointer' ")
  return i -- the reference of the entry
end
Note that the function returns the reference of the entry. You need to use this value in whatever data structure (or function) you use to update (or draw) your object. You can also include a table field to use as remove flag.

Code: Select all

function drawTableEntry(tableReference, removeFlag, img) --tableReference is returned by createEntry
  tableReference.removeFlag = removeFlag
  tableReference.img = img
end
To remove the entry, loop through the references table (using a reverse loop) and look for the remove flag. Be sure to call this function only when needed, for instance, only when the object's state changes.

Code: Select all

function removeEntry(masterTable)
  for i = #masteTable, 1, -1 do
    if (masteTable[i].removeFlag) then
      table.remove(q, i)
      print("item removed from list, index was: "..i)
    end
  end
end
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Remove an object from a table.

Post by HugoBDesigner »

There's a simple way of doing this that I found in Mari0's code. I didn't understand it at the time, but while making a game I did the same thing (I didn't copy Mari0's code, just to be clear. The idea is really basic and many people must have done the same thing). You can add the index number of each coin to a temporary "delete" table and then remove it:

Code: Select all

local delete = {}
for i, v in ipairs(coins) do
    if collidesWithPlayer(v) then
        table.insert(delete, i)
    end
end

for i = #delete, 1, -1 do
    table.remove(coins, delete[i])
end
I think it is very similar to verilog's method as well, so you can use his' if you want :)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests