BehaviourState$ ( entity,id$ )

Parameters

entity - an entity with behaviours, from a loaded world or a copy of one

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

Description

Returns what a switch, door, timer or spawner is doing now, as a word.

A switch is active or inactive. A door is closed, opening, open, closing or blocked: blocked while something is in its way. A timer is idle, running or finished. A spawner is idle, running, full (it has its most, and waits for one to go), finished (it made its one) or faulted (it could not make one, and waits until it is started again).

It is the state at the last UpdateWorld: a command or response given since takes effect at the next one. Use it for a HUD ("The gate is open"), a quest step that waits for a door, or a hint when a spawner has its most. Its state stays when the behaviour is switched off or its object hidden; BehaviourRunning says whether it is running.

Other kinds of behaviour, such as spin or a behaviour of your own code, return "". An ID the entity has no behaviour for is a runtime error.

In a Shared World session every copy returns the state the host, or a server room's server, has: a player who joins while a door opens finds it opening, part of the way, and one who joins while a timer runs finds it running, with the time it has left.

See also: CommandBehaviour, BehaviourRunning, BehaviourEnabled, WorldEventBehaviour.

Example

; BehaviourState Example
; ----------------------

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

; The Switchyard: a plate, the gate it opens, a lamp's timer and a spawner
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,4,-6
PointEntity camera,FindWorldEntity(world,"plate")
plate=FindWorldEntity(world,"plate")
gate=FindWorldEntity(world,"gate")
lamp=FindWorldEntity(world,"lamp")
pickup=FindWorldEntity(world,"pickup")

While Not KeyDown(1)

    ; Space turns the plate on and off, as standing on it does
    If KeyHit(57) Then
        If BehaviourState(plate,"switch")="active" Then
            CommandBehaviour plate,"switch","deactivate"
        Else
            CommandBehaviour plate,"switch","activate"
        EndIf
    EndIf

    UpdateWorld
    RenderWorld

    Text 0,0,"Space: turn the plate on or off   Esc: exit"
    Text 0,20,"Plate: "+BehaviourState(plate,"switch")
    Text 0,40,"Gate: "+BehaviourState(gate,"door")
    Text 0,60,"Lamp's timer: "+BehaviourState(lamp,"timer")
    Text 0,80,"Pickup's spawner: "+BehaviourState(pickup,"spawner")

    Flip

Wend

FreeWorld world
End

Index