DirkScripts
Docs
Class
βΉοΈ Shared Module - This can be used by both clients and the server
#lib.class
Create a new class definition with optional inheritance.
lualocal Animal = lib.class('Animal') function Animal:constructor(name, sound) self.name = name self.private.sound = sound -- private field end function Animal:speak() return self.name .. ' says ' .. self.private.sound end local dog = Animal:new('Rex', 'Woof') print(dog:speak()) -- "Rex says Woof"
#Signature
lualocal MyClass = lib.class(name, super)
| Parameter | Type | Required | Description |
|---|---|---|---|
| name | string | yes | Class name (used for type checking) |
| super | table | no | Parent class to inherit from |
#Inheritance
lualocal Dog = lib.class('Dog', Animal) function Dog:constructor(name) self:super(name, 'Woof') -- call parent constructor end function Dog:fetch(item) return self.name .. ' fetches the ' .. item end
#Type Checking
lualib.isClass(dog) -- true lib.instanceOf(dog, Animal) -- true lib.instanceOf(dog, Dog) -- true
#Private Fields
Fields stored on self.private are only accessible from within the class methods. External access will error.
luaprint(dog.private.sound) -- errors: cannot access private field
