Skip to main content

FSM

Contains functions for controlling npc behaviors.

info

This module can only be used in Entity scripts.

State Functions​

state.idle()​

Creates an idle state.

Example​

Make an Entity start in idle state.

function onStart()
idle = FSM.state.idle()
idle.run()
end

state.patrol(patrolPoints, config)​

Creates a patrol state.

Parameters​

  • patrolPoints — Lua table with Vector3 elements. (Optional).
  • config — Lua table with the state configuration. (Optional).

Example​

Make an Entity patrol around certain points.

function onStart()
patrolPoints = {Vector3.new(0,0,2), Vector3.new(2,0,-1)}

patrol = FSM.state.patrol(patrolPoints)
patrol.run()
end

state.attack(config)​

Creates an attack state, this state will make the entity attack the closest player.

Example​

Make an Entity attack the closest player.

function onPlayerJoin(player)
seek = FSM.state.seek(player)
seek.run()
end

state.seek()​

Creates a seek state.

Example​

Make an Entity seek the closest player.

function onStart()
seek = FSM.state.seek()
seek.run()
end

state.flee()​

Creates a flee state.

Example​

Make an Entity flee away from the closest player.

function onStart()
flee = FSM.state.flee()
flee.run()
end

state.create(callback, stateName)​

Creates a new custom state with a custom functionality.

Parameters​

  • callback — Lua Function with the state logic.
  • stateName — state name used for debugging. (Optional).

Example​

Make an Entity rotate clockwise.

function rotateClockwise()
Rotation.turn(1)
end

function onStart()
customState = FSM.state.create(rotateClockwise, "rotate clockwise")
customState.run()
end

state.wait(seconds)​

Creates a state that waits for X amount of seconds.

Parameters​

  • seconds — the amount of seconds that the state is going to wait.

Example​

Make an Entity wait 2 seconds between moving randomly.

function moveRandomly()
local randomPoint = Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
Movement.moveTo(randomPoint)
end

function onStart()
moveState = FSM.state.create(moveRandomly, "move randomly")
waitState = FSM.state.wait(2)

-- if the move state is done, go to the wait state
moveState.addRule(moveState.isDone, waitState)
-- if the wait state is done, go to the move state
waitState.addRule(waitState.isDone, moveState)

moveState.run()
end

Behavior Functions​

behavior.patrolAndAttack(config)​

Calls a pre-made FSM that patrols and attacks.

Parameters​

  • config — Lua table with the state configuration. (Optional).

Example​

Make an Entity patrol in a radius of 3, and attack when the player is closer than 2 units.

function onStart()
config = {
patrolRadius = 3,
attackRadius = 2
}

FSM.behavior.patrolAndAttack(config)
end

behavior.patrolSeekAttackAndFlee(config)​

Calls a pre-made FSM that patrols, seek, attacks and flee.

Parameters​

  • config — Lua table with the state configuration. (Optional).

Example​

Make an Entity patrol in a radius of 5, seek the player if he is closer than 5 units, attack the player if he is closer than 2 units, and flee if below 50% hp, and a weapon cooldown of 1 second.

function onStart()
config = {
patrolRadius = 5,
seekRadius = 5,
attackRadius = 2,
fleeHealth = 0.5,
weaponCooldown = 1
}

FSM.behavior.patrolSeekAttackAndFlee(config)
end

Functions​

stop()​

Stops the current state.

Example​

Make an Entity patrol and attack, and after 10 seconds stop the behavior.

function onStart()
FSM.behavior.patrolAndAttack()
Task.wait(10)
FSM.stop()
end