Behaviors*

Synopsis

GameObject:AddBehavior will add a Behavior component to an object from a loaded Lua script resource. UIObject:AddBehavior will add a UIBehavior component, which evaluates additional functions that are specific to UIObjects.

gameManager = GameObject.Create()
-- Load "gameManager.lua" as a component type named "GameManagerBehavior"
Ico.Load("GameManagerBehavior", "gameManager.lua")
-- Add a Behavior of type "GameManagerBehavior" to an object, and cache a local reference
local newComponent = gameManager:AddBehavior("GameManagerBehavior")
-- Give the instance of "GameManagerBehavior" a unique global name
newComponent:SetID("gameManagerStatic")
local m = newComponent:ID()
Ico.Log(m)

Behavior Template

local function Update(context)
-- Update is evaluated every frame.
end

local function LateUpdate(context)
-- LateUpdate is evaluated after Update() every frame.
end

local function OnEnable(context)
-- OnEnable is evaluated when the script is enabled.
-- By default, Behaviors are enabled when added to an object.
end

local function OnDestroy(context)
--OnDestroy is evaluated when the Behavior or the object it is attached to is destroyed.
end

local function OnDisable(context)
--OnDisable is evaluated when the Behavior or the object it is attached to is disabled.
end

-- UI functions are only called by UIBehaviors.

local function OnButtonDown(context)
-- InteractiveMode on the UIObject must be set to "Button". Evaluated when the UI button is held down. 
end

local function OnButtonUp(context)
-- InteractiveMode on the UIObject must be set to "Button". Evaluated when the UI button is up. 
end

local function OnButtonCancel(context)
-- InteractiveMode on the UIObject must be set to "Button". Evaluated when the UI button is canceled.
end

local function OnHoverEnter(context)
-- Evaluated when the cursor hovers the UIObject.
end

local function OnHoverExit(context)
-- Evaluated when the cursor stops hovering the UIObject. 
end

local function OnDrag(context)
-- Evaluated when the UIObject is dragged by the cursor.
end

local function OnScrollWheel(context)
-- Evaluated when the mouse scroll wheel is scrolled.
end

local ScriptAsset = {
    Update = Update,
    LateUpdate = LateUpdate,
    OnEnable = OnEnable,
    OnDisable = OnDisable,
    OnDestroy = OnDestroy,
    OnButtonDown = OnButtonDown,
    OnButtonUp = OnButtonUp,
    OnButtonCancel = OnButtonCancel,
    OnHoverEnter = OnHoverEnter,
    OnHoverExit = OnHoverExit,
    OnDrag = OnDrag,
    OnScrollWheel = OnScrollWheel
};

return ScriptAsset;

context

context contains information about the Behavior instance. Warning: context is writable, which allows additional variables to be added, but also allows its initial values to be edited.

context.object : Object Returns the GameObject or UIObject the instance of the script is attached to.
context.behavior : Behavior Returns the Behavior or UIBehavior that the instance of the script is used in.