User Tools

Site Tools


Sidebar

hpl3:game:scripting:entity_components

Entity Components

Overview

What follows here is an overview of all the different entity components that are present. Entity components are used to extend the functionality of any derived iLuxEntity class.

To add a module simply call the creation function in the SetupAfterLoad() call in the entity script file. It is often required to save a handle to the returned class as well.

CharMover

Creation function: cLuxCharMover@ cLux_CreateEntityComponent_CharMover(iLuxEntity @apEntity, iCharacterBody @apCharBody)

General

This is used to more easily move around a character body. It controls movement forward, rotation and very simple avoidance behaviors.

PathFinder

Creation function: cLuxPathFinder@ cLux_CreateEntityComponent_PathFinder(iLuxEntity @apEntity)

General

This is used to add pathfinding capabilities. It relies on Path nodes being placed in the map and usually works together with CharMover (but this is not needed).

StateMachine

Creation function: cLuxPathFinder@ cLux_CreateEntityComponent_StateMachine(iLuxEntity @apEntity)

General

This adds a state machine to the entity. It can be used for pretty much every thing but is mainly used for AI purposes.

It is important to note that there can only be ONE statemachine in a class!

To use the statemachine, you need to create states right after having created the module. It is very important that all states are always created in the same order for all entities using the same script file (basically meaning you should not dynamically create states during the update loop or similar). For instance:

@mpStateMachine = cLux_CreateEntityComponent_StateMachine(mBaseObj);
mpStateMachine.AddState("Idle", eState_Idle);
mpStateMachine.AddState("Move", eState_Move);
mpStateMachine.AddState("Stop", eState_Stop);

Every state has a couple of actions (methods really) that are called in response to different events. These are:

Enter()
Called when the state is started.
Leave()
Called when the state is over. This is called before Enter of the new state!
Update(float afTimeStep)
Called every update.
SubStateOver(int alSubStateId)
Called when a sub state is over. More on substates below.
Message(int alMessageId)
This is any entity message that is intercepted. Can also be a custom message.
TimerUp(int alTimerId)
When a timer is over.

For each state, each of these functions get a name based on the syntax like this:
void State_[StateName]_[Action]
For instance the Enter() action in the state “Idle” is:

void State_Idle_Enter()

Note that you only have to define the actions that you want, so for instance a Stop state might only be something like:

void State_Stop_Enter() {
 //Stop the character
}
void State_Stop_Leave() {
 //Make the character move again
}

And skip Update, etc. (This is actually the best practice too!)

Sub State

A sub state is just like a state that runs along side the normal state. What makes it different is that they only last as long as the state that started it. Also sub states cannot change the normal state, only what the next substate will be. It can be sort of seen a smaller state machine inside a state. The sub states are created just like normal states but using the AddSubState(tString, int) method instead. Also note that substates can be shared between all of the states. They only belong to the a state in the sense that a sub state started in state X will only run while state X is active.

The function syntax for sub states is:
void SubState_[StateName]_[Action] For example:

void SubState_ThrowObject_Enter()

When a sub state is over, the state that started it has the action SubStateOver(int alSubStateId) called, where alSubStateId contains the id of the sub state.

Sub state has all the same actions as the normal state except for SubStateOver(int alSubStateId)

Another important feature of sub states is that there does not have to be one set. So it is okay to do:

 mpStateMachine.ChangeSubState(-1)

which set no state at all as sub state. This is actually the default setting when ever a new normal state is started.

Default State

The default state is a state that can be used by both normal states and sub states. To add it just write a state function with the name “Default” instead of a state name. (This means a state cannot be named “Default”.) Then this function will be called if the current state does not contain it. So for instance if you have something like:

void State_Default_Update(float afTimeStep){
...
}
 
void State_State1_Update(float afTimeStep){
...
}

If the state machine is currently in State1 then the Update for State1 will be called. However, if the current state is State2 and it do

hpl3/game/scripting/entity_components.txt · Last modified: 2017/04/20 07:57 by alexkalopsia