All about Object Oriented Programming

Can’t believe this tutorial is almost a decade old and still holds up this well. Excellent job!

1 Like

I discovered this thread a while back but come back to it now and again to remind myself/correct myself on what I’m doing but this thread has been incredibly helpful in metatables and I’ve even implemented it into my projects. Great resource :slight_smile:

Very nice tutorial!

I’ve actually written a module that I might open-source later that actually makes implementing OOP very easy including inheritance.

Calling module:class(your_table_here) automatically assigns it a :new() function for making instances and a :init() function that gets called every time you make a new object for setting initial values.

All these functions can be overwritten if desired.
Calling module:class(table1, table2) for instance will turn table1 into a class that inherits from table2.

Using bindable events, other scripts can communicate with classes without having to require() them or know of their existence, should all be relatively memory safe too.

Your tutorial is nice and I hope more people will utilize OOP, it’s unfortunate Lua doesn’t just have classes like C++, C# and Java.

Using metatables still feels a bit hacky an unofficial.
They’re also slower and less efficient than native OOP implementations.
Unfortunately tables use more memory than objects. :frowning:

Hello there, I’m new to OOP so I have a question. Does Car.new() actually create a new car ? If so then how do you define what Class it is?

Great tutorial, very clear and concise, however I would go over metatables for new people more in depth, as it’s a essential aspect of replicating oop within luau

it’s just a function that puts its arguments inside of the self value.

love the idea! sounds simple to make though.

I had trouble seeing the point in OOP, then I read this and it all makes sense lol.

This tutorial is truly amazing! I can’t thank you enough for it :smiley:

This was made in 2014, however, it’s still very useful.

1 Like

I’ve been doing this already in a different way! OOP is much more convenient though.

local Car = {

["Speed"] = 100
["Size"] = Vector3.new()
}

amazing way of explaining it thank you so much man!

2024 and still helpfull :stuck_out_tongue: tysm!!!

2 Likes

If you think that method isn’t efficient then I was wondering how you would go about making replication?

I’m not asking about how to do the actual replication but how you would have :Replicate() be called.

If you want to replicate aka sync, you might need to implement your own syncing system with UUID and stuff like that.

Awesome post thanks this helped me learn some new things!

The man himself is here laying down the straight power code.

setmetatable(self, nil) to prevent dead objects from repeatedly calling methods is exactly why we turn to our greats for major support; Thanks for this!