StateClock

A state name and how long it's been in it, for state-machine entities (enemy AI, a player's attack/dodge/hurt states, a boss's phases). Call update(dt) once per frame, at the top of onUpdate(), then branch on state.

set() enters an open-ended state; setTimed() one that lasts a set time (isTimeUp(), remainingFraction()). justEntered() is true for the first update after a state is entered, for one-off entry work. Optionally, give it a sprite and play() switches its animation only when the name changes. Subclass and override onEnter() for anything else.

Properties

  • state (string) — The current state
  • previousState (string) — The state before this one
  • seconds (float) — Seconds since the state was entered, counted by update()
  • sprite (BGE.Sprite) — Optional: play() switches this sprite's animation
  • orientedSprite (BGE.DrawableOrientedSprite) — Optional: play() switches this sprite's oriented animation (playOrientedAnimation())
  • animation (string) — The animation play() last switched to

Example

CODE
m.clock = new BGE.StateClock("idle")
m.clock.sprite = m.sprite
' In onUpdate(dt):
m.clock.update(dt)
if m.clock.state = "windup"
if m.clock.seconds >= 0.4
m.clock.setTimed("strike", 0.25)
m.clock.play("strike")
end if
else if m.clock.state = "strike"
if m.clock.justEntered()
m.playSound("swing")
end if
if m.clock.isTimeUp()
m.clock.set("idle")
m.clock.play("idle")
end if
end if

Constructor

new StateClock(state?: string): StateClock

Parameters

  • state (string, optional, default: "\"\"") — the starting state (not "entered": justEntered() stays false)

Instance Methods

set(state: string, restart?: boolean): boolean

Enters state with no time limit. Setting the current state again does nothing (so it's safe to call every frame) unless restart is true, which enters it afresh.

Parameters

  • state (string)
  • restart (boolean, optional, default: false)

Returns

  • boolean — whether the state was entered

setTimed(state: string, durationSeconds: float): void

Enters (or re-enters) state for durationSeconds; isTimeUp() turns true once they've passed. The clock doesn't leave the state by itself.

Parameters

  • state (string)
  • durationSeconds (float)

Returns

  • void

update(dt: float): void

Advances the clock. Call once per frame.

Parameters

  • dt (float) — seconds

Returns

  • void

justEntered(): boolean

True during the first update after the state was entered (from the update() call until the next one), so entry work runs once.

Returns

  • boolean

isTimeUp(): boolean

Whether a timed state's duration has passed. Always false for a set() state.

Returns

  • boolean

remaining(): float

Seconds left of a timed state (0 for a set() state).

Returns

  • float

remainingFraction(): float

1 as a timed state starts, falling to 0 as its time runs out (0 for a set() state).

Returns

  • float

play(animation: string, restart?: boolean): void

Switches the sprite's animation if animation isn't the one play() last switched to, or restart is true. Needs sprite or orientedSprite set.

Parameters

  • animation (string)
  • restart (boolean, optional, default: false) — play it from the start even if it's already playing

Returns

  • void

playAnimation(animation: string, restart: boolean): void

protected

Parameters

  • animation (string)
  • restart (boolean)

Returns

  • void

onEnter(previousState: string): void

protected

Parameters

  • previousState (string)

Returns

  • void