Usage
Create and manage objects, synchronize variables, listen for object events and register render functions.
Spawning objects
Create an object
Spawn an object with a unique ID and optional object settings.
--> ObjectController.create = function(_model, _position, _uid, _options)
-- _options = {
-- rotation = vector3,
-- freezed = boolean,
-- collision = boolean,
-- alpha = number,
-- clickable = boolean,
-- servervars = table,
-- sharedvars = table,
-- }
-- @return {
-- uid = self.data.uid,
-- object = ObjectController._store[self.data.uid]
-- }
local o = ObjectController.create('prop_barrel_01a', vector3(-2612, 1870, 167), 'uid-1', {
clickable = true
})
-- print(o.uid) - @string
-- print(o.object) - @table
Setting and getting variables
Shared and server variables
Set and retrieve shared or server-only values on an object.
local o = ObjectController.create('prop_barrel_01a', vector3(-2612, 1870, 167), 'uid-1', {
clickable = true
})
o.object.setSharedVar('name', 'Shared Variable Example')
o.object.setSharedVar('progress', 5)
o.object.setServerVariable('safe_key', '49fasj384823492898')
-- Get variables
local name = o.object.getSharedVar('name')
print(name) -- @returns 'Shared Variable Example'
print(o.object.getServerVar('safe_key')) -- @returns '49fasj384823492898'
Shared event listeners
OnObjectClicked
Listen for an object click on the server or client.
AddEventHandler(Config.Events.object_clicked, function(uid, data)
-- Object clicked
-- print(uid, data)
end)
OnObjectVariableChange
Listen for changes to an object's variables on the server or client.
AddEventHandler(Config.Events.variable_changed, function(uid, key, value)
-- Object variable changed
-- Config.DebugMsg(string.format('Object variable changed: (%s) %s', key, value))
end)
Export functions
Server-side exports
Expose the object create, delete, get and existence functions.
exports('oc_create', ObjectController.create)
exports('oc_delete', ObjectController.delete)
exports('oc_get', ObjectController.get)
exports('oc_exist', ObjectController.exist)
Client-side exports
Expose functions for adding and removing render callbacks.
exports('oc_addfunction', FunctionController.add)
exports('oc_removefunction', FunctionController.remove)
Render function pool
Integration notes
Managed callbacks
A custom thread runs every function added to the pool, so you do not need to create a separate loop for streamed objects.
Unique function IDs
If you add a function with an existing UID, a message appears in the client console and the original function remains active.
Render function examples
Add render callbacks for object data, then remove a callback when it is no longer needed.
-- FunctionController.add = function(uid, func)
-- Render variables
FunctionController.add('render-names', function(data) -- <-- data always gets called
DrawText3D(data.position.x, data.position.y, data.position.z + 1.2, json.encode(data.sharedvars))
end)
-- Add another render function, such as markers
FunctionController.add('render-markers', function(data) -- <-- data always gets called
local x, y, z = table.unpack(data.position)
DrawMarker(2, x, y, z + 1.5, 0.0, 0.0, 0.0, 0.0, 180.0, 0.0, 0.25, 0.25, 0.25, 255, 255, 0, 50, false, true, 2, nil,
nil, false)
end)
-- Remove the render function after a timeout
Citizen.CreateThread(function()
Citizen.Wait(5000)
FunctionController.remove('render-names')
end)
Object class functions
Available object methods
Methods available on an object instance.
+ setPosition(_pos: vector3)
+ setRotation(_rot: vector3)
+ setFreezed(_state: boolean) -- (Freeze object)
+ setModel(_model: string) -- (Change object model)
+ setAlpha(_alpha: number) -- (Set object alpha)
+ setServerVariable(key: string, value: any)
+ getServerVar(key: string)
+ setSharedVar(key: string, value: any)
+ getSharedVar(key: string)
+ save (You need to setup it with database etc, the examples are already inside the server.lua file)