Using _ENV to cut down on "self"

I've been playing around with PICO-8 for a project and came across a bit of code from Kevin Thompson that reassigns the _ENV variable to allow objects to cut down on all the uses of self in code.

I tried it out in a Playdate project and it seems to work! I feel like my code is so much more legible without all the self clutter. I haven't done any serious benchmarking because it's a relatively simple project and haven't run into any problems on device, but I'm wondering if any of y'all can foresee issues with going this route.

I'm also wondering if someone who understands LUA's metatable magic a bit better has ideas for how to incorporate this into the existing Playdate SDK object class so that I could get the benefit of both worlds, or if they are too incompatible?

6 Likes

_env trick is a double edged sword and really useful in pico8 to save tokens.

Not sure forcing that in the sdk would be wise - too easy to trip the user into creating or masking variables.

2 Likes

I wanted to follow up on this ancient thread in case anybody comes to it via google. I decided against using this method because it felt like a constant fight against Lua to make it work, and it was easy to accidentally make a variable global or something screwy. So I don't really recommend it.