Timer (wait_time) not working?

Godot Version

4.0

Question

I am new to Godot. I followed some tutorials and now trying to build something simple to see what I’ve learned so far.

I am making a very simple Platformer. I made an ‘Arrow’ scene, and now having issues with the ‘attack’ animation.

Before I added a timer, the attack was working (without showing the arrow, since I added the arrow later), but I had another animation that showed the attack was working.

I added the arrow scene/script, and i think it’s correct there, it’s pretty simple:

extends Area2D

var speed = 300

func _ready():
	set_as_top_level(true)
	
func _physics_process(delta):
	#position += (Vector2.RIGHT*speed).rotated(rotation) * delta
	var direction = Vector2.RIGHT.rotated(rotation)
	position += direction * speed * delta


func _on_visible_on_screen_enabler_2d_screen_exited():
	queue_free()

And this is my player scene/script:

extends CharacterBody2D


var SPEED = 300.0
const JUMP_VELOCITY = -400.0

var bow_equipped = true
var bow_cooldown = 1.0
var arrow = preload("res://scenes/arrow.tscn")


# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

var is_attacking = false
@onready var timer = $Timer

func _ready():
	timer.wait_time = bow_cooldown  # Directly set the wait_time property
	timer.one_shot = true
	timer.connect("timeout", Callable(self, "_on_cooldown_timeout"))

func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	var direction = Input.get_axis("left", "right")
	
  # Handle attack and movement logic
	
	if Input.is_action_just_pressed("attack") and bow_equipped and is_on_floor():
		is_attacking = true
		SPEED = 0
		Vector2(0, 0)
		var arrow_instance = arrow.instantiate()
		arrow_instance.rotation = $Marker2D.rotation
		arrow_instance.global_position = $Marker2D.global_position
		add_child(arrow_instance)
		$AnimatedSprite2D.play("attack")
		timer.start()
		is_attacking = false
		
	elif not Input.is_action_pressed("attack"):
		is_attacking = false
		if direction == 0:
			$AnimatedSprite2D.play("idle")
		if direction != 0:
			$AnimatedSprite2D.play("walk")
		if direction != 0 and not is_on_floor():
			$AnimatedSprite2D.play("jump")
		SPEED = 300
	
	#flip character 
	if direction > 0:
		$AnimatedSprite2D.flip_h = false
	elif direction < 0:
		$AnimatedSprite2D.flip_h = true
	
	
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()
	
	
func player():
	pass 

func _on_cooldown_timeout():
	SPEED = 300

It’s giving me the following error: Invalid set index ‘wait_time’ (on base: ‘null instance’) with value of type ‘float’.

I read the documentation, and wait_time should accept the float value i gave to bow_cooldown, but I am pretty sure i must have missed something.

Is path for your timer node correct?

The problem is that the timer variable is null for some reason.

You can drag&drop the timer node into the code while holding the control key to create a proper reference to it, in case that’s the problem (incorrect node path maybe…).

If that’s not the problem then not sure… but you could also create the timer in code:

var timer

func _ready():
	timer = get_tree().create_timer()
	timer.wait_time = bow_cooldown  # Directly set the wait_time property
	timer.one_shot = true
	timer.connect("timeout", Callable(self, "_on_cooldown_timeout"))

That’s it! It’s actually pretty simple. Apparently for some reason the Timer node was deleted from the Nodes Tree, definitely by mistake, but the code was still there for the $Timer so i thought that wasn’t the issue.

I added the timer node again and now it works. Thanks!

1 Like

Many thanks! I added the Timer node again and now it works. Much appreciated :smiley:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.