Let's create a new class that inherits from BaseClass
-- Creates a new Class called "Person" inheriting from BaseClass-- and stores it in the global variable PersonPerson = BaseClass.Inherit("Person")-- You can also create a new class that inherits from an inherited class-- Let's create an Employee class, which will inherit from Person and BaseClassEmployee = Person.Inherit("Employee") -- Spawn an instance of this class using the default constructorlocal my_employee =Employee()-- Destroy an instance by using the default destructormy_employee:Destroy()
Constructor/Destructor override
You can create your own Constructor/Destructor for your classes, that will allow to control the behaviour of your class when an instance is created or destroyed
-- Here we add the class constructor, called when a new instance is created-- We also add 2 parameters which will be used when spawning the instancefunction Person:Constructor(sName,iAge) self:SetName(sName or"John Doe") self:SetAge(iAge or18)end-- We can now call the constructor with the following syntaxlocal my_person =Person("Jane Doe", 19)-- Here we add the class destructor-- It'll be called when an instance is destroyed with `my_object:Destroy()`function Person:Destructor()print("Person ["..self:GetName().."] `Destructor` called")end-- We can also call the constructor of another class in the new classfunction Employee:Constructor(sName,iAge,sJob) Person.Constructor(self, sName, iAge) self:SetJob(sJob)end-- `instance:Super` will return the class from which the instance class inherits-- The example above could've been done by calling `self:Super` instead of `Person`function Employee:Constructor(sName,iAge,sJob) self:Super().Constructor(self, sName, iAge) self:SetJob(sJob)end
Storing Data
You can store data on any instance, either in a classic key/value indexation
You can also use the :SetValue and :GetValue methods, which triggers "ValueChange" events on the class and on the instance it's called on
local my_person =Person()-- Let's add a value on an instance of the classmy_person.foo ="bar"-- Accessing the key "foo" will return the value "barprint(my_person.foo)-- We can also use `instance:SetValue` to store the key/value-- This will trigger "ValueChange" event on the class, and on the instancemy_person:SetValue("foo", "bar")-- To get value assoaciated to a key, we use `instance:GetValue`print(my_person:GetValue("foo"))
Adding new Methods
Adding or overriding methods to your classes is easy, let's say we want to add a new method for Person, we just do that:
function Person:SetName(sName)if (type(sName) ~="string") thenreturnend self:SetValue("name", sName)end
Within your methods, you can access the called instance with self.
Then you can call your cool method!
local my_person =GetPersonSomehow()my_person:SetName("John Doe")
Local Events
It's possible to trigger local events on a class or an instance, example:
-- Subscribe and call local events on instanceslocal my_instance =GetInstanceSomehow()my_instance:Subscribe("SomeEvent", function(...)print("Event fired!")-- Optionally we can return false to unsubscribe to the event after being firedreturnfalseend)my_instance:Call("SomeEvent")-- You can also subscribe to or call events on the class itself-- It will only be triggered on the class and won't be dispatched to instanceslocal callback = MyClass.ClassSubscribe("SomeEvent", function(...)print("Event fired!")end)MyClass.ClassCall("SomeEvent", ...)MyClass.Unsubscribe("SomeEvent", callback)
Synchronized Classes
To synchronize instance creation/destruction on a class, you must follow these simple steps
Define your class on the shared side, and pass true on the sync parameter of Inherit
After that, spawn/destroy your instances on the 🟦 Server side
-- Serverlocal new_instance =MySyncClass()-- ClientMySyncClass.ClassSubscribe("Spawn", function(self)print("New instance of the class spawned by the server!")end)
Network Events
It is also possible to trigger custom events on instances of your Class, using the methods CallRemote or BroadcastRemote like in the examples above:
Network events requires you to setup a synchronized class first
Check the Synchronized Classes example before
When sending remote events, the first parameter will always be the instance on which the remote event is sent
Or from Client to Server
-- Clientlocal my_instance =GetInstanceSomehow()my_instance:CallRemote("CoolEvent", "foo", "bar")-- Serverfunction MyClass:OnCoolEvent(player,arg1,arg2)print(player) -- The player who sent the eventprint(arg1) -- output: "foo"print(arg2) -- output: "barendMyClass.SubscribeRemote("CoolEvent", MyClass.OnCoolEvent)
When receiving network events on the server, the 2nd parameter in the callback will always be the player sending the packet
Cloning Instances
You can clone instances very easily, it's doable with the :Clone() method
local my_instance =GetInstanceSomehow()local new_instance = my_instance:Clone()
To filter the key/values that will be cloned, you can use the ignored keys parameter of :Clone(), like how it's done in the following example:
local my_instance =GetInstanceSomehow()-- By using the filter argument, we make the cloned instance ignore some keys to copylocal new_instance = my_instance:Clone({"health", "armor"})
Cloning an instance will copy all the key/values of the cloned instance into the new instance, except the filtered ones, and it's ID