As I use OOP more in my games, I stumbled upon software design patterns (Singleton, Factory, Command Pattern, etc.) and was wondering how I would go on implementing it into my OOP structures in my game.
local CrystalManager = {}
CrystalManager.__index = CrystalManager
function CrystalManager.new(name, rarity, refined)
local newCrystal = {}
newCrystal._Gem = game.ReplicatedStorage.GameAssets.GemTypes:WaitForChild("Default"):Clone()
newCrystal._Name = name or "Crystal"
newCrystal._Rarity = rarity or "Common"
newCrystal._Refined = refined or false
newCrystal._Maid = Maid.new()
return setmetatable(newCrystal, CrystalManager)
end
function CrystalManager:SetProximityAndProperties(DataManager)
-- Do Stuff
end
This is an example of OOP used in a game I’m developing. Basically, in my crystal generating scripts I would require the module and call this function:
local Object = CrystalManager.new("Crystal", "Common", false)
-->>: The crystal's name, rarity and refineness is randomized & interchangable
Object:SetProximityAndProperties(dataManager)
-->>: Just sets the properties and events for the object
All this does is generate a random gem, for example, Crystal, Ruby, Emerald, etc., then sets the properties and events for the object. So this brings me to the questions that I have:
What kind of software design patterns can I use to enhance my OOP structures (Eg. The code posted above) ? If so, what are some examples of how I would go on implementing it?
What kind of software design patterns are commonly used in Lua Roblox Development?
The design patterns you mentioned are typically used in purely Object-Oriented Programming languages. Because lua is not an object-oriented programming language, these typically won’t come up.
For instance, why would you use a singleton if you could just write the code to do what you need? Roblox doesn’t have interfaces so you can’t really create a Factory, and why would we use a Command Pattern when we can just directly call a function?
You’ll notice that all of the patterns you just listed have a corresponding UML diagram when you look them up. This is because they are intended to be used in languages that are primarily object-oriented, which luau is not.
luau’s OOP setup with metatables is fairly bare-bones, but it definitely complements all of the other programming methods available in luau. With all of that being said, although this really isn’t an answer to your question, I would be careful when trying to implement these design patterns because you might just be making your life increasingly more difficult in doing so.
Sorry, I was away from the dev forums for a while. Thank you for your answer, I only recently grasped the concept of the design patterns and was just wondering how I would be able to apply it to Roblox. You are right that all of these design patterns can be done in other ways because like you said, Lua isn’t really an OOP-focused language. This is a great answer, and while I do have some design patterns implemented in one of my games, I will be sure to be careful when using them