Object Oriented Programming on Roblox

What is this tutorial about?

In this tutorial, I will talk about OOP and how you can create classes in Lua.
I’ll start by explaining what OOP is and good use cases for OOP, then move on to actual creating the code.

Before we begin, please read the following:

What is a struct?

A struct is simply a structure.

Everyone has used objects before, right?
An object is a structure, for example.

A struct is simply a template for an object.

What is a class?

A class is a form of structure. It’s a constructor which can create a template object for you. A class can have methods and variables, as well as private methods and variables.

Private methods/variables are things that can only be accessed by the class, but not externally.

Classes are helpful because they make creating objects easier. If we had an object with a lastname, firstname and age property, we could use a class to create a template for all of these properties.

Plan:

  • What is OOP?
  • What are use cases for OOP?
  • How can we implement OOP?

This is a fairly short tutorial, and it shouldn’t take too long for you to read it.
If you find this tutorial useful, let me know so I can create more tutorials.

Part 1: What is OOP?

Object oriented programming is a style of programming. Object oriented programming (or OOP) is something you find most often in high-level languages, like Javascript. We’re focusing on classes in this tutorial. Unfortunately, Lua doesn’t have an easy way of creating classes. This style of programming is geared towards using objects. Hence, ‘object oriented programming’.

Part 2: What are use cases for OOP?

Object oriented programming is very helpful for quite a lot of things. For example, if we had a list of users, we need a sort of template to create it, like C++'s struct. But… if we need to add functions to that struct, we need classes. Let’s say that each user looked like this:

{
   firstname: 'John',
   lastname: 'Doe',
   age: 54
}

If we typed that out for each user, it would be inefficient and slow.

Part 3: Implementation

Structs

Structs are incredibly simple to implement.
All we need is a function which returns a newly allocated object.

Like this:

local createUser = function (first, last, age)
	local alloc = {}
	alloc.firstname = first
	alloc.lastname = last
	alloc.age = age
	return alloc
end
Classes

Classes are little bit more complicated.
We could use the same method as structs, but there’s a much better way.

The reason that the other method isn’t the greatest is because we need a self property. That method also makes it much harder to create functions for the class.

The self property is a reference to the current object. In Javascript, it’s called this.
For example, let’s say I had object a with a method hello on it. If we call hello with the :hello() syntax, it will pass in a as the first argument. Put simply, it is the referenced object.

What is a metatable?

A metatable is much like a proxy.
It ‘intercepts’ requests to an object. For example, if I wanted to ‘intercept’ a ‘get’ to a value, I could add the __index object/method. It’ll have arguments like name which we could run operations on and decide a return value. Same goes for set.

How will we implement this?

  • First, we’ll create a base metatable
  • Second, we’ll create a ‘new’ function which creates an instance of that base table
  • Third, we’ll create our our properties on the new instance
  • Last, we’ll return our new table

We can start by creating our base table.

local base = {}
base.__index = base;

What we’re doing is telling our base metatable to reference itself. That way, we can create methods on the base that will work for all instances.

Next, we need to create our ‘new’ function.

function module.new(first, last, age)
	
end

So far, it’s just a simple function.
Let’s create a new table inside of that function now.

local alloc = {}
alloc.firstname = first
alloc.lastname = last
alloc.age = age

Alright, we have our table. But how can we make it an instance of ‘base’?

Remember how ‘base’ is supposed to be a metatable?
We can use the setmetatable method which is built-in to Lua.
It takes in an object and a metatable.

Now, let’s add our setmetatable method.

return setmetatable(alloc, base)

Pretty simple, right?
We’re pretty much done, but at this state, it’s pretty redundant. This is pretty much a struct.

To add a class method, we simply add a method to base.
Let’s say we wanted to add a getfullname method. This method will return firstname + ' ' + lastname.

We need to use the :Function syntax rather than the .Function syntax so we get the self property.

function base:getfullname()
	return self.firstname .. ' ' .. self.lastname
end

Now we can test it using our new method.
In fact, Roblox is smart enough to tell that the result from the new function has the getfullname property.

Now, if we do something like this:

local Jack = person.new('Jack', 'Will', 32)
local Mike = person.new('Michael', 'Jackson', 43)
local Alex = person.new('Alexander', 'Williams', 21)

print(Jack:getfullname())
print(Mike:getfullname())
print(Alex:getfullname())

Our output will be:

Jack Will
Michael Jackson
Alexander Williams

