Dealing damage to the player

Godot Version

4.3

Question

How do i actualy use timers in a _physics_process()

variable context
inHurtZone Variable that i have changing based on if in an area
playerHealth player health
hurtDamage the amount of damage inflicted onto the player (in this case it’s set to 10)

if inHurtZone == true: await get_tree().create_timer(0.5).timeout playerHealth -= hurtDamage else: pass

When i go in the area, instead of my health gradually decreasing, in goes away basicly instantly

how can i solve this?

You know _physics_process is called 60 times per second right? Unless you have a big hp, the player will die fast because is losing 600 hp per second. If you want to do a DoT damage you need to use a Timer node instead and in your _physics_process you’ll just make sure to start/stop the timer.

  1. Create a Timer node
  2. Connect the timeout signal to your code
  3. Set the timer node autoplay to false and one-shot to false too
  4. Set the wait time to 0.5 (or any value you want to wait between the damage ticks)
# Code

# Create a var to know if you're already_applying the dot
var is_applying_dot := false


func _physics_process(delta) -> void:
	# Check if is inside the damage zone and if the dot is already applying
	if inHurtZone == true and is_applying_dot == false:
		# Set the variable to true and start the timer
		is_applying_dot = true
		$Timer.start(0.5)

	# If player is outside the damage area but was taking damage before makes the damage stop
	elif inHurtZone == false and is_applying_dot == true:
		is_applying_dot = false
		$Timer.stop()


func _on_Timer_timerout() -> void:
	playerHealth -= hurtDamage

Thanks, this worked :smiley:

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