Classes
This page will go over Noir's class system. Classes are used throughout Noir, like for services, etc.
Last updated
This page will go over Noir's class system. Classes are used throughout Noir, like for services, etc.
Last updated
All of this might be confusing if you don't understand OOP. Consider reading up on if you don't know or properly understand OOP.
In Noir, classes are simply advanced tables that can be cloned numerous times into class objects.
This is an example of a class. The ---@
bits are for intellisense.
We can create a class by simply calling Noir.Class
and providing a name as the first argument.
This class is missing an :Init()
method, which is required! Let's fix that.
To break this down, every time MyClass:New()
is called, a new table is created, and methods from MyClass
are copied over (apart from :Init()
and internal methods). Once this is done, MyClass.Init()
is called, with the first argument being the new table.
This :Init()
method is essentially responsible for setting up class objects descending from MyClass
. This is similar to Python's __init__
method for classes.
Adding a custom method is very simple. It's essentially the same as the :Init()
method, but we rename the method and change the functionality.
This method essentially returns the name that was stored in class objects descending from MyClass
because of the :Init()
method.
To create an object from a class, simply call the :New()
method like so:
Easy as that! Note that arguments passed to :New()
will be passed to the class's :Init()
method, as said earlier.
Noir's classes system has support for inheritance. To inherit from another class, simply provide the class you want to inherit from as the second argument to Noir.Class()
.
self:InitFrom()
essentially creates an object from MyClass
(the parent class we passed to the method) and passes all created attributes and methods down to the new Cuh4
object.
Intellisense is relatively easy to setup with classes. Check out the example at the start of this page.
Consider reading up on for more information on using intellisense with classes.