By 'only once' do you mean 'can be called only once', or 'do things once when called and switch to the next one'?
Here's state machine that I've found somewhere in the web. So if it is the latter, you just call exit_state() at the end of anycode you execute. But if it is the former, then you will need a variable somewhere that will store the fact that this state had already been called (there are diferent ways to do that).
state-machine.gd
extends Node
class_name StateMachine
var currentState : SMState
func _process(delta: float) -> void:
if currentState:
currentState.state_update(delta)
func _physics_process(delta: float) -> void:
if currentState:
currentState.state_phys_update(delta)
func change_state(newState : SMState = initialState):
newState.state_enter()
currentState = newState
state_machine_state.gd
extends Node
class_name SMState
@onready var owner_unit : Unit = get_parent()
var statemachine : StateMachine
# called by state machine right before this SMstate set as currentstate, every time it happens
func state_enter():
pass
# updated by _process() in runtime, when this SMstate is current state
func state_update(delta: float) -> void:
pass
# updated by _phys_process() in runtime, when this SMstate is current state
func state_phys_update(delta: float) -> void:
pass
func state_exit(state) -> void:
statemachine.change_state(state)