Full code:

local person = {}

local base = {}
base.__index = base

function person.new(first, last, age)
	local alloc = {}
	alloc.firstname = first
	alloc.lastname = last
	alloc.age = age
	return setmetatable(alloc, base)
end

local Jack = person.new('Jack', 'Will', 32)
local Mike = person.new('Michael', 'Jackson', 43)
local Alex = person.new('Alexander', 'Williams', 21)

print(Jack:getfullname())
print(Mike:getfullname())
print(Alex:getfullname())

All of what we just did is equivalent to this Javascript code:

class Person {
	constructor(first, last, age) {
		this.first = first;
		this.last = last;
		this.age = age;
	}
	getfullname() {
		return this.first + ' ' + this.last;
	}
}

const Jack = new Person('Jack', 'Will', 32);
const Mike = new Person('Michael', 'Jackson', 43);
const Alex = new Person('Alexander', 'Williams', 21);

console.log(Jack.getfullname());
console.log(Mike.getfullname());
console.log(Alex.getfullname());

That’s all!

Now you know how OOP works, congrats! I look forward to seeing what creations you come up with.

9 Likes

There’s quite a lot of flaws in this post.

TL;DR: I would not recommend any newbies to this tutorial. Instead, I would recommend checking out this detailed and comprehensive OOP tutorial here:

By telling the viewers “is a style of programming”, I’d like saying cooking is a style of action. The viewers wouldn’t understand what you meant by that. OOP should be defined as a style of programming which revolves around objects. For example, think of any single object you’ve seen in your life, a cat, a cat, a house, a water bottle and etc. OOP focuses on these objects because it focuses on their property, what they do and the condition/state of each object.

Your Part 2 just adds even more confusion to viewers as you’re using complex scripting terms and words in front of newbies to OOP. You should’ve used a better and understandable example like “imagine you want to create thousand of car instances in your game, of course you can duplicate every single script and car model but that’s just a wasteful of time and spacing and more unprofessionalism. OOP helps us to overcome this issue because all we need to do is just create some functions, use that function to create a car whilst giving unique properties in an ease!”

Again, adding even more confusion to your viewers. You should explain completely what is a struct. If I was to explain what is it, I would just use the term constructor instead in which what it does is to create an object.

Other confusing terms you use in this tutorial such as:

You didn’t define what is a class.

What is self?

Again, you used complex terms to not make the viewer understand.

In conclusion, your tutorial is badly written and I wouldn’t recommend anyone to read this tutorial. You didn’t explain it well as like the other users who created this tutorial.

2 Likes

That is very harsh.
Then again, everyone needs a little bit of criticism.

I’ve updated my post to account for everything you’ve said. I’ve never created a tutorial on here before (at least not that I can remember), so please, be a little bit nicer.

2 Likes

Again, your tutorial doesn’t explain much, you’re confusing your readers and expect them to know what it means when you’re clearly putting words that they don’t understand in front of them.

1 Like

Agreed, @ItzMeZeus_IGotHacked could have been a lot more tactful with how they wanted to share constructive criticism. That said, there are valid points.

If this is your first attempt at a tutorial #1 good first try, keep at it, don’t stop and #2 thanks for trying to further the knowledge of the community by sharing what you know and putting yourself out there.

@ItzMeZeus_IGotHacked provided good points to clarify on and expand upon in your tutorial. I recommend you go back and add some of those yourself, perhaps expanding upon them and clarifying in a way that makes sense with the points you are trying to get to.

There is a lot that can be said about OOP (same goes for many topics), so there’s space to drill into still, even if other tutorials already exist. It’s going to big to be very specific about who your audience is and what you expect them to know going into it. If you expect this to be a very fundamental OOP tutorial, then you need to write with the expectation that your reader (be they a DevForum member or a random person searching this up on the internet) has 0 knowledge and then edit/revise with that in mind. If you are expecting them to have some background in OOP already, that’s fine too, but that’s going to dictate the direction, flow, word choice, and content in the rest of the tutorial. Be mindful too about what’s already out there, too. Try to find something that the other tutorials miss, breeze over, don’t expand enough upon, etc. so that you can really make sure your reader is getting the most out of the tutorial and using their time wisely.

Good luck and looking forward to reading the revised version and further tutorials you have to share :slight_smile:

5 Likes

A more brief, digestible explanation of object oriented programming. It takes skill to compact information while still being clear. Good work keep it up!

2 Likes