Object Oriented Programming in PICO-8 based On Metatables
A comprehensive guide to object-based programming in the Lexaloffle virtual console
Hello my fellow gamedevs!
This time I want to introduce you to a programming concept that could be extremely helpful to create maintainable and human readable code, gamedev is an already complex task and when you can only invest short periods around your full-time job is quite common you open your project after a while and you completely forgot what tha hell you were trying to achieve.
At this pill I want you to understand why OOP is useful and how can we easily implement it on PICO-8
Pro Tip. OOP Paradigm could be applied to other engines like Pyxel, Picotron, Phaser and even Godot!
Alan Kay coined the term “object oriented programming” at grad school in 1966 or 1967. The big idea was to use encapsulated mini-computers in software which communicated via message passing rather than direct data sharing — to stop breaking down programs into separate “data structures” and “procedures”.
Real world rules to understand OOP
Nothing better than everyday language and examples to understand gamedev concepts, in this case we are applying a Coding Paradigm that is widely used, not only for videogames, but to create software and websites.
From my point of view, where it fits better is under videogames, because I can clearly see a natural translation between LUA Objetcs (Metatables) and the entities that we need on our virtual world. My goal at this article is to make it also natural for you.
But… What is an object?
Looking at the human definition of object we can say it is anything that is visible or tangible and is relatively stable in form. A thing, person, or matter to which thought or action is directed.
We can also say objects have properties and behaviors
Let’s start with a simple1 example, something we use on a daily basis.... doors!
Properties. Think about the key aspects that could define an object. Applying it to our door example:
Opened. Could be Yes or no.
Color. Red, blue, brown
Behaviors. Think about the key actions/usage a certain object could have, again looking at our door:
Open. The action to open the door
Close. The action to close the door
Do you want to learn gamedev around your full-time job? This is you place. Subscribe to keep track of small PICO-8 oriented articles to learn fast and effectively.
OOP Applied to PICO-8
I really think applying our real world rules to videogames could help through gamedev, specially when we think about data structures and physics, without further delay… Let’s create our first tiny door on PICO-8!
First of all, I created a cute tiny door for this example using the built-in PICO-8 Pixel Art editor.
Then I’ve defined the Door object under a different tab to keep code and structure organiced
Our door is quite simple, just 40 lines of code and we are including properties and behaviors.
Let’s go through the different important aspects of our Door:
Properties
SP. This is the initial sprite that we will set.
X. This is the X position in the screen
Y. This is the Y position in the screen
OPENED. A boolean value to know if it’s open or closed
Behaviors
Update. Update SP based on OPENED value
Draw. call SPR() function to actually draw the door on screen based on SP value
Open. Set OPENED as true value
Close. Set OPENED as false value
Cool, now writing with a more programming oriented language we have a door with just the variables (properties) and functions (Behaviors) that we need, now we just need to create our first door at the main loop of our PICO-8 project
Simple, easy and clean. You can now apply this concept to any other entity you would need in your game; the player, an obstacle, our just your score system could be also another object of course.
Important. Metatables are quite different than Classes on Python or even LUA. Metatables are just advanced table structures where we can store values and functions so we can emulate the native OOP capabilities of LUA.
Remember LUA for PICO-8 is a minimal implementation and you can’t apply the built-in OOP where we have features like Inheritance and multiple Inheritance
I have a challenge that I’m sure you will be able to complete quickly…
just adding lines at the main loop, are you able to draw more doors in different positions? How would you do that?
Thank you for reading, if you enjoyed this content please share with your family, partner, dogs and cats. 💌
Doors could be somehow painful to implement on Gamedev






