Timer not counting down or timing out?

Godot Version

4.3

Question

So I have a timer that is supposed to run at the beginning of an enemy state, but sometimes the timeout wouldn’t trigger and the enemy would be stuck in the state. When I print the time_left inside the process function, it shows that timer.start() gets executed but the time left stays at zero and never changes. I run into this issue when the enemy goes into this state from the same state.

can you show us your code?

this is the script of the walk state which causes the issue:

extends State
class_name EnemyWalkState

@export var actor: Enemy
@export var animator: AnimationPlayer
@export var walk_timer: Timer
@export var speed: int

signal saw_player
signal no_player

var player_seen: bool
var direction_var

func _ready() -> void:
	set_physics_process(false)
	in_state = false
	walk_timer.timeout.connect(walk_timer_timeout)

func enter_state():
	print("enter walk")
	in_state = true
	walk_timer.wait_time = 2
	walk_timer.start()
	#setting random direction to walk
	direction_var = randi_range(0, 1)
	if direction_var == 0:
		speed *= -1
	set_physics_process(true)

func _physics_process(_delta: float) -> void:
	animator.play("walk")
	print(walk_timer.time_left)
	if (speed > 0 and actor.ground_on_right) or (speed < 0 and actor.ground_on_left):
		actor.velocity.x = speed
	else:
		actor.velocity.x = 0


func walk_timer_timeout():
	print("timeout")
	if  actor.player_in_range and in_state:
		print("saw player")
		saw_player.emit()
	else:
		print("no player")
		no_player.emit()
	walk_timer.stop()

func exit_state():
	set_physics_process(false)
	walk_timer.stop()
	in_state = false

Is the timer set as a one-shot? I’m not sure how you are using it, but if it was one-shot, then you wouldn’t need walk_timer.stop() at the end of your walk_timer_timeout() function.

Unrelated,

	walk_timer.start(2)

is the same as:

	walk_timer.wait_time = 2
	walk_timer.start()

Based on what you’re saying, I would guess your enter_state() code is not being run when you transition from the same state.