Log In  

people often ask about object orientation (OOP) and inheritance, but can get confusing or incomplete guides. I want to help everyone understand how prototypes work!

lua does support object-oriented programming (but does not require it), based on prototype inheritance — not classes.
metatables are part of this but also often misunderstood. this is a complete explainer that I wrote on discord and keep not putting here in a clean way. so I am putting it here as it is!

so what is a prototype?

goblin={
 sp=42,
}

function goblin:new(x,y)
 local obj={x=x, y=y}
 return setmetatable(obj, {__index=self})
end

function goblin:draw()
 spr(self.sp,self.x,self.y)
end

gob1=goblin:new(40,40)
gob2=goblin:new(60,70)

alright so this is like 6 different things working together.

1) table with methods: goblin is a table with property sp and methods new and draw (a method is a function attached to an object)

calling goblin:new(40,40) is the same as goblin.new(self,40,40); self is «the object we are working on»; defining function goblin:draw() is the same as function goblin.draw(self) and the same as goblin={sp=42, draw=function(self) spr(...) end, somethingelse=true}

there is a difference between the two methods in my example: when we call goblin:new, the self param inside it will be the goblin table itself (the prototype), but when we have gob1:draw() then self inside draw will refer to gob1.

2) metatables. there are special methods that lua uses to implement operators (+, /, .., etc). the method for addition for example is __add (we call these metamethods). if I want to be able to do vector1 + vector2for my custom vector tables for example, I need to define a method named __add but I can’t do it in the vector prototype table, lua will not look it there; it is required to define these metamethods in a table’s metatable. (the metatable is similar to a class in other object-oriented languages: it is the template, or the definition, or the model for many tables.) so that’s what happens in goblin:new: I define a metatable with one metamethod and set it on my goblin object.

3) prototype. these are great! if I have 50 goblins on screen and they have different health and coordinates but the same properties for sprite, width, height, etc, and the same methods for update, attack, draw, etc, I would like to define all the shared things in one place, then have custom things in each goblin table. that’s what a prototype is for! here in goblin example we have sp and draw shared, and in a specific goblin table (it starts as obj on line 6, then it’s gob1 or gob2 in my example) we have specific data like x and y.
much more info on this really useful site: https://gameprogrammingpatterns.com/prototype.html

alright so how do I say «gob1 and gob2 should delegate to goblin prototype for some properties»? I define a metatable with an __index property. it is called by lua when I do gob1.sp and sp is not found inside gob1. it can be a function (can be useful to have a dynamic property that’s computed from other properties) or another table: the prototype!
more: https://www.lua.org/pil/13.4.1.html

now this is the cool thing with prototype-based object-oriented language that you don’t have in such a nice way with class-based OO languages
what if I want 40 goblins on screen and 10 archer goblins? these should have a different sprite and different attack method.

archer=goblin:new()

archer.sp=52

function archer:attack(target)
 --use the bow
end

arcgob1=archer:new(20,50)
arcgob2=archer:new(40,70)

look at this from bottom to top:

  • arcgob1.x will be 20, y 50
  • arcgob1’s and arcgob2’s prototype is archer, because when we call archer:new, self on line 6 of my first code block points to archer
  • arcgob1.sp is 52 (the value comes from the prototype)
  • arcgob1:attack(hero) will use our archer:attack method.
  • arcgob1:draw() will use goblin:draw! archer itself is a table that has goblin for prototype
    draw is not found in arcgob1, look at its prototype archer, not found, look at its prototype goblin, found! call it with self pointing to arcgob1.

result:

the confusion is that we often see code like this to explain prototypes (or «class», but lua doesn’t have classes):

goblin={sp=42}

function goblin:new(obj)
 setmetatable(obj, self)
 self.__index=self
 return obj
end

why do we need to add goblin.__index = goblin?? it’s its own prototype? but it’s a metatable? both?!
the thing is, we don’t need that. a metatable is only strictly required to implement metamethods to define what operators do. when we want a prototype, {__index=proto} is the metatable, and proto the prototype. clarity at last!)

hope this helps! please ask follow-up questions if needed, try to implement your own cart and share it!

P#147311 2024-04-25 16:35

this guide is complementary for sequence tables (to have multiple goblins and update/draw all of them easily): https://www.lexaloffle.com/bbs/?tid=44686

P#147384 2024-04-26 16:28
1

Hey Merwok, just wanted to thank you for taking the time to make this post!

I'm a fairly new Pico-8/Lua user and this is exactly what I was looking for regarding class inheritance! I will be applying it!

P#147655 2024-04-30 17:26

[Please log in to post a comment]