BBMOD_AnimationStateMachine

A constructor defined in BBMOD_AnimationStateMachine.gml

Extends BBMOD_StateMachine

new BBMOD_AnimationStateMachine(_animationPlayer)

Description

A state machine that controls animation playback.

Arguments

Name Type Description
_animationPlayer Struct.BBMOD_AnimationPlayer The animation player to control.

Properties

Name Description
AnimationPlayer The state machine's animation player.

Inherited properties

Name Description
Finished If true then the state machine has reached its final state.
OnEnter A function executed on the start of the state of the state machine. It should take the state machine as the first argument. Default value is undefined.
OnExit A function executed on the end of the state machine. It should take the state machine as the first argument. Default value is undefined.
OnPostUpdate A function executed in the update method after the current state is updated. It should take the state machine as the first argument and delta time as the second argument. Default value is undefined.
OnPreUpdate A function executed in the update method before the current state is updated. It should take the state machine as the first argument and delta time as the second argument. Default value is undefined.
OnStateChange A function executed when the state changes. It should take the state machine as the first argument and its previous state as the second argument. Default value is undefined.
Started If false then the state machine has not yet entered its initial state.
State The current state or undefined.

Inherited methods

Name Description
add_state Adds a state to the state machine.
change_state Changes the state of the state machine and executes BBMOD_StateMachine.OnStateChange.
finish Enters the exit state of the state machine.
start Enters the initial state of the state machine.
update Executes function for the current state of the state machine (if defined).

Example

Following code shows an animation state machine which goes to the "Idle" state on start and independently on the current state switches to "Dead" state when variable hp meets 0. After the death animation ends, the state machine enters the final state and the instance is destroyed.

/// @desc Create event
destroy = false;
animationPlayer = new BBMOD_AnimationPlayer(model);

animationStateMachine = new BBMOD_AnimationStateMachine(animationPlayer);
animationStateMachine.OnEnter = method(self, function () {
    animationStateMachine.change_state(stateIdle);
});
animationStateMachine.OnExit = method(self, function () {
    destroy = true;
});
animationStateMachine.OnPreUpdate = method(self, function () {
    if (hp <= 0 && animationStateMachine.State != stateDead)
    {
        animationStateMachine.change_state(stateDead);
    }
});

stateIdle = new BBMOD_AnimationState("Idle", animIdle);
animationStateMachine.add_state(stateIdle);

stateDead = new BBMOD_AnimationState("Dead", animDead);
stateDead.on_event(BBMOD_EV_ANIMATION_END, method(self, function () {
    animationStateMachine.finish();
}));
animationStateMachine.add_state(stateDead);

animationStateMachine.start();

/// @desc Step event
animationStateMachine.update(delta_time);
if (destroy)
{
    instance_destroy();
}

/// @desc Clean Up event
animationPlayer = animationPlayer.destroy();
animationStateMachine = animationStateMachine.destroy();

See also

BBMOD_Animation, BBMOD_AnimationInstance, BBMOD_AnimationPlayer, BBMOD_AnimationState

Do you find this page helpful?

Copyright © 2024, BlueBurn. Built on April 18, 2024 using GMDoc.