I'm making progress with my coin collection system, but I have a problem with destroying coins

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
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

I'm making progress with my coin collection system, but I have a problem with destroying coins

Post by MaxGamz »

I was able to get coins up and running however I have an issue with removing them from the coin array I put them in, for reference this is how I added the coins to my world:

Code: Select all

    for i = 1, #location do     -- location checks if the tile in the time map is where a coin should be placed (works perfectly)
        local inst = location[i]
        coin[i] = {
            x = inst.x + 8,
            y = inst.y + 8,
            w = 8,
            h = 8,
            index = i,		-- This value holds the index of each specific coin
            isCoin = true
        }
        world:add(coin[i], coin[i].x - coin[i].w * 0.5, coin[i].y - coin[i].h, coin[i].w, coin[i].h)
    end
This next one deals with destroying coins and removing them from the table

Code: Select all

	if other.other.isCoin then  -- uses bump.lua, other is cols[i]
            --remove coin from coin table
            table.remove(coin, other.other.index) -- works but not well
            --remove coin from world
            world:remove(other.other)                 -- I assume works perfectly fine since it should remove the coin from the physics world 
        end
I feel like logically it should work perfectly but it doesn't work 100 percent. Sometimes another coin is "destroyed" and sometimes I have to hit it twice for it to register.
User avatar
dusoft
Party member
Posts: 705
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: I'm making progress with my coin collection system, but I have a problem with destroying coins

Post by dusoft »

How about just doing:

Code: Select all

 
coin[i]=nil

?
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

Re: I'm making progress with my coin collection system, but I have a problem with destroying coins

Post by MaxGamz »

dusoft wrote: Mon Oct 02, 2023 8:55 am How about just doing:

Code: Select all

 
coin[i]=nil

?
I tested that idea out and it worked! However it is still a little glitchy. The coins do disappear, all of them, but for a specific coin when I hit it, it takes out another coin with it at the same time and I'm not sure why.
Last edited by MaxGamz on Mon Oct 02, 2023 11:14 am, edited 1 time in total.
User avatar
dusoft
Party member
Posts: 705
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: I'm making progress with my coin collection system, but I have a problem with destroying coins

Post by dusoft »

The you might have some issues with your indexing. With the solution proposed above, you get a table with gaps.
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

Re: I'm making progress with my coin collection system, but I have a problem with destroying coins

Post by MaxGamz »

dusoft wrote: Mon Oct 02, 2023 11:14 am The you might have some issues with your indexing. With the solution proposed above, you get a table with gaps.
My only major problem is why I take 2 coins at the same time when I grab one. This happened with the proposed solution, but its getting me somewhere
User avatar
dusoft
Party member
Posts: 705
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: I'm making progress with my coin collection system, but I have a problem with destroying coins

Post by dusoft »

Then post the complete code or the relevant parts of it.
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

Re: I'm making progress with my coin collection system, but I have a problem with destroying coins

Post by MaxGamz »

dusoft wrote: Mon Oct 02, 2023 12:47 pm Then post the complete code or the relevant parts of it.
Here is the complete love file just to make it easier
testGame.love
(883.73 KiB) Downloaded 158 times
User avatar
dusoft
Party member
Posts: 705
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: I'm making progress with my coin collection system, but I have a problem with destroying coins

Post by dusoft »

Your issue was that you were looping over `coin` using `for` and `#coin` length. But with gaps, your table could contain index 7, but the length would be e.g. just 5. So you would never come to coin index 7.

I recommend using pairs to iterate.

You former buggy code (main.lua, line 73):

Code: Select all

for i = 1, #coin do
            if coin[i] ~= nil then
                coin[i].anim:draw(goldCoin, coin[i].x, coin[i].y, nil, 1, 1, 4, 8)
            end
        end
The fixed code could be:

Code: Select all

for i, value in pairs(coin) do
            if coin[i] ~= nil then
                coin[i].anim:draw(goldCoin, coin[i].x, coin[i].y, nil, 1, 1, 4, 8)
            end
        end
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

Re: I'm making progress with my coin collection system, but I have a problem with destroying coins

Post by MaxGamz »

dusoft wrote: Mon Oct 02, 2023 10:07 pm Your issue was that you were looping over `coin` using `for` and `#coin` length. But with gaps, your table could contain index 7, but the length would be e.g. just 5. So you would never come to coin index 7.

I recommend using pairs to iterate.

You former buggy code (main.lua, line 73):

Code: Select all

for i = 1, #coin do
            if coin[i] ~= nil then
                coin[i].anim:draw(goldCoin, coin[i].x, coin[i].y, nil, 1, 1, 4, 8)
            end
        end
The fixed code could be:

Code: Select all

for i, value in pairs(coin) do
            if coin[i] ~= nil then
                coin[i].anim:draw(goldCoin, coin[i].x, coin[i].y, nil, 1, 1, 4, 8)
            end
        end
Oh ok I see what you mean by the problem with the gaps! But the load function should still be fine right?
User avatar
dusoft
Party member
Posts: 705
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: I'm making progress with my coin collection system, but I have a problem with destroying coins

Post by dusoft »

Yep, the generation is fine.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests