Log In  
Follow
borsjt
[ :: Read More :: ]

Im having a bit of trouble understanding how to use tables, I'm quite new to programming so I took some code from Dylan Bennett who made some excellent tutorials on pico-8.

I'm using a table to create bills on screen and then delete them one by one from the screen.

So far so good.

But now I want to make a cursor that blinks on the last bill in the table, then after deletion, the cursor would hover on the next last bill in the table.

To make that work I would need the X and Y coordinates of the last placed bill and the ones following after that.

That's where I get into trouble. I'm using this function to set up the bills coordinates and sprite;

function make_bill(x,y)
    b={}
    b.x=x
    b.y=y
    b.sprite=rnd(3)+3 
    add(bills,b)
end

Then I use this to create the bills;

function make_bills()
    bills={}
    for i=1,100,1 do
        make_bill(32+rnd(56),32+rnd(56))
    end
end

But when I use a debug to find the b.x and b.y values it returns a fixed number that doesn't change as I delete the bills. Also checking in debug shows that the table b = 0.

I don't understand how to get the b.x and b.y values for each bill and am not even sure if these values are stored for each bill I create. To me it seems like the table b only stored the last created b.x and b.y values because they were simply overwritten in the creation of the other bills.

Maybe I'm not really understanding how tables work.

Any help would be greatly appreciated!

Thanks in advance!

Oh and here is my full code stack for clarity;

function _init()
    cls()
end

function _update()
    start_set()
    pop()
end

function _draw()
    cls()
    foreach(bills,draw_bill)
    debug()
end

function drawcursor()

end

function pop()
    poke(0x5f00+92,255)
    if btnp(🅾️) then
        deli(bills)
    end
end

function debug()
    if bills~=nil then
        print(#bills,0,0,7)
        returnxy(b)
    end
end

function start_set()
    poke(0x5f00+92,255)
    if btnp(❎) then
        make_bills()
    end
end

function make_bills()
    bills={}
    for i=1,100,1 do
        make_bill(32+rnd(56),32+rnd(56))
    end
end

function make_bill(x,y)
    b={}
    b.x=x
    b.y=y
    b.sprite=rnd(3)+3 
    add(bills,b)
end

function draw_bill(b)
    spr(b.sprite,b.x,b.y,1,2)
end

function returnxy(b)
    print(b.x,0,7,7)
    print(b.y,0,14,7)
    print(#b,0,21,7)
end
P#96181 2021-08-18 14:35

Follow Lexaloffle:          
Generated 2024-04-16 09:41:05 | 0.066s | Q:5