vincent_io [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=34100 Is this the right approach for functions with variable arguments? <p>I have a tile object which contains the sprite number and other properties. I want to update different properties at different times, instead of passing all properties all the time.</p> <p>First I tried this approach:</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 update_tile(values) tile = values 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 of course that unsets anything that you don't pass in <code>values</code></p> <p>Then I settled on the following approach:</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 update_tile(values) for k,v in pairs(values) do tile[k] = v 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>Am I correct that this is the right way to do it, or is there a better (code readability or less tokens) way to do this?</p> https://www.lexaloffle.com/bbs/?tid=37161 https://www.lexaloffle.com/bbs/?tid=37161 Thu, 26 Mar 2020 12:25:19 UTC Iterating over map calling multiple functions <p>Hi,</p> <p>I'm iterating over all map cells multiple times, to do various actions:</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 t_x = 0, 127 do for t_y = 0, 63 do -- scan tiles end end for t_x = 0, 127 do for t_y = 0, 63 do -- do something end end for t_x = 0, 127 do for t_y = 0, 63 do -- place tiles 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>This works, but uses a lot of tokens (do something is actually 4-5 different actions).</p> <p>As there are a lot of interdependencies between cells, I can not do 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>for t_x = 0, 127 do for t_y = 0, 63 do -- scan tiles -- do something -- place tiles 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>Instead, I wanted to do 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>functions = { scan_tiles(t_x, t_y), do_something(t_x, t_y), place_tiles(t_x, t_y), } for f in all (functions) do for t_x = 0, 127 do for t_y = 0, 63 do f(t_x, t_y) 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>But I never get the arguments passed to the functions I call.</p> <p>How to go about this?</p> <p>All help very much appreciated!</p> https://www.lexaloffle.com/bbs/?tid=37108 https://www.lexaloffle.com/bbs/?tid=37108 Fri, 20 Mar 2020 06:57:36 UTC Metatables, rnd() and for loops <p>Hi,</p> <p>I'm using a metatable for particles in my game. For example, when the player dies the sprite explodes in a burst of pixels. I use <em>rnd()</em> to off-set the player position with a random number, and as I generate a lot of different particles (based on the colors the character is composed of) I want to move the for-loop into the constructor.</p> <p>Unfortunately, for some reason the randomness doesn't work when I build the for-loop inside the particle constructor. Compare these two examples:</p> <h3>For loop in caller function</h3> <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>particles={} particle = { x_offset=0, y_offset=0, } function particle:new(o) self.__index = self local pi = setmetatable(o or {}, self) pi.x=pi.x-rnd(pi.x_offset) pi.y=pi.y-rnd(pi.y_offset) add(particles,pi) end function game_over() for i=1,6 do particle:new({x=p.x,y=p.y,x_offset=12,y_offset=8}) 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><strong>Result:</strong> I end up with 6 particles, each with a different <em>x</em> and <em>y</em> value due to the <em>rnd()</em> function being passed the <em>x_offset</em> and <em>y_offset</em> values.</p> <h3>For loop in constructor</h3> <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>particles={} particle = { x_offset=0, y_offset=0, } function particle:new(count,o) self.__index = self for i=1,count do local pi = setmetatable(o or {}, self) pi.x=pi.x-rnd(pi.x_offset) pi.y=pi.y-rnd(pi.y_offset) add(particles,pi) end end function game_over() particle:new(6,{x=p.x,y=p.y,x_offset=12,y_offset=8}) 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>Result:</strong> I end up with 6 particles, but they all have the same <em>x</em> and <em>y</em> values. As if the <em>rnd()</em> is called only once and then applied to the other 5 iterations of the for-loop.</p> <h3>Questions</h3> <ol> <li>Why does the code behave this way?</li> <li>What's the right pattern to apply here?</li> </ol> https://www.lexaloffle.com/bbs/?tid=33327 https://www.lexaloffle.com/bbs/?tid=33327 Tue, 19 Feb 2019 20:41:06 UTC Converting map tiles to a table <h3>Introduction</h3> <p>I'm trying to implement my &quot;map to table&quot; conversion as proposed in my <a href="https://www.lexaloffle.com/bbs/?tid=32916">previous forum post</a>. To this end I created two functions: one for iterating over the map and storing it in a multi-dimensional table (<strong>generate_world()</strong>) and one for iterating over that table and placing the sprites (<strong>build_world()</strong>).</p> <h3>Problem</h3> <p>I get it to nicely iterate through the map, print out the tiles it finds and (presumably) store it in a table. However, when iterating over the table things get awry. I do get it to iterate over the first dimension (when printing it returns <strong>[table]</strong>), but when iterating over the second dimension it's empty (doesn't print anything).</p> <p>All help would be greatly appreciated!</p> <h3>Code</h3> <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 generate_world() world={} t_x=0 t_y=0 max_x=48 max_y=16 print &quot;generating world ...&quot; for x=0,max_x do world[x]={} for y=0,max_y do sp=mget(x,y) if sp != 0 then print(&quot;tile: &quot;..x..&quot;,&quot;..y..&quot;: &quot;..sp) world[x][y] = sp end end end print &quot;done&quot; end function build_world() for x in all(world) do for y in all(x) do print(y) -- this actually never gets executed 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> https://www.lexaloffle.com/bbs/?tid=32919 https://www.lexaloffle.com/bbs/?tid=32919 Wed, 16 Jan 2019 19:46:34 UTC Pick-up items <p>Hey all,</p> <h3>Introduction</h3> <p>I haven't done any development for many years and in general hardly did any game development. Now I'm getting back into it with the awesome Pico-8 and to get familiar with the engine and game dev I'm working on a platformer.</p> <h3>Current progress</h3> <p>The current status is that I have a player character, physics, a (very) basic map and some sound effects. It all works nicely and I'm currently working on items that the player can pick up (power-ups and such). Before moving forward I was hoping to learn some best practices from you all and get some feedback on my current approach.</p> <h3>Approach or implementing items</h3> <p>As I want to have rather large maps with many items I want to place items using the map editor, not by defining them manually in the code (as tweaking during play-testing would be a pita). At the same time I want the items to have some animation <em>before</em> they're picked up and of course an animation when they're picked up as well.</p> <p>My current idea is to scan the map during _init() using <strong>mget()</strong> for these items and then add them to an array which would be scanned and updated during the game loop. The idea is to iterate over the elements in the array and use <strong>mset()</strong> to change the tiles to do the animation before they're picked up.</p> <p>Then I'm using collision detection to detect whether the player is picking up an item and if so play a sound effect + run an animation.</p> <h3>Feedback please</h3> <ul> <li>Does the above approach make sense or is there a better way to achieve this?</li> <li>What are the pros/cons? Are there any alternatives?</li> <li>Wouldn't iterating over the entire map to move the items to an array be too slow?</li> <li>Can I do the loading in chunks based on what's in view? (e.g. using the camera position)</li> </ul> <p>All help is very much appreciated!</p> https://www.lexaloffle.com/bbs/?tid=32916 https://www.lexaloffle.com/bbs/?tid=32916 Wed, 16 Jan 2019 07:58:40 UTC