Class TCastleBehavior

Unit

Declaration

type TCastleBehavior = class(TCastleComponent)

Description

Behaviors can be attached to TCastleTransform to perform specific logic, for example implement creature movement. This implements a simple component-system for TCastleTransform. Using behaviors allows to attach independent functionality like TCastleBillboard, TCastleSoundSource, creature AI and more. CGE provides implementation for some behaviors, and you can create your own too.

You implemement a descendant of TCastleBehavior, typically overriding its Update method, and add it to TCastleTransform by TCastleTransform.AddBehavior. Inside TCastleBehavior, access the TCastleTransform instances by Parent.

The API of TCastleBehavior is deliberately a subset of the TCastleTransform API, for example both have Update method. Using TCastleBehavior is just simpler and more efficient, as TCastleBehavior doesn't have own transformation, children and it cannot render something. If this is too limiting, remember you can always implement more powerful behaviors by creating TCastleTransform descendants instead of TCastleBehavior descendants, and accessing the TCastleTransform.Parent from a child.

Hierarchy

Overview

Methods

Protected procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); virtual;
Protected procedure ParentChanged; virtual;
Protected function CanAttachToParent(const NewParent: TCastleTransform; out ReasonWhyCannot: String): Boolean; virtual;

Properties

Public property Parent: TCastleTransform read FParent;

Description

Methods

Protected procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); virtual;

Continuously occuring event, for various tasks.

Parameters
RemoveMe
Set this to rtRemove or rtRemoveAndFree to remove this component from Parent after this call finished. rtRemoveAndFree additionally will free this item. Initially it's rtNone when this method is called.
Protected procedure ParentChanged; virtual;

Called after Parent changed, at the end of TCastleTransform.AddBehavior, TCastleTransform.RemoveBehavior.

Protected function CanAttachToParent(const NewParent: TCastleTransform; out ReasonWhyCannot: String): Boolean; virtual;

Check can this behavior be added to NewParent. When this returns False, it has to set also ReasonWhyCannot. When overriding this, you can use e.g. this code to make sure we are the only behavior of given class:

function TCastleBillboard.CanAttachToParent(const NewParent: TCastleTransform;
  out ReasonWhyCannot: String): Boolean;
begin
  Result := inherited;
  if not Result then Exit;

  if NewParent.FindBehavior(TCastleBillboard) <> nil then
  begin
    ReasonWhyCannot := 'Only one TCastleBillboard behavior can be added to a given TCastleTransform';
    Result := false;
  end;
end;

Properties

Public property Parent: TCastleTransform read FParent;

Parent TCastleTransform of this behavior. Change it by doing TCastleTransform.AddBehavior, TCastleTransform.RemoveBehavior.

Nil if this behavior is not yet added to a parent.


Generated by PasDoc 0.16.0.