OOP w/ actual Objects: Using Construct OOP

This project has been deprecated. Although I’ve rewritten this a few times, I’ve ultimately decided it is unnecessary bloat. If others are still interested in this project, I’ll release the rewrites as is.

8 Likes

Sure, here it goes:

I think your approach is a bit over-engineered. Even your examples are tough to follow.

I do not think there is much security gained here. If you have rogue code running on your server, you have much bigger issues than public/private variables.

Sure, private variables are great for organization in languages like Java or C#. But really, the benefit there is keeping things tidy. That benefit doesn’t translate well to Lua.

4 Likes

I disagree, and here’s why:

Take a look at the following example:

Car = {}
Car.__index = Car
function Car.new(position, driver, model)
    local newcar = {}
    setmetatable(newcar, Car)
    newcar.Position = position
newcar.Driver = driver
newcar.Model = model

return newcar
end
function Car:Boost()
    self.Speed = self.Speed + 5
end

Traditionally, that’s fine and in most cases, will work. My solution is more geared toward frameworks and tools which may not wish to expose all their variables within their Objects. I don’t disagree this may be over-engineered for most use cases, but even then it’s a pretty neat way of cleanly organizing stuff. I plan to integrate my Bleu Pigs PigBot into examples here in a bit, allowing me to show more detailed examples better showing what it can actually do.

2 Likes

I can’t view that thread and I wonder what can I use this for can you give us a few examples? Not just for creating custom Classes but what else?

Don’t get me wrong it’s a great tutorial, I just want to know more.

1 Like

Whoops, didn’t realize that, my bad. For the time being, I’ve put direct links to both the ROBLOX Model and my real world example (a project I’m working on for my group called PigBot). I plan to rebuild the tutorial using direct content from PigBot as examples, hopefully helping to better clear and explain in more detail.

I agree with this. It’s quite a bit more complex to implement a class using your module than it is with metatables. The private member support is handy, but also simple to implement yourself.

That being said; I would love to see Construct provide some of the more difficult to accomplish patterns, such as multiple inheritance, or an inheritance chain allowing for referencing using the direct super class, akin to parent property of roblox instances.

Accessing the method from object.Super could allow for calling a non-instanced version of the class

object.Super.Super:NonInstancedMethod()

This could be really useful using default states to get a pure calculation when your current object state would effect the result of said method.

2 Likes

How do I make a class? Your documentation does not make this clear. In the OP it seems like you make a class via:

local Construct = require(workspace.ConstructOOP)
local Enums = Construct.new("BaseObject", "Enum")

But that doesn’t seem to work. In the readme, you need some sort of ObjectsManager to create a class, but how do I even get ObjectsManager?

1 Like

@InfinityDesign It’s a complex system cause it was built to solve some complex issues with how Lua works. Lua doesn’t include native OOP, and I personally dislike not being able to keep “private data” private with how traditional OOP works. As for the Super idea, I’ll most certainly take a look

@XAXA yeah … I was in the middle of creating the tutorial when I had an unexpected event come up and quickly finished the rest. Guess I did it a little too quickly, and I apologise for that lack of information. Im already reworking the tutorial to be more informative, just looking for time to do (thanks Amazon Fullifilment). I am aiming to have the updated tutorial out this weekend.

ObjectsManager is the root of Construct. Construct is ObjectsManager and ObjectsManager is Construct. When you require the Construct Module, you will receive the ObjectsManager to manage the creation/implementation of new classes. To create a new BaseClass, you will need to use ObjectsManager.createClass. To create a new Instance of said BaseClass, you’ll use Construct.new(“ClassName”, “NameOfObject”, vararg …)

This is really cool. I like the fact that you’re tackling OOP in Lua but I also agree with Osyris’s statement that this is over-engineered. Sure, OOP is great but Lua isn’t designed to be an imperative language. It, at most, mimics the surface of OOP and trying to force it to do more adds layers of complexity to code you do not need. You can gain the same benefits in Lua through FRP than through the OOP approach in a simpler fashion.

Thanks for the feedback!

I haven’t gotten around to it yet, but I’ve optimized and cleaned up how Construct works quite a bit in the project I’m currently using it with. I plan to port it over soon, as it’s definitely a bit cleaner and easier to understand.

Yeah, I feel like if I used this, I’d be writing more code than I would be otherwise. Maybe it’s better constrained, but writing less code overall is usually the bigger priority in game development. Writing perfect code just isn’t that important.

1 Like

To each their own, as we all form opinions based on our own experiences. /shrug

I will not disagree with the factor in which it may be over complicated for a lot of cases. Again, this is a framework for those who are wanting to build easily readable managed code with extensive control/flexibility over the Object itself. Yes, it is more time consuming, but you gives you a very powerful OOP experience. long-term stability, and more consistent structure. Not to mention, after building all of your classes, the actual logic interfacing with the Objects themselves (if done in how the framework was intended) will overall be shorter and less time-consuming.

… but thats just my opinion formed from my experiences. And thus, I released them for others to tinker with. I don’t expect it to be a perfect solution nor for anyone to agree with my solutions.

Forgot to add: I have revamped Construct (and published it with an example) based on the feedback I’ve received so far. I also plan to update this tutorial within the next month or so, but am currently time constrained due to personal issues and a game I’m trying to meet a deadline with.