zovirl [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=44961 Let's learn about tables! <p>If you feel like you don't understand Lua's tables, I hope this post helps. I use tables all the time when I'm programming; they are super useful. Let's study them so you can use them too!</p> <p>Tables can do several different things, which makes them a bit tricky. We'll start by exploring a common use: using tables to store a list of things, such as:</p> <ul> <li>A hand of cards: 2&spades;, 3&hearts;, Q&diams;, K&clubs;, J&clubs;</li> <li>A list of monsters: Ogre, goblin, slime mold</li> <li>The positions of all the black checkers: 12, 24, 20, 27</li> </ul> <h2>Making lists</h2> <p>This makes an empty list:</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>items={}</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>This makes a list with the names of some things found in a forest:</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>items={'tree', 'rock', 'stream'}</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Now <code>items</code> contains 3 things; you can think of it like 3 boxes:</p> <img style="margin-bottom:16px" border=0 src="/media/44961/3_list1_make.png" alt="" /> <p>The first box contains 'tree', the second contains 'rock', the third contains 'stream'.</p> <h2>Getting items out of a list</h2> <p>Each box has a number, called its &quot;index&quot;. To get the value from the first box, we do <code>items[1]</code> 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>&gt; print(items[1]) tree</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>We can also store the index in a variable, 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>&gt; current_index=1 &gt; print(items[current_index]) tree</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>To pick a random item from a list, we can use rnd():</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>&gt; print(rnd(items)) stream &gt; print(rnd(items)) tree</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <h2>Changing items in a list</h2> <p>We can change the value stored in a box by using its index. Here's how to replace 'rock' with 'moss':</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>&gt; items[2]='moss'</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <img style="margin-bottom:16px" border=0 src="/media/44961/4_list2_change.png" alt="" /> <h2>Length of a list</h2> <p>To get the number of items in a list, we use #items:</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>&gt; length=#items &gt; print(length) 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>Here's a common way to get the last item of the list. It uses the list's length as an index:</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>&gt; print(items[#items]) stream</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <h2>Adding items</h2> <p>Items can be added to a list using add().</p> <p>This adds to the end of the list:</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>add(items, 'mushroom') -- adds at end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <img style="margin-bottom:16px" border=0 src="/media/44961/list3_add.png" alt="" /> <p>This adds at the start of the list:</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>add(items, 'wolf', 1) -- adds at index 1</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <img style="margin-bottom:16px" border=0 src="/media/44961/list4_add.png" alt="" /> <h2>Deleting items</h2> <p>We can delete items by value:</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>del(items, 'tree')</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <img style="margin-bottom:16px" border=0 src="/media/44961/list5_del.png" alt="" /> <p>or by index:</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>deli(items,2)</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <img style="margin-bottom:16px" border=0 src="/media/44961/list6_deli.png" alt="" /> <h2>Looping over a list</h2> <p>We can access every item in a list using a for loop and all():</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>for item in all(items) do print(item) 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>That would print 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>wolf stream mushroom</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>If you need the index too, you can use a numeric for loop from 1 up to the length of the list:</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>for i=1,#items do print(i..': '..items[i]) 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>That would print 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>1: wolf 2: stream 3: mushroom</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Ok, that's it for lists.</p> <p>Wow, that turned out longer than I expected. I also want to explore using tables to store dictionaries, but before I do, does anyone have any questions? Or feedback? Is this useful? Would it be better with more (or less) detail? Let me know what you think...</p> https://www.lexaloffle.com/bbs/?tid=54314 https://www.lexaloffle.com/bbs/?tid=54314 Thu, 28 Sep 2023 04:00:10 UTC Would a &quot;math for games&quot; tutorial be useful? Or a collisions tutorial? <p>I feel like making a set of tutorial carts. What topic would help people the most? </p> <p>I was thinking of a simple introduction to math for games, since that topic comes up frequently on the forums. Or maybe how to do collision detection? I see people asking about that fairly often too.</p> <p>Any thoughts? Or suggestions of a topic? Would you find this useful?</p> https://www.lexaloffle.com/bbs/?tid=53792 https://www.lexaloffle.com/bbs/?tid=53792 Sat, 19 Aug 2023 00:07:32 UTC Minotaur <p> <table><tr><td> <a href="/bbs/?pid=128248#p"> <img src="/bbs/thumbs/pico8_minotaur_v1-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=128248#p"> minotaur_v1</a><br><br> by <a href="/bbs/?uid=44961"> zovirl</a> <br><br><br> <a href="/bbs/?pid=128248#p"> [Click to Play]</a> </td></tr></table> </p> <p>King Minos has thrown you into the minotaur's labrynth! Can you kill the minotaur, rescue your fellow Athenians, and find your way out of the labrynth?</p> <p>Controls:<br /> Arrows to move.<br /> Z to attack.</p> https://www.lexaloffle.com/bbs/?tid=52343 https://www.lexaloffle.com/bbs/?tid=52343 Fri, 07 Apr 2023 03:42:59 UTC Perspective-correct texture mapping <p>3D texture mapping with tline (plus some other stuff, like wireframe rendering, and solid color polygon rendering). Did this as a learning project, to study old-school pre-GPU 3D graphics.</p> <p>Thanks to freds72 and johanp for inspiration &amp; example code.</p> <p> <table><tr><td> <a href="/bbs/?pid=83849#p"> <img src="/bbs/thumbs/pico8_texture_map-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=83849#p"> texture_map_0</a><br><br> by <a href="/bbs/?uid=44961"> zovirl</a> <br><br><br> <a href="/bbs/?pid=83849#p"> [Click to Play]</a> </td></tr></table> </p> <p>References if you want to learn.<br /> <a href="http://www.multi.fi/~mbc/sources/fatmap2.txt">http://www.multi.fi/~mbc/sources/fatmap2.txt</a><br /> <a href="https://chrishecker.com/miscellaneous_technical_articles#perspective_texture_mapping">https://chrishecker.com/miscellaneous_technical_articles#perspective_texture_mapping</a><br /> <a href="https://www.cs.cmu.edu/~fp/courses/graphics/pdf-color/14-raster.pdf">https://www.cs.cmu.edu/~fp/courses/graphics/pdf-color/14-raster.pdf</a><br /> <a href="https://en.wikipedia.org/wiki/digital_differential_analyzer_(graphics_algorithm">https://en.wikipedia.org/wiki/digital_differential_analyzer_(graphics_algorithm</a>)<br /> <a href="https://en.wikipedia.org/wiki/selection_sort">https://en.wikipedia.org/wiki/selection_sort</a><br /> <a href="https://en.wikipedia.org/wiki/sutherland%e2%80%93hodgman_algorithm">https://en.wikipedia.org/wiki/sutherland%e2%80%93hodgman_algorithm</a><br /> Book: &quot;Computer Graphics: Principles and Practice 2nd ed.&quot;</p> https://www.lexaloffle.com/bbs/?tid=40234 https://www.lexaloffle.com/bbs/?tid=40234 Fri, 06 Nov 2020 05:29:03 UTC Anonymous inline functions don't work in tabs? <p> <table><tr><td> <a href="/bbs/?pid=83769#p"> <img src="/bbs/thumbs/pico8_bug_anon_inline_func-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=83769#p"> bug_anon_inline_func</a><br><br> by <a href="/bbs/?uid=44961"> zovirl</a> <br><br><br> <a href="/bbs/?pid=83769#p"> [Click to Play]</a> </td></tr></table> </p> <p>I'm trying to use anonymous inline functions and I'm finding some weird behavior when they are on tabs.</p> <p>Putting everything in tab #0 works:</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>x=1 function _draw() cls(1) print('x is '..x) end (function() x=2 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>And moving the inline function call to tab #1 works:</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>x=1 function _draw() cls(1) print('x is '..x) end --&gt;8 (function() x=3 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 putting an inline function call on both tab #0 and tab #1 does not work:</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>x=1 function _draw() cls(1) print('x is '..x) end (function() x=2 end)() --&gt;8 (function() x=3 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>I don't understand why. It gives this error:</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>runtime error line 7 tab 0 (function() attempt to call a nil value at line 7 (tab 0)</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=40179 https://www.lexaloffle.com/bbs/?tid=40179 Thu, 05 Nov 2020 05:27:36 UTC Why is this cart so slow on Raspberry Pi Zero? <p>I'm trying to figure out why this cart only runs at 10 FPS on a Raspberry Pi Zero. stat(1) returns 0.49 and stat(7) returns 30. However, the screen is only updating at 10-11 FPS. Other carts with higher stat(1) values don't show this problem, so it seems specific to something in this cart.</p> <p>I can optimize to get the CPU usage down to 17%, and then the real framerate becomes acceptable. However, I'd actually like to understand why it is slow so I can work around it. (Also, I'd prefer to be able to use the whole CPU budget instead of just 17% of it!)</p> <p>I assume what's happening is that the pico-8 CPU costs are optimistic for some instructions, and the actual cost on the Pi Zero hardware is higher. But which instructions?</p> <p>Are there specific instructions I should be avoiding?</p> <p>Are there specific types of drawing (like large map areas, or off-screen drawing) I should avoid?</p> <p>Can I hook up a profiler to see what it is spending its time on?</p> <p>The original raspberry pi post (<a href="https://www.lexaloffle.com/bbs/?tid=3085">https://www.lexaloffle.com/bbs/?tid=3085</a>) says math-heavy carts run slowly. Is that still true?</p> <p>Are there any performance tricks for writing carts that run well on a raspberry pi?</p> <p> <table><tr><td> <a href="/bbs/?pid=78645#p"> <img src="/bbs/thumbs/pico8_slow-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=78645#p"> slow</a><br><br> by <a href="/bbs/?uid=44961"> zovirl</a> <br><br><br> <a href="/bbs/?pid=78645#p"> [Click to Play]</a> </td></tr></table> </p> https://www.lexaloffle.com/bbs/?tid=38602 https://www.lexaloffle.com/bbs/?tid=38602 Mon, 29 Jun 2020 00:59:33 UTC