In this easy tutorial I’m going to show you the absolute easiest way to do Object Oriented Programming on Roblox. The really good thing about this method is that it’s very similar to the way you do OOP in Python, and maybe even simpler! This will be really easy, I promise!
Keep in mind that although this method is likely the easiest, it is not the fastest, as stated by @ffrostfall. So it shouldn’t be used for more complex games with numerous objects of the same class.
I am going to use a person as an example for this tutorial.
First, to create a class, all you need to do is create a function with the name of the class you want to make:
function Person()
end
Now let’s add some attributes! To do this, first create a self
variable which references an empty table. Then, add some attributes by doing self.{attribute name} = {value}
. You can also add parameters for the class in the brackets after Person
:
function Person(name, age)
local self = {}
self.name = name
self.age = age
self.iq = 5
end
Next, let’s create a method for our Person
class. To do this, simply do self.{method name} = {function}
. You can of course add parameters for methods as well:
function Person(name, age)
local self = {}
self.name = name
self.age = age
self.iq = 5
self.IncreaseIQ = function(amount)
self.iq += amount
end
end
Now, the last step is to simply return the self
table:
function Person(name, age)
local self = {}
self.name = name
self.age = age
self.iq = 5
self.IncreaseIQ = function(amount)
self.iq += amount
end
return self
end
That’s it!
Now let’s create a Person
object! To do this, create a variable and set it to the class function and specify the parameters:
local paul = Person("Paul", 37) -- name, age
Now we can do things like getting/setting an attribute or calling a method on the object:
local paul = Person("Paul", 37)
paul.age = 4
paul.IncreaseIQ(7) -- Paul's IQ is now 12!
Inheritance
The last part is how to implement inheritance. I’m going to use Creature
as my superclass and Person
and Animal
as my subclasses which will inherit all attributes and methods from the Creature
class.
The first step is to create a Creature
class as well as a Person
and Animal
class in the exact same way as before. Then, set the self
variable in each subclass to the Creature
class, right below where you initialized the variable.
If you want the superclass to take parameters, simply specify the parameters normally and put an ellipsis (…) as the first parameter for each subclass and inside where you called the superclass:
function Creature(name, age) -- superclass
local self = {}
self.name = name
self.age = age
self.IncreaseAge = function(amount)
self.age += amount
end
return self
end
function Person(..., cash) -- subclass
local self = {}
self = Creature(...) -- inheritance
self.cash = cash
self.iq = 12
self.DecreaseIQ = function(amount)
self.iq -= amount
end
return self
end
function Animal(..., favoritePrey) -- subclass
local self = {}
self = Creature(...) -- inheritance
self.favoritePrey = favoritePrey
self.kills = 0
self.EatPrey = function(preyName)
self.kills += amount
print(self.name.." just ate "..preyName.."!")
end
return self
end
Finally, let’s create objects for our subclasses:
local paul = Person("Paul", 37, 0) -- name, age, cash
paul.DecreaseIQ(10) -- Paul's IQ is now 2!
local charcoal = Animal("Charcoal", 5, "Paul") -- name, age, favorite prey
charcoal.EatPrey("Paul") -- Charcoal just ate Paul!
charcoal.IncreaseAge(2) -- Charcoal is now 6 years old!
Thanks for reading! Please let me know how I can improve this method and if there is a better method.