I am making a horror game with a monster that has 3 different stages. How would i change the pathfinding etc. of the monster over time?
Probably with a state-machine
I have been using godot for about a year so i need a more detailed explanation
do you know what a statemachine is?
Yes. However i do not know how exactly they work.
Okay. What exactly do you want to change in the different stages of the boss?
Scripting a state machine can be as complicated as you’d like. A two-state machine could be represented by a bool, true/false
var chasing_player: bool = false # our state variable
func _process(delta: float) -> void:
if chasing_player: # our chase state
speed += delta
chase()
else: # # # # # # # not chase state
wander()
A step up from this, supporting any number of states could use an enumerated type.
enum EnemyState {
WANDERING,
INVESTIGATING,
CHASING,
EATING
}
var my_state: EnemyState = EnemyState.WANDERING
func _process(delta: float) -> void:
if my_state == EnemyState.WANDERING:
wander()
elif my_state == EnemyState.INVESTIGATING:
travel_to(last_interest_point)
elif my_state == EnemyState.CHASING:
speed += delta
chase()
elif my_state == EnemyState.EATING:
health += delta
else:
assert(false, "Invalid state!")
Another helpful function is state transitions for setting values relevant to each state.
func transition_state(new_state: EnemyState) -> void:
if new_state == EnemyState.CHASING or my_state == EnemyState.CHASING:
speed = 6 # reset speed when chasing begins or ends
my_state = new_state # apply new state
You can see this gets much larger, and with more states it keeps growing.
Some will make scripts to be attached as children or resources for each state, then use the active state’s function each _process
.
var active_state = $States/Wandering
func _process(delta: float) -> void:
active_state.process(self)
func transition_state(new_state: NodePath) -> void:
var next_state = get_node(new_state)
active_state.transition_away(self)
next_state.transition_to(self)
active_state = next_state
Hopefully that is enough to get started, as you can see state machines can start small and get much larger and complicated, so it’s best to get in there and make a mess of it!