Class System
The Class System provides a simple and intuitive way to create object-oriented code in Lua, making it easier to organize and structure your FiveM scripts.
Compatibility
✅ Shared - Works on both client and server-side
Available Methods
class(name)
class(name)Creates a new class with the specified name.
Parameters:
name(string) - The name of the class
Returns:
table- The created class
Example:
local Animal = class("Animal")
-- Or use global access
local Animal = class("Animal")Class:constructor(func)
Class:constructor(func)Defines the constructor function for the class.
Parameters:
func(function) - The constructor function
Example:
local Animal = fivem.Class.create("Animal")
Animal:constructor(function(self, name)
self.name = name
end)
-- Or use global access
local Animal = Class.create("Animal")
Animal:constructor(function(self, name)
self.name = name
end)Class:method(name, func)
Class:method(name, func)Adds a method to the class.
Parameters:
name(string) - The name of the methodfunc(function) - The method function
Example:
local Animal = fivem.Class.create("Animal")
Animal:method("getName", function(self)
return self.name
end)
Animal:method("makeSound", function(self)
return "Some animal sound"
end)
-- Or use global access
local Animal = Class.create("Animal")
Animal:method("getName", function(self)
return self.name
end)
Animal:method("makeSound", function(self)
return "Some animal sound"
end)Class:static(name, func)
Class:static(name, func)Adds a static method to the class.
Parameters:
name(string) - The name of the static methodfunc(function) - The static method function
Example:
local Animal = fivem.Class.create("Animal")
Animal:static("createDefault", function(name)
return Animal:new(name)
end)
-- Or use global access
local Animal = Class.create("Animal")
Animal:static("createDefault", function(name)
return Animal:new(name)
end)
-- Usage
local animal = Animal.createDefault("Generic Animal")Class:new(...)
Class:new(...)Creates a new instance of the class.
Parameters:
...- Arguments to pass to the constructor
Returns:
table- The new instance
Example:
local Animal = fivem.Class.create("Animal")
Animal:constructor(function(self, name)
self.name = name
end)
local animal = Animal:new("Generic Animal")
-- Or use global access
local Animal = Class.create("Animal")
Animal:constructor(function(self, name)
self.name = name
end)
local animal = Animal:new("Generic Animal")Class:extend(name)
Class:extend(name)Creates a new class that extends the current class.
Parameters:
name(string) - The name of the new class
Returns:
table- The new extended class
Example:
local Animal = fivem.class("Animal")
Animal:constructor(function(self, name)
self.name = name
end)
local Dog = Animal:extend("Dog")
Dog:constructor(function(self, name, breed)
self:super(name) -- Call parent constructor
self.breed = breed
end)
-- Or use global access
local Animal = class("Animal")
Animal:constructor(function(self, name)
self.name = name
end)
local Dog = Animal:extend("Dog")
Dog:constructor(function(self, name, breed)
self:super(name) -- Call parent constructor
self.breed = breed
end)Class:private(name, func)
Class:private(name, func)Adds a private method to the class (convention-based).
Parameters:
name(string) - The name of the private methodfunc(function) - The private method function
Example:
local Animal = fivem.class("Animal")
Animal:private("validateName", function(self, name)
return name and type(name) == 'string'
end)
Animal:method("setName", function(self, name)
if self:validateName(name) then
self.name = name
end
end)
-- Or use global access
local Animal = class("Animal")
Animal:private("validateName", function(self, name)
return name and type(name) == 'string'
end)
Animal:method("setName", function(self, name)
if self:validateName(name) then
self.name = name
end
end)instance:super(...)
instance:super(...)Calls the parent class constructor or method.
Parameters:
...- Arguments to pass to the parent
Example:
local Animal = fivem.class("Animal")
Animal:constructor(function(self, name)
self.name = name
end)
local Dog = Animal:extend("Dog")
Dog:constructor(function(self, name, breed)
self:super(name) -- Call parent constructor
self.breed = breed
end)
-- Or use global access
local Animal = class("Animal")
Animal:constructor(function(self, name)
self.name = name
end)
local Dog = Animal:extend("Dog")
Dog:constructor(function(self, name, breed)
self:super(name) -- Call parent constructor
self.breed = breed
end)Inheritance Example
-- Extended class
local Dog = Animal:extend("Dog")
Dog:constructor(function(self, name, breed)
self:super(name) -- Call parent constructor
self.breed = breed
end)
Dog:method("makeSound", function(self)
return "Woof!"
end)
Dog:method("getBreed", function(self)
return self.breed
end)
-- Usage
local dog = Dog:new("Buddy", "Golden Retriever")
print(dog:getName()) -- Output: Buddy (inherited from Animal)
print(dog:makeSound()) -- Output: Woof! (overridden in Dog)
print(dog:getBreed()) -- Output: Golden Retriever (Dog method)Best Practices
Use descriptive class names - Make class names clear and specific
Initialize all properties - Set default values in the constructor
Validate inputs - Always validate parameters in methods
Use inheritance appropriately - Don't over-engineer simple classes
Last updated