jhol [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=33342 Tables, All(), and Count() <p>Hello!</p> <p>I have just started to dive into Pico 8 and Lua. I've been working on building a simple animator for my game and have run into some strange issues with regard to tables.</p> <p>I declare a table and later add to it like this:</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>a = {} a.p = {x=1, y=2, z=3}</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>The function count(a) then returns 0. Additionally, all(a) and foreach(a, function) will not iterate overt the table. Strangely, I am still able to access the information. For example,</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>print(a.p.x)</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>will correctly print the number 1. If I declare the table explicitly</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>a = {p={x=1,y=2,z=3}}</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>I have no issues. I also don't have issues if I use add() to insert new table values. However, I need to utilize the associative array functionality of the table and to add and remove values at runtime, so neither of these solutions is sufficient.</p> <p>The best fix I have found is to use pairs() instead of all(). For some reason, pairs() works properly regardless of how I add values to the table. This solves my problem, but the fact that I have no idea why leads me to believe that I have some serious misunderstanding of how Lua tables and/or the Pico 8 tables api works. I'm curious as to whether others have encountered this issue and would appreciate some feedback as to where my misunderstanding may lie.</p> https://www.lexaloffle.com/bbs/?tid=32752 https://www.lexaloffle.com/bbs/?tid=32752 Mon, 31 Dec 2018 20:22:02 UTC