Guys please help. How to create delay timer

Godot Version

3

Question

I want to create a timer when my player dies after 3 seconds changes the scene to the menu. My player code

extends KinematicBody2D

var speed = 500 var acceleration = 800 var deceleration = 1000 var velocity = Vector2.ZERO var axis = Vector2.ZERO var rotation_speed = 500 var target_rotation = 0.0 var current_rotation = 0.0 var return_speed = 5 var MAX_ROTATION_DEGREE = 30 var PopEffect = preload(“res://Scenes/pop.tscn”)func _ready(): self.connect(“area_entered”, self, “_on_Area2D_area_entered”) func _physics_process(_delta):axis.x = Input.get_action_strength(“right”)-Input.get_action_strength(“left”)
axis.y = Input.get_action_strength(“down”)-Input.get_action_strength(“up”)

axis = axis.normalized() * speed

velocity = velocity.linear_interpolate(axis, 0.025)

velocity = move_and_slide(velocity)func _process(delta):if Input.is_action_pressed(“right”):
target_rotation = deg2rad(MAX_ROTATION_DEGREE)
elif Input.is_action_pressed(“left”):
target_rotation = deg2rad(-MAX_ROTATION_DEGREE)
else:
target_rotation = 0.0

current_rotation = lerp_angle(current_rotation, target_rotation, return_speed * delta)
rotation = current_rotation
rotation = clamp(rotation, -PI/8, PI/8)func _on_Area2D_area_entered(area): if area.is_in_group(“Bullet”): die() print(“bullet”) if area.is_in_group(“Enemy”): die() print(“enemy”) if area.is_in_group(“Border”): die() print(“border”)func die(): print(“you are dead”) var pop_effect_instance = PopEffect.instance() pop_effect_instance.position = position pop_effect_instance.emitting = true get_parent().add_child(pop_effect_instance) queue_free()

to create a one-time timer you can do:

yield(get_tree().create_timer(3), "timeout")
change_to_menu()

Put that in the method where your player dies

EDIT:
my original comment was for Godot 4.x

2 Likes

So i dont need to create timer node for this ?.Only this script ?

Technically Yes

1 Like

Thanks,i cant now try,i will try later.

1 Like

I am getting an error the method change the scene isnt declared …
so i just change it with this
func die():
print(“you are dead”)
var pop_effect_instance = PopEffect.instance()
pop_effect_instance.position = position
pop_effect_instance.emitting = true
get_parent().add_child(pop_effect_instance)
queue_free()
yield(get_tree().create_timer(3), “timeout”)
get_tree().change_scene(“res://Scenes/Menu.tscn”)
but it dont change the scene so i dont understand why

thats because you use queue_free() → your object deletes itself before the timer runs out. A fix would be to call a method on the parent of the player:

func die():
    print(“you are dead”)
    var pop_effect_instance = PopEffect.instance()
    pop_effect_instance.position = position
    pop_effect_instance.emitting = true
    get_parent().playerDied(pop_effect_instance)
    queue_free()

And the parent needs this method:

func playerDied(pop_effect_instance) -> void:
    add_child(pop_effect_instance)
    yield(get_tree().create_timer(3), “timeout”)
    get_tree().change_scene(“res://Scenes/Menu.tscn”)
1 Like

I assume you copied the code. Replace the ’ " ’ with your own. They are not completely vertical

2 Likes

Also the playerDied method has to be in the parent of the player

1 Like

I will try soon thanks

i am new at coding can you explain what you mean i dont understand well.

How to do that

yes as you said there is an error but i dont know how to fix it


:confused:

Put the “playerDied” method in your Game.gd script then it should work fine

Thanksss sooo much it worked.Thank you

1 Like

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