Skip to main content

ScriptInstance

Client Server

The lua table referred to in a script with self The Script component attached to the object can be found via self.component.

local self = {}

--Called once on object creation before Start()
function self:Attach()
end

--Called once on object creation after Attach()
function self:Start()
end

--Called once a frame after Start() if the object is active
function self:Update(dt)
end

--Called after Update(), before rendering
function self:LateUpdate()
end

function self:OnActivate()
end

function self:OnDeactivate()
end

--Called when script component or object is destroyed
function self:Detach()
end

return self

Script Lifecycle

The rules:

  • The script is loaded and Attach() is called right away, even if the object is inactive
  • If the object is active Start() etc is called one frame later. Currently only on server this may change in the future
  • Each step is done for all applicable objects in a row i.e load all scripts, Attach() all objects, Start() all active objects
  • Never call OnActivate() OnDeactivate() before Start()
  • Never call OnDeactivate() if OnActivate() has not been called

Methods

nil LateUpdate()

Called after Update(), before rendering

nil RPC(string, ...)

Call a lua function by name from server to all clients or from one client to server. You may pass parameters. You may send tables and basic math types such as Vec3 or Quat but other classes may not be supported.

function self:Start()
--remember to sync the script component to clients otherwise there is no one to receive the RPCs
self.component.syncToClients = true
self.cam = Scene:GetActiveCamera()

events.mouseButtonDown.addListener(self, function (button, from)
if self.onServer then return end
--if left mouse button pressed create a ray from mouse position and direction
if button == 1 then
local mpos = Input:MousePosPerc()
local tf = self.cam.object.transform
local pos = tf.pos + tf.forward * 1.5

--call this function on all clients with these two parameters
self:RPC("serverRaycast", pos, tf.forward*1000)
end
end)

end

function self:serverRaycast(origin, ray)
assert(self.onServer)
local hit = Collision():Raycast(origin, ray)[1]
if hit then
--make a cross at hit
deb:cross(hit.pos)
print("hit")
end
end

nil RPC(integer, string, ...)

From server call a lua function by name on one specific client

nil Attach()

Called once on object creation before Start() even if the script is not active

nil Detach()

Called when script component or object is destroyed

nil Start()

Called once on object creation after Attach() once the script becomes active

nil FixedUpdate()

nil Update(number deltaTime)

Called once a frame after Start() if the object is active

nil LateUpdate(number deltaTime)

nil OnActivate()

Script active state changed through object or component. Never called before Start()

nil OnDeactivate()

Script active state changed through object or component. Never called before Start() and OnActivate()

nil EditorAttach()

nil EditorDetach()

nil EditorStart()

nil EditorFixedUpdate()

nil EditorUpdate(number deltaTime)

nil EditorLateUpdate(number deltaTime)

nil EditorOnActivate()

nil EditorOnDeactivate()

nil RPC(string funcName, ... args)

Properties

Object obj

Script com

Transform transform

The transform of the object this script is attached to

boolean onServer

Use this to run part of the code only on server or client

if self.onServer then
-- do something only on server
end

boolean onClient

Use this to run part of the code only on server or client

if self.onClient then
-- do something only on client
end

Script Com

Script Component

Script component

The script component, separate from the lua table

Object Obj

Object object

The object this script is attached to

Transform Transform

boolean OnServer

boolean OnClient