Page 1 of 1

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

Posted: Sun Oct 01, 2023 9:50 pm
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.

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

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

Code: Select all

 
coin[i]=nil

?

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

Posted: Mon Oct 02, 2023 11:13 am
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.

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

Posted: Mon Oct 02, 2023 11:14 am
by dusoft
The you might have some issues with your indexing. With the solution proposed above, you get a table with gaps.

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

Posted: Mon Oct 02, 2023 12:01 pm
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

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

Posted: Mon Oct 02, 2023 12:47 pm
by dusoft
Then post the complete code or the relevant parts of it.

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

Posted: Mon Oct 02, 2023 4:30 pm
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 159 times

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

Posted: Mon Oct 02, 2023 10:07 pm
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

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

Posted: Mon Oct 02, 2023 10:39 pm
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?

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

Posted: Tue Oct 03, 2023 3:14 pm
by dusoft
Yep, the generation is fine.