Well you are using objects in that function, but OOP can be a lot more
Basically the cool thing about OOP is that you can write classes which can create tons of objects who have all the same code (so to speak), but are independent from each other.
Here's a simple class:
Code: Select all
local Ball = {}
function Ball.new()
local self = {}
self.x, self.y = love.math.random( 0, 100 ), love.math.random( 0, 100 )
self.speed = love.math.random( 10, 80 )
function self:update( dt )
self.x = self.x + dt * self.speed
self.y = self.y + dt * self.speed
end
return self
end
return Ball
You can create a bunch of balls and each of them will have its own x and y location:
Code: Select all
function love.load()
-- Create a bunch of balls.
balls = {
Ball.new(),
Ball.new(),
Ball.new(),
Ball.new(),
Ball.new()
}
end
function love.update( dt )
for i, v in ipairs( balls ) do
v:update( dt )
end
end
function love.draw()
for i, v in ipairs( balls ) do
love.graphics.circle('line', v.x, v.y, 5, 8)
end
end
The benefits should be clear: We only had to write the code for once and all the balls can use it. But that's just scratching the surface. One of the other really cool things is inheritance. It basically means that you can hand down functionality from a parent to its kid. Let's extend the example from above. We now want to have squares and balls. Obviously squares also need to know their location in the world and move around BUT they certainly need to be drawn differently. Let's move the relevant to a "parent" class:
Code: Select all
local GeometryObject = {}
function GeometryObject.new()
local self = {}
self.x, self.y = love.math.random( 0, 100 ), love.math.random( 0, 100 )
self.speed = love.math.random( 10, 80 )
function self:update( dt )
self.x = self.x + dt * self.speed
self.y = self.y + dt * self.speed
end
return self
end
return GeometryObject
We can now inherit from that parent and use its functionality without having to write a single line of code:
Code: Select all
local Ball = {}
function Ball.new()
local self = GeometryObject.new() -- Inherit from parent.
function self:draw()
love.graphics.circle('line', self.x, self.y, 5, 8)
end
return self
end
return Ball
Code: Select all
local Square = {}
function Square.new()
local self = GeometryObject.new() -- Inherit from parent.
function self:draw()
love.graphics.rectangle('line', self.x, self.y, 20, 20)
end
return self
end
return Square
Code: Select all
function love.load()
-- Create a bunch of balls.
objects = {
Ball.new(),
Ball.new(),
Ball.new(),
Ball.new(),
Ball.new(),
Square.new(),
Square.new(),
Square.new(),
Square.new(),
Square.new()
}
end
function love.update( dt )
for i, v in ipairs( objects ) do
v:update( dt )
end
end
function love.draw()
for i, v in ipairs( objects ) do
v:draw()
end
end
The cool thing is we can now use squares just like we used balls, but didn't have to rewrite the code. When you want to change the movement code for your objects you won't have to go through every object (squares, rectangles, circles and so on), but instead you just have to change the code in the parent class. Also note how each object now knows how to draw itself ( we simply call its draw() function ). Without OOP you'd probably have something like this in your love.draw:
Code: Select all
function self:draw()
love.graphics.rectangle('line', rectangle1x, rectangle1y, 20, 20)
love.graphics.rectangle('line', rectangle2x, rectangle2y, 20, 20)
love.graphics.rectangle('line', rectangle3x, rectangle3y, 20, 20)
love.graphics.rectangle('line', rectangle4x, rectangle4y, 20, 20)
love.graphics.rectangle('line', rectangle5x, rectangle5y, 20, 20)
love.graphics.rectangle('line', rectangle6x, rectangle6y, 20, 20)
end
There is a lot more to the subject and I didn't even explain the code above, but I hope it gives you an idea of what OOP CAN be (then there is also polymorphism and a lot of other cool stuff) and how it could be achieved in Lua. There also is a beginner tutorial on OOP in Lua
here.
I've also heard that Lua is a 'prototype' language and that's different than what C++ is, but don't really understand what that means either.
It basically means that instead of having classes, interfaces and all the other cool features of an object oriented language (like Java) you are basically getting inheritance by cloning and moving around other objects (in the case of Lua this means tables). So Lua isn't an object oriented language by definition, but can be used in an object oriented way nonetheless.