Check if body is still inside Area2D after X amount of time passes

Godot Version

4

Question

Hello, I’m trying to do a damage system for my game. When a player moves into a “bad tile”, it calls the signal on the player hurtbox, takes away a life point and makes it invincible for 1.5 seconds. The thing is, I’m trying to figure out how to make the player take damage again if after the invincibility period they’re still placed in a bad tile. I tried doing it in _process(delta) but no matter what conditional I put, the process takes away all the health and bugs the game. Here is the code:

func _process(delta):
	if in_dmg_area && !invincible:
		health -= 1
		invincible = true
		inv.visible = true
		after_damage_timer.start()
	if health == 0:
		inv.visible = false

		timer.stop()
	
		$AnimatedSprite2D.play("Death")
		can_move = false

func _on_player_hurtbox_body_entered(body):
	in_dmg_area = true
	if body.name == "BadTiles" && !invincible:
		$HurtSound.play()
		$HitEffect.play("Hit")
		health -= 1
		set_health()
		
		invincible = true
		inv.visible = true
		after_damage_timer.start()
		
		if(health < 1):
			var lost = lost_game_scn.instantiate()
			stats_viewport.add_child(lost)
			lost.connect("restart", _restart_game)

func _on_player_hurtbox_body_exited(body):
	in_dmg_area = false

func _on_after_dmg_inv_timeout():
	invincible = false
	inv.visible = false
	
func set_health():
	match health:
		0:
			life_heart_01.set_frame(11)
			life_heart_02.set_frame(11)
			life_heart_03.set_frame(11)
		1:
			life_heart_01.set_frame(0)
			life_heart_02.set_frame(11)
			life_heart_03.set_frame(11)
		2:
			life_heart_01.set_frame(0)
			life_heart_02.set_frame(0)
			life_heart_03.set_frame(11)
		3:
			life_heart_01.set_frame(0)
			life_heart_02.set_frame(0)
			life_heart_03.set_frame(0)

add a variable (boolean) that never be false if the character never left the area2d

if it’s true, it will hurt the player after the cooldown wears off

Yes I already added that, it is in_dmg_area, it starts as false, turns to true when the player enters a bad tile, and turns again to false when it exits, but it is still taking away all the life in the _process function and I can figure out how to take away only 1 life point, make the player invincible again and so

yes sorry just noticed too
but i dont see you set the invincible to false?

Sorry I forgot to add the code, invincible is set to false when the timer reaches its timeout, I’ll add it now

it’s okay, so it actually can get to non-invincible, have you check how many times the timer stop? because it should be impossible for this

line of code to be true if invincible is not false
something has to - nvm

try change the && to and

Nope it is still wrong, I noticed that no matter if invincible is true or false the if statement always enters

even changing the && to and?

Yes but I think now I figured it out, I changed the order of the code and it seems to be working, now it is like this:

	if invincible == false and in_dmg_area == true and health >= 1:
		make_invincible()
		health -= 1
		set_health()

instead of this

if in_dmg_area && !invincible:
		health -= 1
		invincible = true
		inv.visible = true
		after_damage_timer.start()

and make_invincible() contains the logic to make the player invincible for 1.5 seconds.
Thank you for your help!

nice, glad it works, i thought the && made it failed, but it was not