FSM
Contains functions for controlling npc behaviors.
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.seek(seekTarget)
Creates a seek state.
Parameters
Example
Make an Entity seek a player.
function onPlayerJoin(player)
seek = FSM.state.seek(player)
seek.run()
end
state.flee(fleeTarget)
Creates a flee state.
Parameters
Example
Make an Entity flee away from a player.
function onPlayerJoin(player)
flee = FSM.state.flee(player)
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 patrolling.
function onStart()
patrolPoints = {Vector3.new(0,0,2), Vector3.new(2,0,-1)}
patrol = FSM.state.patrol(patrolPoints)
waitState = FSM.state.wait(2)
patrol.addRule(patrol.isDone, waitState)
waitState.addRule(waitState.isDone, patrol)
patrol.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(attackTarget, config)
Calls a pre-made FSM that patrols, seek, attacks and flee.
Parameters
- attackTarget — Entity or Player to attack, flee and seek.
- 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 onPlayerJoin(player)
config = {
patrolRadius = 5,
seekRadius = 5,
attackRadius = 2,
fleeHealth = 0.5,
weaponCooldown = 1
}
FSM.behavior.patrolSeekAttackAndFlee(player, config)
end