Roblox OOP (Object Oriented Programming)

Hi,I know it’s been several month since you did this post but can you answer a question that has been bothering me for a while and it’s that why do people use self and why most oop are using it .

Thanks either way :grinning:

2 Likes

Sorry for the late reply, if you haven’t already figured it out by now, self is basically a way to call the table of information you’ve created in your example.New() function.

If you want a more detailed explanation with examples, let me know!

1 Like

I apologize for the late reply (I really outdid myself with that one)

I’d like you to elaborate on what you mean by function overwriting. Like the ability to reconstruct a function and it’s ability from scratch? I’m not too sure what you mean.

2 Likes

If you dont mind answering another question and that is why is oop so important I mean like a table that just contain info about the gun how do people find that useful I just dont get why people use it

1 Like

There is already a tutorial from 2014 about this…


though I appreciate this, I am using OOP now :wink:

3 Likes

You can have not only info but everything you need for your gun.

People will use this because it is a more efficient approach, that’s really it. You don’t have to use OOP at all but it makes it much easier for you as your games get more complicated. Duplicating a gun would not require making a new script, using classes you can make new guns with little effort etc

2 Likes

I cant seem to see any images. Is it my device problem or is it gone?

4 Likes

I can’t seem to see any either, I’m not sure why the images stopped appearing, I’ll try to re-upload the images, Thank you for your patience!

5 Likes

wait how would you use the “powerup” in this case

1 Like

Why people still use OOP while we got module… 4 year I hear that, 4 year I never understand how the hell it can help…

1 Like

It simplifies things and makes them easier to maintain and modify. You can go to one script where that one thing is located, rather than searching through multiple scripts to find it.

1 Like

That what I call a module… It do that exactly

1 Like

I just realized this topic is about OOP and not modules

1 Like
1 Like

What about the static method for metatable?

1 Like

Static and member methods alike will be “inherited” using the setmetatable method.

1 Like

I really want to do OOP, but I reject using metatables. Is this possible?

1 Like

Yes, its possible. Example:

local module = {}

function module.func(self, a) 
	print(self.num + a)
end

function module.new() 
	return {
		num = 0,
	}
end

-- OR

local module2 = {}

function module2.new() 
	return {
		num = 0,
		func = function(self, a)
			print(self.num + a)
		end,
	}
end

local mod = module.new()
mod.num = 1 
module.func(mod, 2)

local mod2 = module2.new()
mod2.num = 1
mod2:func(2)
-- both print three

I just recommend using metatables as you dont need to understand them to do OOP, and you dont need to either define functions weirdly (the second one) or call them weirdly (the first one).

1 Like

This is a very good tutorial, thanks!

1 Like

Oh well, how do you inherit base class Car and have your own functions

local redCar = RedCar.new()

redCar:Drive() -- Inherited from base method in Car
redCar:Honk() -- The custom method in redCar
1 Like