Page cover

πŸŽ’ Examples

Some simple examples on how to use ClassLib


Creating a new class

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 Person
Person = 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 BaseClass
Employee = Person.Inherit("Employee") 

-- Spawn an instance of this class using the default constructor
local my_employee = Employee()

-- Destroy an instance by using the default destructor
my_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


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


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:

Within your methods, you can access the called instance with self.

Then you can call your cool method!


Local Events

It's possible to trigger local events on a class or an instance, example:


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


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

  • From Server to Client

When sending remote events, the first parameter will always be the instance on which the remote event is sent

  • Or from Client to Server

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

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:

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

Last updated

Was this helpful?