Help Please.How to stop timer when my player dies, and create an highscore timer?

Godot Version

3

I want when my player dies the main timer stops counting time and then after it show my record time.

My Timer

extends Label

var time_alive: float = 0.0

func _ready():
	# Set the initial text of the Label
	text = str(int(time_alive))
	# Start processing every frame
	set_process(true)

func _process(delta):
	# Increment the time_alive by the time passed since the last frame
	time_alive += delta
	# Update the Label's text to show the elapsed time in seconds
	text = str(int(time_alive))


My player


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().playerDied(pop_effect_instance)
	queue_free()

Please paste your code with formatting, between three ticks like so:

```
type or paste code here
```

The forum will add this with the </> button or ctrl+e


It seems like you are manually creating a timer that counts up on _process, what is going wrong with this implementation?

Okay, i will add my code as you said, with this timer there nothing wrong, i just want to stop my timer when player dies and then create an high score label that will say my maximum time.I try do this but it didnt work.

I have changed my code is this good for you?

What part of that didn’t work?

Code formatting looks good!

1 Like

Sorry now in my country is 11 30 pm so i am going to sleep, see you tomorrow.

Hi,thats my code

extends Label

var time_alive: float = 0.0
var running: bool = true

func _ready():
	# Set the initial text of the Label
	text = str(int(time_alive))
	# Start processing every frame
	set_process(true)

func _process(delta):
	if running:
		# Increment the time_alive by the time passed since the last frame
		time_alive += delta
		# Update the Label's text to show the elapsed time in seconds
		text = str(int(time_alive))

func stop_timer():
	running = false
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")
onready var timer_label = $Timer1

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().playerDied(pop_effect_instance)
	timer_label.stop_timer()
	queue_free()


Your player doesn’t have a child node $Timer1 that’s a child of the Game node. You could use export or move the label under the player; probably best with a canvas layer

1 Like

I will try soon,thanks

When i move my label under the player it starts to move with him and queue_free(), how to fix that

I didn’t anticipate queue_free() on the player, bad advice from me on that. export is probably your best bet, or using a root path

die():
    $/root/Game/Timer1.stop_timer()
1 Like

why there is an error : path expected after $


I have changed smth, and now it works great,thanks.I want to add another label for high score how to make it copy a time of my main label?

$HighScoreLabel.text = "High Score: " + $Timer1.text

You can add the text together with a prefix to the new label

1 Like


I cant fix it (

Again, your script is on the node Highscore, this has no children so it can’t search for $Highscore.

In fact, you are trying to get the label that the script is on, you can use self or edit the property directly.

Then you need to consider the path to Timer1, from Highscore we need to go to the parent (../) then we can search for the timer (Timer1)

text = "High Score: " + $../Timer1.text
1 Like

Thanks i will try soon

Thanks it worked now.

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