the node timer not work.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By WinstXD320

I have a problem with node timer. i made for code, but always when i want to start the time never start or if start, never call the function when the time end. Maybe is the structure of the code.

extends CharacterBody2D

var  SPEED_ATTACK = 300.0
const JUMP_VELOCITY = -400.0
var IS_ATTACK: bool = true
var WAIT_TIME: Timer = Timer.new()

func _ready():
	self.add_child(WAIT_TIME)
	WAIT_TIME.wait_time = 2
	WAIT_TIME.timeout.connect(func():When_time_end())
	
func _physics_process(delta):
	up_direction
	Attack()
	move_and_slide()
	
func Attack():
	if IS_ATTACK:
		velocity.y = SPEED_ATTACK
		if is_on_floor():
			WAIT_TIME.start()
			
			print(IS_ATTACK)
	else:
		velocity.y = -SPEED_ATTACK
		
func When_time_end():
	print(IS_ATTACK)
	IS_ATTACK = false
:bust_in_silhouette: Reply From: jgodfrey

You have some syntax issues. Modifying your code, something like this should work:

var WAIT_TIME: Timer = Timer.new()

func _ready():
	self.add_child(WAIT_TIME)
	WAIT_TIME.wait_time = 2
	WAIT_TIME.timeout.connect(When_time_end)	
	WAIT_TIME.start()

func When_time_end():
	print("timeout!")

Note, I’m starting the timer directly in _ready() here. You can start it however you want, but you need to ensure that it DOES get started…