I'll give it a go, you weren't very specific with the problem but I'll tell you the same things I tell most people I'm trying to teach OOP to.
Code: Select all
--Let's make a class library--
--We first make a table as our base class object..
local class = {}
--Then we give it an extension function, this is what you call to make your new class.
--The setmetatable function is what ties the new class to the class library and any sub classes you make
function class:extend(subClass)
return setmetatable(subClass or {}, {__index = self})
end
--Then add a constructor function, this function is what you call to make a new copy of your object think of it like a big rubber stamp that stamps out objects.
--Notice the (...) at the end this will take any arguments you pass the function and pass them straight to the objects init function...
function class:new(...)
local copy = setmetatable({}, {__index = self})
return copy, copy:init(...)
end
--The init function you would write for most objects themselves because it is where you set up your objects.
--I put one here so it won't crash if I forget to write one
function class:init(...) end
--Always remember to return your table!
return class
Now we have a class library let's start making some classes...
Code: Select all
--first off require the class library...
local class = require "class"
--Now let's make a shape...
local shape = class:extend() --This is a base class so we don't pass a parent class
--First we write the init function that will be called as the object gets created...
--Notice we don't have to write the function shape:new(...) as that is in the class library and any function calls to shape will
--look in the shape table first and if the function is not found it will look to the parent which in this case is class
function shape:init(x, y, w, h, col)
self:setPosition(x, y)
self:setSize(x, y)
self.col = col
end
--Let's write those functions we just called as well :P
function shape:setPosition(x, y)
self.x, self.y = x, y
end
function shape:setSize(w, h)
self.w, self.h = w, h
end
function shape:getColour()
return self.col[1], self.col[2], self.col[3], self.col[4]
end
--Also something to draw to the screen...
function shape:draw()
love.graphics.setColor(self:getColour())
love.graphics.rectangle("fill", self.x - self.w / 2, self.y - self.h / 2, self.w, self.h)
end
return shape
So our rectangle shape is cool but what if we want a circle?
Code: Select all
--first off require the shape library
local shape = require "shape"
--Start making a circle...
local circle = shape:extend()
--Circles need fewer arguments than the basic shape to define them so let's write a new init...
function circle:init(x, y, rad, col)
self:setPosition(x, y) --We can still use any functions we wrote in shape so our circle will use setPosition() from shape
self.radius = rad
self.col = col
end
--We probably need a new Draw too as rectangles can be a bit too quadrilateral to truly capture the essence of a circle...
function circle:draw()
love.graphics.setColor(self:getColour())
love.graphics.circle("fill", self.x, self.y, self.rad)
end
return circle
So as you can hopefully see from the code above, metatables that we set in the class lib are like an "If you can't find it here then look here" inheritance system. Because shape is a class and circle is a shape, circle can see any functions made in shape OR class so even though we never wrote the function for circle:new we can still call it...
Code: Select all
local circle = require "circle"
function love.load()
myCircle = circle:new(50, 50, 10, {255,255,255,255})
end
function love.draw()
myCircle:draw()
end
This is because it looks for circle:new and can't find one so it checks the metatable and sees that shape is a thing! I'll look there.
Sadly nothing under shape:new either but thanks to shape's metatable I now know class is a thing! Upon looking in class it finds the function new and because of the ":" it knows that the self being referred to in that function is circle without us having to write.
myCircle = circle.new(circle, 50, 50, 10, {255,255,255,255})
So basically using the ":" for our objects instead of the "." means we don't have to write the name twice.
It's important that I stress that I have not run this code, It is written more as a guide and totally off the top of my head but I hope it helps you and your brother.