Another way to go about it is to use a table like so (using the example of a car):
Car = {}
Car.new = function(miles,gas,health)
local self = {}
self.miles = miles or 0
self.gas = gas or 0
self.health = health or 100
self.repair = function(amt)
self.health = self.health + amt
if self.health > 100 then self.health = 100 end
end
self.damage = function(amt)
self.health = self.health - amt
if self.health < 0 then self.health = 0 end
end
return self
end
It creates a table called 'Car' which would be the equivalent of a class, not an instance, then it defines a method "new" in the Car class which returns an instance of a car with variables and functions. An example to using this implementation:
local myCar = Car.new()
print(myCar.health)
myCar.damage(148)
print(myCar.health)
myCar.repair(42)
print(myCar.health)