CommandBehaviour entity,id$,command$

Parameters

entity - an entity with a switch, door, timer or spawner behaviour, from a loaded world or a copy of one

id$ - the behaviour's ID, such as door

command$ - what to do; each kind takes its own:
  a switch: activate, deactivate or reset
  a door: open, close or toggle
  a timer: start, stop or reset
  a spawner: start, stop, spawn or reset

Description

Tells one of an entity's switches, doors, timers or spawners what to do, as a response's action does.

Use it when your own code decides: open the castle gate when the player has the key, start a countdown when a level begins, or spawn one more enemy. The command is queued, and the behaviour does it at the next UpdateWorld, in the same order as the world's own responses, so a door opened from code and one opened by a plate behave the same way.

Open and close are safe to repeat: opening an open door does nothing, and reversing a door halfway carries on from where it is. A switch you activate stays on until it is deactivated, or its time is up; reset turns it off and lets a one-use switch be used again. A timer's start runs it from the beginning when it is idle or finished, and its retrigger setting says what starting a running one does; stop clears its countdown, and reset its count too. A spawner's spawn asks for one more, which is skipped when it already has its most; start and stop its schedule, and stopping keeps what it made; reset removes what it made, cancels what it was about to make, and leaves it idle until it is started.

A behaviour that is switched off (EnableBehaviour) keeps its commands until it is switched on again. StartBehaviour does what this command's activate, open or start does, and switches the behaviour on. A command the kind does not take, or an ID the entity has no behaviour for, is a runtime error.

In a Shared World session, only the host, or a server room's server, runs switches, doors, timers and spawners, and every other copy shows what they do. There, the command does nothing on the other copies, as the host's copy of your program gives it: to let a player's key open a door, send the press to the host with SendSharedMsg, and have the host's copy give the command.

See also: BehaviourState, StartBehaviour, EnableBehaviour, WorldEventBehaviour, WorldPhysicsAutomatic.

Example

; CommandBehaviour Example
; ------------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

; The Switchyard: a gate that a plate opens, and a lamp a timer shows
world=LoadWorldAsync("../../../samples/behaviours/switchyard.world")
While Not WorldReady(world)
    If WorldLoadState(world)=WORLD_LOAD_FAILED Then RuntimeError WorldLoadError(world)
    Cls
    Text 0,0,"Loading the switchyard..."
    Flip
Wend

SetWorld world
camera=FindWorldEntityByName(world,"Camera")
PositionEntity camera,0,3,-4
PointEntity camera,FindWorldEntity(world,"gate")
gate=FindWorldEntity(world,"gate")
lamp=FindWorldEntity(world,"lamp")

While Not KeyDown(1)

    ; O opens the gate, C closes it, T toggles it; R resets the lamp's timer
    If KeyHit(24) Then CommandBehaviour gate,"door","open"
    If KeyHit(46) Then CommandBehaviour gate,"door","close"
    If KeyHit(20) Then CommandBehaviour gate,"door","toggle"
    If KeyHit(19) Then CommandBehaviour lamp,"timer","reset"
    If KeyHit(57) Then CommandBehaviour lamp,"timer","start"

    ; The commands take effect at the next UpdateWorld, which also steps
    ; the world's physics: a world with doors steps its own
    UpdateWorld
    RenderWorld

    Text 0,0,"O: open  C: close  T: toggle the gate   Space: start  R: reset the lamp's timer   Esc: exit"
    Text 0,20,"Gate: "+BehaviourState(gate,"door")+"   Lamp's timer: "+BehaviourState(lamp,"timer")

    Flip

Wend

FreeWorld world
End

Index