sulai [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=25420 Enums in Lua <p>I recently tried to implement an enum structure in Lua, which turned out to be very useful. So I thought I might share it here :)</p> <p>Find the latest version here (it's part of a Pico-8 library) <a href="https://github.com/sulai/Lib-Pico8/blob/master/lang.lua">https://github.com/sulai/Lib-Pico8/blob/master/lang.lua</a></p> <p>Then enum(...) function generates an enum structure from a list of names.</p> <p>The generated structure is useful when it comes to readability and object orientation.</p> <p>As a use case for Pico-8, you can comfortably generate named objects that map to sprite ids. So if you want to associate names to your sprites like &quot;sword&quot;, &quot;shield&quot;, etc to show that to the user, this is a nice way to do it.</p> <p>Example usage in a RPG:</p> <div> <div style="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> tiles = enum( {&quot;grass&quot; ,&quot;water&quot;, &quot;rock&quot;} ) -- landscape sprites start at 1 items = enum( {&quot;sword&quot; ,&quot;shield&quot;, &quot;bow&quot;}, 16 ) -- item sprites start at 16 colors = enum( {&quot;black&quot;, &quot;blue&quot;, ... }, 0 ) -- map color palette. actions = enum( {&quot;look&quot;, &quot;take&quot;, ... } ) -- just an enum. </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>You can now use the enums to make your code more readable:</p> <div> <div style="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> if action==actions.take then mset(x,y,tiles.grass.id) pset(x,y,colors.green.id) end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <ul> <li>You can resolve to its name like tiles.water.name.</li> <li>You can resolve by its id, eg: tiles[3].name</li> <li>you can iterate over all enum entries like: for item in items.all() do print(item.name) end</li> <li>You can offset the ids by using tiles=enum(names, 10). Useful if your sprites start at index 10.</li> <li>You can also map arbitrary ids to objects like furniture=enum({[15]=&quot;table&quot;, [20]=&quot;chair&quot;}).</li> <li>You can use the generated objects as a starting point for more complex objects like furniture.table.heavy=true,<br /> or attach functionality like:</li> </ul> <div> <div style="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> actions.look.execute = function(item) print(&quot;Looks like a usual &quot;..item.name) end actions.take.execute = function(item) if not item.heavy then add(inventory, item) end end function onactionuse(action, item) action.execute(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>Keep in mind that the usage of enums consumes more tokens than using magic numbers. You trade tokens for readability and elegant code.</p> https://www.lexaloffle.com/bbs/?tid=29891 https://www.lexaloffle.com/bbs/?tid=29891 Sun, 03 Sep 2017 18:33:08 UTC Smooth cursor movement <p>I'm just starting a new game from scratch and programming pico-8 feels really nice :)</p> <p>So the first step is to have a cursor moving on a map. I wanted it to be really smooth and snappy and fast. So this is what I came up with.</p> <p>Feel free to use it in your projects :)</p> <p> <table><tr><td> <a href="/bbs/?pid=43862#p"> <img src="/bbs/thumbs/pico43864.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=43862#p"> Smooth cursor movement</a><br><br> by <a href="/bbs/?uid=25420"> sulai</a> <br><br><br> <a href="/bbs/?pid=43862#p"> [Click to Play]</a> </td></tr></table> </p> https://www.lexaloffle.com/bbs/?tid=29883 https://www.lexaloffle.com/bbs/?tid=29883 Sat, 02 Sep 2017 13:08:00 UTC [Spin-off] Colony on a tiny planet <p>I'm happy to announce my very first contribution to the Pico-8 community, which is a spin-off of Makiki's game title &quot;A lone colony on a small planet&quot;.</p> <p>Your goal now is to colonize an even smaller planet, but with some difficulties added to it. So this one is a more of a thinking game, while Makikis original is more of fast builder.</p> <p>Credits:</p> <ul> <li><a href="https://www.lexaloffle.com/bbs/?tid=29214">Makikis &quot;A lone colony on a small planet&quot;</a></li> <li><a href="https://www.lexaloffle.com/bbs/?pid=43788#p43788">Music by RobbyDuguay</a>.</li> </ul> <p>Story:</p> <p><strong>&quot;You have been given a mission. You need to purify a small planet, so we will be able to colonize it. It is rich in resources, so it shouldn't pose a big problem for you. We believe in your management skills. Good luck.&quot;</strong> -- Makiki</p> <p>Controls:</p> <ul> <li>O to build.</li> <li>X is a multi tool. <ul> <li>build faster. you can keep it pressed and move around.</li> <li>select what to build by using X over an existing building</li> <li>get information about a building</li> <li>change transport type</li> <li>show the map and statistics</li> </ul></li> </ul> <p> <table><tr><td> <a href="/bbs/?pid=43859#p"> <img src="/bbs/thumbs/pico58904.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=43859#p"> [Spin-off] Colony on a tiny planet 1.6.8</a><br><br> by <a href="/bbs/?uid=25420"> sulai</a> <br><br><br> <a href="/bbs/?pid=43859#p"> [Click to Play]</a> </td></tr></table> </p> <p>Hint to get started: produce energy and connect the energy producer to quarries. Once powered, they start mining minerals.</p> <p>More hints (spoiler):<br /> <div><div><input type="button" value=" Show " onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = ' Hide '; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = ' Show '; }"></div><div><div style="display: none;"></p> <ul> <li>you can build &quot;transport&quot; tiles to transport about everything: energy, coal and oil</li> <li>prefer hydro plants over solar panels, they are cheaper. connect the hydro plants to your quarries with transport</li> <li>building transportation is cheap. you can use them as a switch by removing a transport tile. It's possible to re-route energy that way.</li> <li>activate the core by delivering coal and oil to it</li> <li>once activated, it functions as the core of a purifier cluster. All purifiers adjacent to it work together.<br /> </div></div></div></li> </ul> <p>Development details (spoiler):<br /> <div><div><input type="button" value=" Show " onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = ' Hide '; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = ' Show '; }"></div><div><div style="display: none;"><br /> I liked the setting of Makiki's game, so I invested 2 weeks into it to add features, tweak the game and learn about Pico-8 and Lua along the way.</p> <p>Feature list:</p> <ul> <li>random map generator (had to remove Makiki's hand drawn map, sadly)</li> <li>producers of energy, coal and oil need to be connected to their producers <ul> <li>to get the path finding/validation to work took me ages. My very first experience with Lua though.</li> </ul></li> <li>transport tiles to transport any goods over a larger distance</li> <li>core works as a purifier cluster core. <ul> <li>I think this one is a nice idea, as it replaces late-game grinding with late-game &quot;I need to rewire a few things&quot;. On the other hand, It's perfectly possible to win without activating and using the core, and I think it's both fun in their own ways.</li> </ul></li> <li>lots of tweaks and balancing on the ui, controls and game elements</li> <li>lots of memory and cpu optimization. I still ended up to reduce map size to prevent out of memory errors in the late game. Which actually results in a good game length. It finishes just before you need to start grinding. Thus the title &quot;Colony on a <em>tiny</em> planet. ;)<br /> </div></div></div></li> </ul> https://www.lexaloffle.com/bbs/?tid=29882 https://www.lexaloffle.com/bbs/?tid=29882 Sat, 02 Sep 2017 10:55:53 UTC p8lua: work with external code editors <p>Hi there,</p> <p>I wrote a script which helps to write lua code in an external editor. The script listens to changes in the .lua file and re-integrates the lua code back into the .p8 cartridge. Each time you save.</p> <p>So you can happily change lua files and edit assets in Pico-8 and everything gets merged back as soon as you save. Write some code in your editor, switch to Pico-8 and press ctrl-R to reload. It's quick :)</p> <p>Works great for me in Intelij Idea, where I have syntax highlighting, code analysis and code completion. When trying to call a function, the IDE even shows the function parameters and lua-doc. What more can you ask for?</p> <p>Some other things the script can do for you</p> <ul> <li>include library code (#include library)</li> <li>remove debug code on release (#define debug, #if debug, #end debug)</li> <li>strip comments if you hit the size limit (please consider that comments are helpful for the Pico-8 community ;))</li> <li>convert pico-8 specific statements like += to plain .lua. Some IDEs might work better with plain lua.</li> </ul> <p>That's it, hope you like it. Download it here:</p> <p><a href="https://github.com/sulai/p8lua">https://github.com/sulai/p8lua</a></p> <p>Have a look in the python file, there is more detailled und up to date instructions on the features.</p> <p>PS: one down side though: the script works for Linux only at the moment, since it uses pyinotify for getting notified about file changes. I'm happy to accept pull requests if you want to port it to windows, maybe doing polling on the .lua files.</p> https://www.lexaloffle.com/bbs/?tid=29859 https://www.lexaloffle.com/bbs/?tid=29859 Tue, 29 Aug 2017 07:30:02 UTC basic cpu profiling <p>In case your game gets slow, it's always helpful to identify the parts of code that take up too much time to compute. This is how you can measure execution time:</p> <div> <div style="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> local start = stat(1) -- .... do some intensive stuff .... printh(&quot;this took &quot;..((stat(1)-start)*100)..&quot;% of a frame&quot;) </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Hope it helps, and thanks to <a href="https://www.lexaloffle.com/bbs/?uid=12874"> @Felice</a> for pointing out how to accomplish this.</p> <p>sulai</p> https://www.lexaloffle.com/bbs/?tid=29854 https://www.lexaloffle.com/bbs/?tid=29854 Sun, 27 Aug 2017 08:46:21 UTC basic memory profiling <p>You can use this function in your program to do some basic memory profiling. Given the limit of 1MB memory for Lua variables, a little profiling will be much needed for many of us ;) It will return the amount of all elements of a table, including the content of sub tables.</p> <p>It will not show the actual amount of bytes used, would be interesting if there is a way to calculate that? But you can use this as a rough estimation and check if your optimizations show any effect on the table size.</p> <div> <div style="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> table={} function table.size(t) local size=0 for k,v in pairs(t) do size+=1 if type(v)==&quot;table&quot; then size+=table.size(v) end end return size 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=29848 https://www.lexaloffle.com/bbs/?tid=29848 Sat, 26 Aug 2017 16:03:42 UTC convert anything to string <div> <div style="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> -- converts anything to string, even nested tables function tostring(any) if type(any)==&quot;function&quot; then return &quot;function&quot; end if any==nil then return &quot;nil&quot; end if type(any)==&quot;string&quot; then return any end if type(any)==&quot;boolean&quot; then if any then return &quot;true&quot; end return &quot;false&quot; end if type(any)==&quot;table&quot; then local str = &quot;{ &quot; for k,v in pairs(any) do str=str..tostring(k)..&quot;-&gt;&quot;..tostring(v)..&quot; &quot; end return str..&quot;}&quot; end if type(any)==&quot;number&quot; then return &quot;&quot;..any end return &quot;unkown&quot; -- should never show 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>Example usage:</p> <div> <div style="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(&quot;&quot;..tostring({true,x=7,{&quot;nest&quot;}})) { 1-&gt;true 2-&gt;{ 1-&gt;nest } x-&gt;7 } </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=29844 https://www.lexaloffle.com/bbs/?tid=29844 Sat, 26 Aug 2017 07:12:56 UTC