Timer never goes down

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

So I’m trying to make a powerup that makes you have a shield around you. I managed to make it so that when it comes into contact with the powerup, the shield sprite is visible.

Now I’m struggling with making the shield sprite dissapear. I tried this with timer nodes and with what the answer of this post said: Using yield(get_tree().create_timer(x)) - Archive - Godot Forum.

I also followed this tutorial: space shooter tut powerup godot at DuckDuckGo but that didn’t work either.

both scripts:

extends Area2D

var hp = 3

var PowerupTime = 2

var shieldT = Timer.new()

func _ready() -> void:
	shieldT.set_wait_time(2)
	shieldT.set_one_shot(true)
	self.add_child(shieldT)

func _on_Area2D_area_entered(area: Area2D) -> void:
	if area.collision_layer == 2:
		hp -= 1

func _process(delta: float) -> void:
	if hp <= 0:
		get_parent().queue_free()
		queue_free()
		
	if global.powerup1_active == true:
		shieldT.start()
		if !shieldT.is_stopped():
			get_parent().get_node("Shield").visible = true
			print(shieldT.time_left)
		else:
			get_parent().get_node("Shield").visible = false
			global.powerup1_active = false
			
		print(global.powerup1_active)

extends Area2D

onready var shieldTimer := get_parent().get_node("ShieldTimer")

var hp = 3
var shieldTime := 1

func _ready() -> void:
	get_parent().get_node("Shield").visible = false

func _on_Area2D_area_entered(area: Area2D) -> void:
	if area.collision_layer == 2:
		hp -= 1

func _process(delta: float) -> void:
	if hp <= 0:
		get_parent().queue_free()
		queue_free()
		
	if global.powerup1_active == true:
		shieldTimer.start(shieldTime)
		if !shieldTimer.is_stopped():
			get_parent().get_node("Shield").visible = true
			print(shieldTimer.time_left)
		else:
			global.powerup1_active = false

P.S I’m quite new to GDscript and Godot so I probably missed out something very important. Thanks :slight_smile:

Can you add just the timer part of the code?

ramazan | 2022-04-14 13:31

i added them a bit from memory, so it might not be exact

David000 | 2022-04-14 13:48

i added them a bit from memory, so it might not be exact

It’s not possible for people to look at code and make useful suggestions if it’s not the code you’re actually using.

jgodfrey | 2022-04-14 15:46

well, from what i have shown, please give me some suggestions and ideas and why my code isn’t working?

David000 | 2022-04-14 16:06

:bust_in_silhouette: Reply From: Inces

It is You just start your timer endlessly in process(). You should have noticed that, because your print statement must always show starting time in console. Your timer is never stopped, always running, so it never reaches this part of the code, that turns shield to invisible.

You already used boolean “powerupactive” to border if statements in process. You can reposition it, so the timer is only started once, visibility set to true or false also just once. Of course You should use “timeout” signal or setget for the most ellegant sollution, but whatever works is fine

thank you so much! <3

David000 | 2022-04-14 20:24