dagondev [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=15064 Sample Code - Debugging tools <p> <table><tr><td> <a href="/bbs/?pid=34287#p"> <img src="/bbs/thumbs/pico34286.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=34287#p"> Sample Code - Debugging tools</a><br><br> by <a href="/bbs/?uid=15064"> dagondev</a> <br><br><br> <a href="/bbs/?pid=34287#p"> [Click to Play]</a> </td></tr></table> </p> <p>Hello everyone. </p> <p>As I was writing <a href="https://www.lexaloffle.com/bbs/?tid=28309">Save Yourself</a> I needed some proper debugging methods to speed up finding bugs. Because my game was almost over token and char limit I had to completely scrap debug code at the end of the cycle. So I thought moving this debug to other cart may help someone in same need as I was.</p> <p>FEATURES:</p> <ul> <li>Prints messages with time (in seconds) attached.</li> <li>Deals with strings, numbers and nils</li> <li>Ability to control number of messages shown</li> <li>Ability to use spam filter - useful when you debug something every frame</li> <li>Show cpu consuption for update and draw method</li> <li>Have space for player coordinates and additional info visible below messages</li> </ul> <p>Code is non minified and commented. Feel free to ask questions.</p> <p>GIF from <a href="https://www.lexaloffle.com/bbs/?tid=28309">Save Yourself</a> showing how debug looked in game:<br /> <a href="https://twitter.com/dagondev/status/813686173046546432">LINK</a></p> <p>Edit: occured to me I could add ability to change how much of a screen debug messages take. I will add this in a update, when I get some time to do that...</p> https://www.lexaloffle.com/bbs/?tid=28319 https://www.lexaloffle.com/bbs/?tid=28319 Tue, 27 Dec 2016 04:35:48 UTC Save Yourself v1.0c <p> <table><tr><td> <a href="/bbs/?pid=34213#p"> <img src="/bbs/thumbs/pico34857.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=34213#p"> Save Yourself 1.0c</a><br><br> by <a href="/bbs/?uid=15064"> dagondev</a> <br><br><br> <a href="/bbs/?pid=34213#p"> [Click to Play]</a> </td></tr></table> </p> <p><strong>Save Yourself v1.0c</strong></p> <p>Short adventure game with unique talking mechanic with focus on exploring and talking to NPCs. Meant to be played several times to learn the world and find clues. Game have random elements to throw player off from obvious resolution.<br /> As it was my first released game it isn't very player friendly and is very obtuse in what it requires from you.</p> <p><strong>One run length: 5-20 minutes</strong></p> <p><strong>INTRO:</strong><br /> You are one of many bounty hunters that is after Maggus, the royal mage, after you found King will pay handsomely for this task.<br /> Allied mercenary lead you to enclosed, old, valley mine, where Maggus could hide.<br /> This is your chance, there is only one exit from this place and your companion is guarding it.<br /> There is only one catch, you have only two days - King will pay three times more if you deliver your bounty within specified time.</p> <p><strong>CHANGELOG:</strong><br /> <strong>1.0c:</strong></p> <ul> <li>semi hotfix for bug reported in this <a href="https://www.lexaloffle.com/bbs/?pid=34398#p34398">post by Sibwara</a> (even though character can still stuck in ground, it will always have ability to climb back above ground)<br /> <strong>1.0b:</strong></li> <li>hotfix for not working ladders, reported in this <a href="https://www.lexaloffle.com/bbs/?pid=34846#p34846">post by orangecode</a><br /> <strong>1.0a:</strong></li> <li>fixed bug reported in this <a href="https://www.lexaloffle.com/bbs/?pid=34398#p34398">post by Sibwara</a></li> <li>restored acknowledging (<strong>sometimes</strong>) player making statement by npcs<br /> <strong>1.0:</strong></li> <li>Initial release</li> </ul> <p><strong>LICENSE:</strong><br /> You have my permission to use/modify any fragments of the code provided, crediting is nice but not required.<br /> You DON'T have my permission to use any music or graphics used in this game.</p> <p><strong>SOURCE CODE:</strong><br /> Non-minified version with comments: <a href="https://bitbucket.org/dagondev/save-yourself/src">BitBucket repository page</a></p> <p><strong>DOWNLOADABLE HTML5:</strong><br /> At Itch.io: <a href="https://dagondev.itch.io/save-yourself">Itch.io page</a></p> <p><strong>SOUNDTRACK:</strong><br /> At <a href="https://www.youtube.com/watch?v=U1r-s8MTNiQ&amp;amp;list=PLKoU8E666FlQF2VqvH2OI_6TGPd0yS689">Youtube</a> or <a href="https://soundcloud.com/user-385396989/sets/save-yourself-ost">Soundcloud</a>.</p> <p><strong>Any feedback or just sharing your experience is encouraged.</strong></p> <p><strong>ACKNOWLEDGEMENTS:</strong><br /> Data composition based on, collision system, snow and clouds rendering taken from, game: <a href="https://www.lexaloffle.com/bbs/?tid=2145">celeste by matt thorson + noel berry</a></p> <p>String data storage, and functions for those, based <a href="https://www.lexaloffle.com/bbs/?tid=28160"> on this post.</a></p> https://www.lexaloffle.com/bbs/?tid=28309 https://www.lexaloffle.com/bbs/?tid=28309 Mon, 26 Dec 2016 11:44:32 UTC Let's discuss - Why token count favors ugly code so much <p>Hello, I am hitting token count with my game so often - it is irritating. I feel like I am fighting a war of having clean code vs having less tokens.<br /> (I understand that pico8 is for small games and first step should be cutting features though)<br /> I am not sure if something is wrong or am I expecting too much and trying to use pico8 like modern framework.</p> <p>In the docs is stated:<br /> <strong>One program can have a maximum of 8192 tokens. Each token is a word (e.g. variable name) or operator. Pairs of brackets, and strings count as 1 token. commas, periods, LOCALs, semi-colons, ENDs, and comments are not counted.</strong></p> <p>Let's look at this code:<br /> (taken from my game, but shortened and altered - don't expect this to work! <strong>I am not saying that provided code is greatest piece of clean, descriptive code!</strong>)</p> <p>It draws (on local space - screen) piece of text, npc will say to the player - after delay, with black background to ensure readability.</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> draw_say=function(this,x,y) x=this.x-x y=this.y-y palt(0,false) foreach(this.draw_message_queue,function(obj) local deltatime=gl_app_time.timeelapsed-obj.time local text_len=#obj.m local text_size=text_len*2 local text_time=text_len/4.0 local x1=x-text_size-2 local x2=x+text_size+2 local y1=y-12 local y2=y-4 if deltatime&lt;text_time then rectfill(x1,y1,x2,y2,0) print(obj.m,x1,y1+2,obj.color) end end) palt() 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><strong>This code isn't perfect, there are still magic numbers or non descriptive piece of code like y1=y-12 - what this '-12' means?</strong><br /> But most important things (to me) are explained in local variables like: text_size (how much space string will take on screen) and text_time (how long text should be printed based on length of screen)<br /> <strong>All of this pasted into empty cart gives token count: 100</strong></p> <p>If I change lines:</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> local text_len=#obj.m local text_size=text_len*2 local text_time=text_len/4.0 </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:</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> local text_size=#obj.m*2 local text_time=#obj.m/4.0 </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p><strong>I get token count: 99</strong></p> <p>This wasn't bad change as '#obj.m' still can be quite descriptive (even more if this was '#obj.text'), but shows that using locals is quite wasteful if your value isn't reused more. So going with that logic, let's change more code:</p> <p>If I change lines:</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=this.x-x y=this.y-y local deltatime=gl_app_time.timeelapsed-obj.time local text_len=#obj.m local text_size=text_len*2 local text_time=text_len/4.0 local x1=x-text_size-2 local x2=x+text_size+2 local y1=y-12 local y2=y-4 if deltatime&lt;text_time then </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:</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> local x1=this.x-x-#obj.m*2-2 local x2=this.x-x+#obj.m*2+2 local y1=this.y-y-12 local y2=this.y-y-4 if (gl_app_time.timeelapsed-obj.time)&lt;#obj.m/4.0 then </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 get token count: 95<br /> This comes from removing text_size, text_time and deltatime, but didn't get anything from removing lines 'x=this.x-x' and 'y=this.y-y'. (which make x and y local space btw - yep bad naming from my part)</p> <p>Code isn't so bad, yet, but let's compress this even more:</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> draw_say=function(this,x,y) palt(0,false) foreach(this.draw_message_queue,function(obj) local x1=this.x-x-#obj.m*2-2 local y1=this.y-y-12 if (gl_app_time.timeelapsed-obj.time)&lt;#obj.m/4.0 then rectfill(x1,y1,this.x-x+#obj.m*2+2,this.y-y-4,0) print(obj.m,x1,y1+2,obj.color) end end) palt() 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 now we have <strong>token count: 89!</strong><br /> (Have fun decoding this if you didn't wrote this/came back after long break.)</p> <p>11 tokens.. now we are talking!<br /> But why the f*k you only removed x2 and y2? Because those are only used once. But lets remove all those:</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> draw_say=function(this,x,y) palt(0,false) foreach(this.draw_message_queue,function(obj) if (gl_app_time.timeelapsed-obj.time)&lt;#obj.m/4.0 then rectfill(this.x-x-#obj.m*2-2,this.y-y-12,this.x-x+#obj.m*2+2,this.y-y-4,0) print(obj.m,this.x-x-#obj.m*2-2,this.y-y-12+2,obj.color) end end) palt() 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 get <strong>token count: 99!</strong> </p> <p><strong>So this shows that if you have more than one operation and you reuse it at least once, you should use variables for that.</strong></p> <p>You may ask, well isn't X tokens worth more readability? Answers is yes, but this is only one function... This starts to add up when you have 50 functions (50x11=550 saved tokens!) and you hit token count and want to finish feature or at least run your game to debug... And saving means that your code will get more and more ugly.</p> <p>The thing that irritate me, even more than non descriptive code, is the inconsistency in such actions.</p> <p><strong>I think there is a rule of thumb: if you use some number (especially with operator and variable) more than 3 times it is worth it to put it in separate variable. If you have few operations for one variable, use locals if you use result more than once. If you have even more operations use variables.</strong></p> <p>Which leads to functions that sometimes have local variables and sometimes don't and sometimes have only some variables for magic numbers...</p> <p>So what do you think? Is this is me trying use pico8 wrong?</p> <p>P.S. I understand that pico8 is modelled after old hardware, but do we seriously need to make everything hard because of old times? Embedded code editor, while still pain to use, is reminiscent of old editors and somehow gets modern features to make writing code simpler - why can't this happen with token count as well?</p> <p>P.P.S. Ability to run game with having more tokens would also allow me to write clean code before I finish everything. Which means I would just refactor code before release. This isn't perfect, but would be good start.</p> https://www.lexaloffle.com/bbs/?tid=28177 https://www.lexaloffle.com/bbs/?tid=28177 Fri, 09 Dec 2016 03:06:13 UTC