yonn [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=44806 Can someone help me reduce my token count? <p>I'm working on my first PICO-8 game which is also the first time I really got into programming. Now I almost hit the token count but I don't really know how to shorten the code. I also admit I got really careless when writing the code and was mostly just happy whenever it worked. In at least three instances I'm 100% certain there needs to be a much more efficient way to code, any experienced programmer will probably cringe so bear with me.<br /> But I'd really appreciate any help!</p> <p>The first code is a function that adds text to the screen with each sentence delayed.<br /> oprint2() is just a function I copied from Sophie Houldens talkthrough of Curse of Greed which adds an outline to the text.<br /> Right now there are six texts but I want this function to work with an even greater number of texts and to able to make changes without so much hussle!<br /> The text content and the if-statements to trigger them are always different.<br /> The text style/colors are always the same. Right now I have individual timer variables are for each text which I realize is horrible. If it helps, I'd have no problem with all texts having the same speed.</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 intro() camera() controls() if ending_init==0 then if help==1 then if game_start_t &lt;= 320 then oprint2(&quot; move\n\n ⬅️ ⬆️ ⬇️ ➡️&quot;,8,24,0,15) end if game_start_t &gt;= 180 and game_start_t &lt;= 460 then oprint2(&quot;\n\n search for hints\n\n ❎&quot;,8,44,0,15) end if game_start_t &gt;= 370 and game_start_t &lt;= 530 then oprint2(&quot;\n\nsearch through our belongings\n\n ❎&quot;,8,64,0,15) end if game_start_t &gt;= 500 and game_start_t &lt;= 700 then oprint2(&quot;\n\n surrender\n\n 🅾️&quot;,8,84,0,15) end if game_start_t &lt;= 710 then game_start_t += 1 end end if p.keys &gt;= 1 and dget(2)!=1 then if key_timer &lt;= 240 then oprint2(&quot; a key&quot;,8,24,0,15) end if key_timer &gt;= 50 and key_timer &lt;= 270 then oprint2(&quot;\n\n opens either a chest\n\n or a door&quot;,8,34,0,15) end if key_timer &gt;= 120 and key_timer &lt;= 300 then oprint2(&quot; and might only fit once.&quot;,8,82,0,15) end if key_timer &lt;= 310 then key_timer+=1 else dset(2,1) end end if steponfv &gt;= 1 and dget(3)!=1 then if stepon_timer &lt;= 240 then oprint2(&quot;every small decision&quot;,8,24,0,15) end if stepon_timer &gt;= 50 and stepon_timer &lt;= 270 then oprint2(&quot;\n\n will\n\n influence&quot;,8,34,0,15) end if stepon_timer &gt;= 120 and stepon_timer &lt;= 300 then oprint2(&quot; our fate.&quot;,8,82,0,15) end if stepon_timer &lt;= 310 then stepon_timer+=1 else dset(3,1) end end if musx == 7 and musy == 3 then if restart_timer &gt;= 350 and restart_timer &lt;= 600 then oprint2(&quot;the heart of the desert&quot;,8,24,0,15) end if restart_timer &gt;= 700 and restart_timer &lt;= 1000 then oprint2(&quot;\n\nwhere everything ends\n\n and everything begins ...&quot;,8,34,0,15) end if restart_timer &gt;= 1100 and restart_timer &lt;= 1400 then oprint2(&quot;we came so far this time&quot;,8,54,0,15) end if restart_timer &gt;= 1500 and restart_timer &lt;= 1800 then oprint2(&quot;are you ready\n\n to unlearn everything\n\n again ...?&quot;,8,74,0,15) end restart_timer+=1 if restart_timer &gt;= 1980 then poke(rnd(23800),rnd(0x100)) end end if musx == 1 and musy == 0 and hiddendoorv == 0 and greedv == 0 and steponv == 0 and steponfv == 0 and textreadv==0 and ending_i==0 then if east_timer &gt;= 50 and east_timer &lt;= 250 then oprint2(&quot;this is not the empty room ...&quot;,8,24,0,15) end if east_timer &gt;= 200 and east_timer &lt;= 300 then oprint2(&quot;we shall head east\n\n and ascend!&quot;,8,44,0,15) end if east_timer &lt;= 310 then east_timer+=1 end end if musx == 4 and musy == 0 and hiddendoorv != 0 and textreadv !=0 then if empty_timer &gt;= 30 and empty_timer &lt;= 120 then oprint2(&quot;my mind is restless&quot;,8,24,0,15) end if empty_timer &gt;= 140 and empty_timer &lt;= 220 then oprint2(&quot;the noise is unbearing&quot;,8,44,0,15) end if empty_timer &lt;= 230 then empty_timer+=1 end end 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>The second code teleports the player from certain map tiles to others when stepped on.<br /> Sometimes it works two ways but then it changes to one tile in either x or y to prevent a loop.<br /> My problem is again that the if-statements are always different so I don't know how to group them together and sometimes there are multiple if-statements or they require different variables than p.x and p.y.</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 desert_teleport() if ending_init== 0 then if greedv &gt;= greed_one then if p.x == 1 and p.y == 1 then p.x = 49 p.y = 1 end elseif hiddendoorv &gt;= hiddendoor_one then if p.x == 4 and p.y == 6 then p.x = 88 p.y = 54 end elseif textreadv &gt;= textread_one then if p.x == 79 and p.y == 60 then p.x = 121 p.y = 25 end end if (p.x &gt;= 112 and p.x &lt;= 127 and p.y == 47) or (p.y &gt;= 16 and p.y &lt;= 47 and p.x == 127) then p.x = flr(rnd(14)) + 97 p.y = flr(rnd(14)) + 34 elseif (p.y &gt;= 16 and p.y &lt;= 32 and p.x == 112) or (p.x &gt;= 96 and p.x &lt;= 111 and p.y == 32) then p.x = flr(rnd(14)) + 113 p.y = flr(rnd(14)) + 34 elseif (p.y &gt;= 32 and p.y &lt;= 63 and p.x == 96) or (p.x &gt;= 96 and p.x &lt;= 111 and p.y == 63) then p.x = flr(rnd(14)) + 113 p.y = flr(rnd(14)) + 17 elseif (p.y &gt;= 2 and p.y &lt;= 13 and p.x == 31) then p.x = flr(rnd(12)) + 65 p.y = flr(rnd(12)) + 1 elseif p.x == 79 and p.y == 60 then p.x = 17 p.y = 37 elseif p.x == 16 and p.y == 37 then p.x = 78 p.y = 60 elseif p.x == 35 and p.y == 15 then p.x = 89 p.y = 49 elseif p.x == 89 and p.y == 48 then p.x = 35 p.y = 14 elseif p.x == 40 and p.y == 0 then p.x = 99 p.y = 30 elseif p.x == 99 and p.y == 31 then p.x = 40 p.y = 1 elseif p.x == 6 and p.y == 63 then p.x = 107 p.y = 17 elseif p.x == 107 and p.y == 16 then p.x = 6 p.y = 62 elseif p.x == 92 and p.y == 31 then p.x = 19 p.y = 30 elseif p.x == 19 and p.y == 31 then p.x = 92 p.y = 32 elseif p.x == 0 and p.y == 10 then p.x = 110 p.y = 22 elseif p.x == 111 and p.y == 22 then p.x = 1 p.y = 10 elseif p.x == 16 and p.y == 28 then p.x = 110 p.y = 25 elseif p.x == 111 and p.y == 25 then p.x = 17 p.y = 28 elseif p.x == 56 and p.y == 0 then p.x = 40 p.y = 42 elseif p.x == 79 and p.y == 41 then p.x = 1 p.y = 38 elseif p.x == 0 and p.y == 38 then p.x = 78 p.y = 41 elseif p.x == 47 and p.y == 17 then p.x = 1 p.y = 56 elseif p.x == 0 and p.y == 56 then p.x = 46 p.y = 17 elseif p.x == 41 and p.y == 31 then p.x = 39 p.y = 49 elseif p.x == 39 and p.y == 31 then p.x = 88 p.y = 1 elseif p.x == 38 and p.y == 48 then p.x = 40 p.y = 30 elseif p.x == 40 and p.y == 48 then p.x = 69 p.y = 46 elseif p.x == 89 and p.y == 63 then p.x = 118 p.y = 57 elseif (p.x &gt;= 16 and p.x &lt;= 31 and p.y == 0) or (p.x &gt;= 97 and p.x &lt;= 110 and p.y == 0) then p.x = 88 p.y = 10 end end if ending_init==1 then if p.x == 0 or p.x == 127 or p.y == 0 or p.y &gt;= 63 then p.x = flr(rnd(125)) + 2 p.y = flr(rnd(61)) + 2 end 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>The last function switches out certain maptiles with others if some conditions are met. I have a lot of different tables and functions of this kind but for brevity I'll just post two.<br /> Compared to the first two I think they are less awful but I still feel they could be optimized.</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>trap_table ={109,109,109,79,79,79,79,79,49,50,24,24,25,25,40,40,41,41,96,96,112,112,75,76,91,115,117,117,100,101} function place_traps() for a=0,127 do for b=0,63 do if mget(a, b) == 109 then mset(a, b, trap_table[flr(rnd(#trap_table))+1]) end end end end function place_corrupt() for a=0,127 do for b=0,63 do if mget(a,b)==79 or mget(a,b)==5 or mget(a,b)==14 or mget(a,b)==15 or mget(a,b)==46 or mget(a,b)==47 then mset(a,b,trap_table[flr(rnd(#trap_table))+1]) elseif mget(a,b)==17 or mget(a,b)==52 then mset(a,b,106) elseif mget(a,b)==18 or mget(a,b)==88 then mset(a,b,107) elseif mget(a,b)==19 then mset(a,b,122) elseif mget(a,b)==77 then mset(a,b,83) elseif mget(a,b)==78 then mset(a,b,84) elseif mget(a,b)==66 then mset(a,b,92) elseif mget(a,b)==82 then mset(a,b,108) elseif mget(a,b)==67 or mget(a,b)==68 or mget(a,b)==85 or mget(a,b)==74 or mget(a,b)==98 then mset(a,b,122) elseif mget(a,b)==56 then mset(a,b,93) elseif mget(a,b)==12 or mget(a,b)==13 or mget(a,b)==127 then mset(a,b,flr(rnd(2))+126) elseif mget(a,b)==28 or mget(a,b)==29 or mget(a,b)==8 then mset(a,b,flr(rnd(2))+7) elseif mget(a,b)==53 or mget(a,b)==54 or mget(a,b)==124 then mset(a,b,flr(rnd(2))+123) end end 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>Sorry for so much text but I thought I'd just post them 1:1 like they are in the game.<br /> Thanks for any help!</p> https://www.lexaloffle.com/bbs/?tid=38582 https://www.lexaloffle.com/bbs/?tid=38582 Sat, 27 Jun 2020 17:19:57 UTC