borsjt [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=43091 Tables and how to use them <p>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.</p> <p>I'm using a table to create bills on screen and then delete them one by one from the screen.</p> <p>So far so good.</p> <p>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.</p> <p>To make that work I would need the X and Y coordinates of the last placed bill and the ones following after that.</p> <p>That's where I get into trouble. I'm using this function to set up the bills coordinates and sprite;</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>function make_bill(x,y) b={} b.x=x b.y=y b.sprite=rnd(3)+3 add(bills,b) end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Then I use this to create the bills;</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>function make_bills() bills={} for i=1,100,1 do make_bill(32+rnd(56),32+rnd(56)) end end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>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.</p> <p>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.</p> <p>Maybe I'm not really understanding how tables work.</p> <p>Any help would be greatly appreciated!</p> <p>Thanks in advance!</p> <p>Oh and here is my full code stack for clarity;</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>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</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> https://www.lexaloffle.com/bbs/?tid=44296 https://www.lexaloffle.com/bbs/?tid=44296 Wed, 18 Aug 2021 14:35:07 UTC