ghandolhagen [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=69217 Same but different <p>Hello Community!</p> <p>I wonder if any of you can enlighten me as to the under the hood differences of loops that I am encountering.</p> <p>does anyone know why this 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> function iterate(tbl,fnc) for o in all(tbl) do if fnc=='u' then o:update() elseif fnc=='d' then o:draw() elseif fnc=='h' then hit_test(player,o) 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>while this only works for update and draw but not for testing the collision?</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 iterate(tbl,fnc) for i=1,#tbl do local o=tbl[i] if fnc=='u' then o:update() elseif fnc=='d' then o:draw() elseif fnc=='h' then hit_test(player,o) 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>In order for update and draw to work, the program must know that o=tbl[i] is an object with methods. So why does hit_test act like o is nil?</p> <p>Any insight is greatly appreciated.</p> https://www.lexaloffle.com/bbs/?tid=53403 https://www.lexaloffle.com/bbs/?tid=53403 Thu, 13 Jul 2023 22:23:52 UTC While Loop Question <p>Working on trying to understand RLE and I found a simple algorithm which I converted to pico-8 lua. When I run it however it seems like the while loop exits and executes the next line of code every iteration even though the conditions are still being met. I'm not sure what is going on, but I am sure that it is my fault. :D</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>cls() string='wwwwweehhhhhhhjjiiioop' function print_rle(str) local string=str local n=#string local j=0 for i=1,n do local char_count=1 while i&lt;n-1 and sub(string,i,_)==sub(string,i+1,_) do char_count+=1 i+=1 end print(sub(string,i,_)..char_count,8*j,10,7) j+=1 end end print_rle(string)</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=49643 https://www.lexaloffle.com/bbs/?tid=49643 Wed, 05 Oct 2022 01:13:13 UTC