BehaviourSetting$ ( entity,key$[,default$][,id$] )

Parameters

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

key$ - the setting's name, such as speed

default$ - optional: the text to return when the setting is not set; "" when left out

id$ - optional: the ID of the behaviour to read; when left out, the first of the entity's behaviours that has the setting

Description

Returns a setting of one of an entity's behaviours of the world's own code, as text. The World Editor's Behaviours section edits these settings, and a behaviour's code reads them, usually in its Start method:

Self.speed = Float(BehaviourSetting(owner, "speed", "2"))

When the setting is not set, it returns default$, which is the value the code uses then. The World Editor finds these calls in a behaviour's code to give a new behaviour its settings, with these values.

An entity with two behaviours that have the same setting reads the first of them unless id$ names one; an id$ the entity has no behaviour for is a runtime error. The settings of the built-in behaviours are not text, so for them it returns default$.

See also: WorldBehaviourID, BehaviourData, WorldEntityMetadata, SendWorldEvent.

Example

; BehaviourSetting Example
; ------------------------

; The behaviours sample's own code: its guard, and the runner that runs it
Include "../../../samples/behaviours/behaviours/behaviours.bb"

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

; The coins world, whose guard has settings the World Editor gave it
world=LoadWorldAsync("../../../samples/behaviours/coins.world")
While Not WorldReady(world)
    If WorldLoadState(world)=WORLD_LOAD_FAILED Then RuntimeError WorldLoadError(world)
    Cls
    Text 0,0,"Loading the coins world..."
    Flip
Wend

SetWorld world
guard=FindWorldEntityByName(world,"Guard")
camera=FindWorldEntityByName(world,"Camera")
PositionEntity camera,0,1.6,9.5
PointEntity camera,guard

; The guard's code reads these when it starts; the third value is the one it uses when a setting is not set
target$=BehaviourSetting(guard,"target","player")
speed#=Float(BehaviourSetting(guard,"speed","2"))
range#=Float(BehaviourSetting(guard,"range","5"))

While Not KeyDown(1)

    ; Arrow keys walk the camera, which the guard chases once it is near enough
    If KeyDown(200) Then MoveEntity camera,0,0,0.05
    If KeyDown(208) Then MoveEntity camera,0,0,-0.05
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    UpdateWorld
    UpdateBehaviours world
    RenderWorld

    Text 0,0,"Arrow keys: walk   Esc: exit"
    Text 0,20,"BehaviourSetting(guard,"+Chr(34)+"target"+Chr(34)+") = "+target$
    Text 0,40,"speed = "+speed#+" units a second, range = "+range#+" units"
    Text 0,60,"You are "+EntityDistance(camera,guard)+" units from the guard"
    Flip

Wend

FinishBehaviours world
FreeWorld world
End

Index