Skip to content

CVR Lua Client Behaviour

Runs a Lua script against the client. Extends CVRBaseLuaBehaviour, implements IStateMachineCallbackReceiver, and forwards Unity lifecycle / collision / render / CVR-API events to matching Lua functions when they exist.

CVRLuaScript. ScriptableObject wrapping the Lua source to execute. Required for the behaviour to run.

bool. Declared on the base class. Default true. Scripts on avatars always run locally; on spawnables and worlds, non-local scripts have extra network routing.

BoundObject[]. Declared on the base class. Name/UnityObject pairs injected into the Lua script at initialisation time. Each entry has:

  • name — lookup key used in Lua.
  • boundThing — any UnityEngine.Object (GameObject, component, animator, audio source, etc.).

The script can implement any of the following Lua functions. They match Unity’s message names plus CVR-specific ones. The full list (from GetEventFunctionNames) is:

  • Awake, Start, OnDestroy, Update, FixedUpdate, LateUpdate, OnEnable, OnDisable, OnApplicationQuit, OnApplicationPause, OnApplicationFocus
  • OnCollisionEnter, OnCollisionExit, OnCollisionStay, OnCollisionEnter2D, OnCollisionExit2D, OnCollisionStay2D
  • OnTriggerEnter, OnTriggerExit, OnTriggerStay, OnTriggerEnter2D, OnTriggerExit2D, OnTriggerStay2D
  • OnControllerColliderHit, OnJointBreak, OnJointBreak2D
  • OnCanvasGroupChanged, OnRectTransformRemoved, OnRectTransformDimensionsChange, OnTransformChildrenChanged, OnTransformParentChanged, OnBeforeTransformParentChanged
  • OnMouseDown, OnMouseEnter, OnMouseDrag, OnMouseExit, OnMouseOver, OnMouseUp, OnMouseUpAsButton
  • OnAnimatorMove, OnAnimatorIK
  • OnRenderImage, OnPostRender, OnPreCull, OnPreRender, OnRenderObject, OnWillRenderObject, OnBecameVisible, OnBecameInvisible
  • OnParticleCollision, OnParticleSystemStopped, OnParticleTrigger, OnParticleUpdateJobScheduled
  • CVR API: OnPlayerJoined, OnPlayerLeft, OnInstanceConnected, OnInstanceDisconnected, OnInstanceConnectionLost, OnInstanceConnectionRecovered, OnLocalPlayerAvatarLoaded, OnLocalPlayerAvatarClear, OnRemotePlayerAvatarLoaded, OnRemotePlayerAvatarClear, OnSpawnableCreated, OnSpawnableDestroyed
  • VR mode: OnPreVRModeSwitch, OnPostVRModeSwitch, OnFailedVRModeSwitch
  • Late events: OnPostFixedUpdate, OnPostUpdate, OnPostLateUpdate
  • State machine: OnStateMachineEnter, OnStateMachineExit
  • Runtime gizmo: OnDrawRuntimeGizmos — when defined, the component attaches a RuntimeGizmo that calls this function from LateUpdate inside a gizmo draw context.

CVR API / collision events receive wrapped _LUAINSTANCE_* objects (e.g. _LUAINSTANCE_ScriptedPlayer, _LUAINSTANCE_ScriptedCollision, _LUAINSTANCE_ScriptedRenderTexture) instead of raw Unity types; see the Lua API docs for their surface.

  • Listeners are registered in Start and torn down in OnDestroy. A listener is only added if the script defines the matching function.
  • Each call goes through ExecuteEvent with a 1000ms timeout (5000ms for the initial script load / run). Exceeding the timeout marks the script as crashed.
  • Attempting to manually call a Unity event function (e.g. calling Update() from Lua) raises a “Game Event function, and can’t be manually called!” error.

CVR-GameFiles/ABI.CCK.Components/CVRLuaClientBehaviour.cs, CVR-GameFiles/ABI.CCK.Components/CVRBaseLuaBehaviour.cs