Noir
  • πŸšΆβ€β™‚οΈIntro
  • πŸ“Tutorials
    • πŸŽ“Starting Noir
    • πŸ“–Libraries
    • πŸ–₯️Services
    • ❔Libraries VS Services
    • πŸ’¬Callbacks
    • πŸ’₯Classes
    • πŸ”ŽDebugging
  • πŸ“šExamples
  • β˜„οΈAPI Reference
    • Bootstrapper
    • Built-Ins
      • Classes
        • AITarget
        • Body
        • Command
        • Connection
        • Event
        • HTTPRequest
        • HTTPResponse
        • Library
        • Message
        • Object
        • Player
        • Service
        • Task
        • TickIterationProcess
        • Tracker
        • Vehicle
        • Widgets
          • MapLabelWidget
          • MapLineWidget
          • MapObjectWidget
          • PopupWidget
          • ScreenPopupWidget
          • Widget
      • Libraries
        • Base64
        • Dataclasses
        • Deprecation
        • Events
        • HTTP
        • JSON
        • Logging
        • Matrix
        • Number
        • String
        • Table
      • Services
        • CommandService
        • GameSettingsService
        • HTTPService
        • MessageService
        • NotificationService
        • ObjectService
        • PlayerService
        • TaskService
        • TPSService
        • UIService
        • VehicleService
    • Callbacks
    • Class
    • Classes
    • Debugging
    • Definition
    • Libraries
    • Noir
    • Services
    • TypeChecking
Powered by GitBook
On this page
  • What Are Classes?
  • Creating A Class
  • Adding Custom Methods
  • Creating A Class Object From A Class
  • Inheritance
  • Intellisense
Edit on GitHub
  1. Tutorials

Classes

This page will go over Noir's class system. Classes are used throughout Noir, like for services, etc.

PreviousCallbacksNextDebugging

Last updated 2 months ago

What Are Classes?

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.

person.lua
---@class Person: NoirClass <-- For intellisense
---@field New fun(self: Person, name: string, occupation: string): Person <-- so the Lua extension thinks Person:New() returns a Person and not a NoirClass
---@field name string The name of this person
---@field occupation string The occupation of this person
local Person = Noir.Class("Person")

---@param name string
---@param occupation string
function Person:Init(name, occupation)
    self.name = name
    self.occupation = occupation
end

function Person:PrintInfo()
    print(("I am %s, my occupation is %s."):format(self.name, self.occupation))
end

-- Creating an object/instance from the class above
local JohnDoe = Person:New("John Doe", "Unoccupied")
JohnDoe:PrintInfo()

Creating A Class

We can create a class by simply calling Noir.Class and providing a name as the first argument.

MyClass.lua
local MyClass = Noir.Class("MyClass")

This class is missing an :Init() method, which is required! Let's fix that.

MyClass.lua
function MyClass:Init(name) -- Arguments passed by MyClass:New() end up here
    self.name = name -- Create a class attribute
end

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 Custom Methods

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.

MyClass.lua
function MyClass:MyName()
    return self.name
end

This method essentially returns the name that was stored in class objects descending from MyClass because of the :Init() method.

Creating A Class Object From A Class

To create an object from a class, simply call the :New() method like so:

main.lua
local MyObject = MyClass:New("Cuh4")
print(MyObject.name) -- "Cuh4"
print(MyObject:MyName()) -- "Cuh4"

Easy as that! Note that arguments passed to :New() will be passed to the class's :Init() method, as said earlier.

Inheritance

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().

Cuh4.lua
local Cuh4 = Noir.Class("Cuh4", MyClass, ...) -- you can inherit from multiple classes

function Cuh4:Init()
    self:InitFrom(
      MyClass, -- class to initialize from
      "Cuh4"
    )
end

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.

main.lua
local cuh4 = Cuh4:New()
print(cuh4.name) -- "Cuh4"
print(cuh4:MyName()) -- "Cuh4"

Intellisense

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.

this
this
πŸ“
πŸ’₯
Page cover image