Trouble with timers

Godot Version 4.3

func _process(delta):
	if  playerone.living == false:
		$PlayerOneRespawn.start()
	if playertwo.living == false:
		$PlayerTwoRespawn.start()
	if playerthree.living == false:
		$PLayerThreeRespawn.start()

I want the code to check to see if one of the characters is gone, and if so, start timer, but the timers keep restarting before they finish, how do I just have it run through the code once?

I am still new to Godot, but the problem here is that you have it running through physics process, which is calling it every frame.
What you need is a function upon player death that activates once.
So you can use a signal or some sort of value to determine when a player dies, and upon death call the function to respawn.

2 Likes

That might work in the main player script, but I’m trying to handle spawning/despawning with the level script.

Have you tried setting the timer to oneshot?

1 Like

You have it on _process() function. It will keep starting the timer every frame as long as the conditions are met. You can add more conditions to avoid this.

5 Likes

Or use signals and start the appropriate function once instead of polling the if each frame.

3 Likes

Have a on death signal on player.
Connect to that signal from level script.
Emit the signal from player script when he dies.
The connected function on level script plays once instead of every frame. Write all further logic there.
My newb two cents.

2 Likes

You could try checking the Timer’s is_stopped() method before starting the timer. I’m assuming that calling start() on a Timer that is already started will reset the countdown.
Timer — Godot Engine (stable) documentation in English.

That said, @grzegorzgrzelczyk has given a better answer that will likely result in a cleaner architecture. I’m just providing an alternative if you want a quick and easy option. I think both have their value.

2 Likes

I have found a solution that seems to work, by adding another variable, thanks for your help :slight_smile:

func _process(delta):
	if playerone.living == false and readytorespawnone == true:
		$PlayerOneRespawn.start()
		readytorespawnone = false
	if playertwo.living == false and readytorespawntwo == true:
		$PlayerTwoRespawn.start()
		readytorespawntwo = false
	if playerthree.living == false and readytorespawnthree == true:
		$PlayerThreeRespawn.start()
		readytorespawnthree = false
2 Likes

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