Lozenge [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=121927 Fixing Attempt to Index Global &quot;Player&quot; Error <p>Hi, I'm very new to Pico-8 and coding in general. I've been following some tutorials on how to work in Pico-8, which go over basic elements using separate projects. In order to further my understanding of how the pieces fit together, I wanted to try to incorporate each tutorial into one project. I'm having some trouble though, because my code throws an error when I try to refer to my player class.</p> <p>This is the code I have in Tab 0, the original code to set up movement and directional sprites for the player:</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 _init() player={ x=63, y=63, fx=false, fy=false, sp=1, speed=1 } end function _update() --player movement-- if btn(➡️) then player.x+=player.speed player.sp=9 player.fx=false player.fy=false end if btn(⬅️) then player.x-=player.speed player.sp=9 player.fx=true player.fy=false end if btn(⬇️) then player.y+=player.speed player.sp=1 end if btn(⬆️) then player.y-=player.speed player.sp=5 end if mget(flr((player.x+4)/8),flr((player.y+4)/8))==33 then player.speed=1.2 else player.speed=1 end end function _draw() cls(3) map() spr(player.sp,player.x, player.y,1,1,player.fx, player.fy) print(mget(flr((player.x+4)/8),flr((player.y+4)/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>That all works fine, but when I add code from the animation tutorial into Tab 1, it throws<br /> <strong>RUNTIME ERROR LINE 15 TAB 1<br /> IF PLAYER.SP&lt;LAST_FRAME THEN<br /> ATTEMPT TO INDEX GLOBAL 'PLAYER'<br /> (A NIL VALUE)</strong></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 _init() stimer=0 ani_speed=10 first_frame=1 last_frame=4 end function _update() --player animation-- if stimer &lt; ani_speed then stimer+=1 else if player.sp &lt; last_frame then player.sp+=1 else player.sp=first_frame end stimer=0 end end function _draw() 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>(There aren't spaces before and after my '&lt;' signs in the code, I added those so it would display correctly here.)</p> <p>For some reason, it doesn't recognize the class I defined. I tried putting the code into Tab 0 instead, to the same effect. What am I doing wrong here? I was under the impression that defining variables under a class allows you to change those variables by referencing as <strong>CLASS.VAR</strong>. Any advice would be appreciated.</p> https://www.lexaloffle.com/bbs/?tid=148485 https://www.lexaloffle.com/bbs/?tid=148485 Fri, 18 Apr 2025 01:44:22 UTC