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 BeforeRender( const CameraView: TViewVectors; const ParentParentWorldTransformation: TTransformation); virtual;
Protected procedure ParentChanged; virtual; deprecated 'Use ParentAfterAttach / ParentBeforeDetach or WorldAfterAttach / WorldBeforeDetach';
Protected procedure ParentAfterAttach; virtual;
Protected procedure ParentBeforeDetach; virtual;
Protected procedure WorldAfterAttach; virtual;
Protected procedure WorldBeforeDetach; virtual;
Protected function CanAttachToParent(const NewParent: TCastleTransform; out ReasonWhyCannot: String): Boolean; virtual;
Public procedure BeforeDestruction; override;
Public function World: TCastleAbstractRootTransform;

Properties

Public property Parent: TCastleTransform read FParent;
Public property ListenWorldChange: Boolean read FListenWorldChange write SetListenWorldChange;

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 BeforeRender( const CameraView: TViewVectors; const ParentParentWorldTransformation: TTransformation); virtual;

Event called right before rendering the parent TCastleTransform. Override this if you have to adjust the parent transformation strictly before the rendering.

Compared to adjusting the parent transformation in e.g. Update, adjusting the parent transformation here has advantages:

  1. There's no 1-frame delay between moving the thing that causes the change (like camera) and updating the thing that reacts to it (like a transformation with a billboard).

  2. We adjust transformation before rendering each occurrence of TCastleTransform in the viewport tree. This matters if given TCastleTransform may occur multiple times in the viewport tree, directly or through TCastleTransformReference.

  3. We adjust transformation before each render, so things are OK if given TCastleTransform is present in multiple viewports (see https://castle-engine.io/multiple_viewports_to_display_one_world).

For example, adjusting the TCastleTransform done by the TCastleBillboard is done using this method. The transformation in this case is determined by the camera and parent's parent transformation. The transformation has to be applied without any delay (even a 1-frame delay would be visible, as artifacts caused by billboard not perfectly aligned with the camera would be visible – blending sort along camera direction in sort3D is not perfect if billboard doesn't exactly match camera direction). And the transformation with billboard may be present in multiple viewports and/or multiple times in the same viewport tree.

Parameters
CameraView
Current camera vectors, in world coordinates, used to render the scene now.
ParentParentWorldTransformation

World transformation of the parent of the parent of this behavior. The name is kind of convoluted, but consistent: When the parent of this behavior has exactly one parent TCastleTransform (not zero, not more) then ParentParentWorldTransformation.Transform is equivalent to Parent.Parent.WorldTransform.

Protected procedure ParentChanged; virtual; deprecated 'Use ParentAfterAttach / ParentBeforeDetach or WorldAfterAttach / WorldBeforeDetach';

Warning: this symbol is deprecated: Use ParentAfterAttach / ParentBeforeDetach or WorldAfterAttach / WorldBeforeDetach

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

Protected procedure ParentAfterAttach; virtual;

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

Protected procedure ParentBeforeDetach; virtual;

Called before Parent changed, e.g. at the beginning of TCastleTransform.RemoveBehavior.

This is also called at the destruction of behavior (more precisely from BeforeDestruction now). Even in this case, we guarantee that Parent is still valid during this call, and not in half-destroyed state. Before the actual destructor of TCastleBehavior we do BeforeParentDetach and BeforeWorldDetach and set Parent to Nil.

Protected procedure WorldAfterAttach; virtual;

Called after Parent became part of World. Called only if ListenWorldChange is True.

Protected procedure WorldBeforeDetach; virtual;

Called before Parent stops being part of World. Called only if ListenWorldChange is True.

This is also called at the destruction of behavior (more precisely from BeforeDestruction now). Even in this case, we guarantee that Parent is still valid during this call, and not in half-destroyed state. Before the actual destructor of TCastleBehavior we do BeforeParentDetach and BeforeWorldDetach and set Parent to Nil.

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;

Public procedure BeforeDestruction; override;

This item has no description.

Public function World: TCastleAbstractRootTransform;

// Not used in the end, for now the TransformDesigning and these methods are commented out.

{ Create design-time things that are not displayed in the hierarchy so can be created after behavior selection in editor hierarchy } procedure DesigningBegin; virtual;

{ Destroy design-time stuff. } procedure DesigningEnd; virtual;

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.

Public property ListenWorldChange: Boolean read FListenWorldChange write SetListenWorldChange;

Set this to True to receive WorldBeforeDetach, WorldAfterAttach calls.


Generated by PasDoc 0.16.0-snapshot.