Can anyone give a good explanation on what Object-Orientated-Programming is? It seems advanced and I was wondering if anyone can explain it in a simple form.
All I know so far is its based around objects.
Can anyone give a good explanation on what Object-Orientated-Programming is? It seems advanced and I was wondering if anyone can explain it in a simple form.
All I know so far is its based around objects.
Basically, an object (an instance of a class) encapsulates logic.
Classes define the base logic that each object will share.
For example, say we have a class that makes up an employee. Employees are not all going to be the same, but they all share the same necessary info. (their age, badge ID, name, etc.)
So you could have a class that defines these members that each object will have.
I’ll write an example in C# code.
class Employee {
private string name;
private int badge_ID;
public Employee(string name, int badge_ID)
{
this.name = name;
this.badge_ID = badge_ID;
}
}
Employee Tom = new Employee("Tom", 95);
Employee Jones = new Employee("Jones", 300);
Classes also have constructors, to initialize member variables of the newly created object to some values.
Object-oriented design is quite nice when you’re working with a large number of objects that will all share similar (or the exact same) functionality. When making complex types of objects, you might find you need to share functionality from a different class, but add on to it. This is referred to as inheritance.
Lua is not object-oriented (as in it has no class syntax), although with use of metatables you can get pretty close.
Thank you so much. So glad you took your time to write this. Really appreciated.
Quick question, what would this code look like in roblox studio?
local Employee = {}
function Employee.new(name: string, badge_ID: number)
local self = setmetatable({
name = name,
badge_ID = badge_ID
}, Employee)
return self
end
local Tom = Employee.new("Tom", 95)
local Jones = Employee.new("Jones", 300)
This is untested but in theory should work.
Assuming you want employee objects to emulate inheritance from the employee class you’d need to define the metatable’s __index
metamethod.
local self = setmetatable({
name = name,
badge_ID = badge_ID
}, {__index = Employee})
-- Sound class to handle all sounds in the game. It automatically creates an array of all sounds from the Sounds folder
local SoundService = game:GetService("SoundService")
Sound = {}
Sound.__index = Sound
function Sound.new(soundsFolder:Folder)
local self = {}
setmetatable(self, Sound)
self.gameSounds = soundsFolder or workspace.Systems.Sounds:GetChildren()
return self
end
function Sound:playSound(whichSound:string, length:IntValue)
local soundLength = length or 0
for soundID, sound in pairs(self.gameSounds) do
if sound.Name == whichSound then
sound:Play()
else
print("Error: Sound not found, double check spelling.")
print(" If sound still doesn't play contact a scripter.")
end
end
end
function Sound:stopSound(whichSound:string)
for soundID, sound in pairs(self.gameSounds) do
if sound.Name == whichSound and sound.Playing then
sound:Stop()
else
print("Error: Sound not found, double check spelling.")
print(" If sound still doesn't play contact a scripter.")
end
end
end
return Sound
This is a simple Sound Class I created.
To use that class in any of your other scripts you would have to
local Sound = require(game.ReplicatedStorage.Sound)
partSound = Sound.new()
partSound:playSound("click1")
If you need any further help with creating a class let me know. I’ve designed and used a ton of classes in my games.