So in my game I have an enemy set up. When something enters the enemies attack range, if it is the player, I want the enemy to attack the player. I do this by running a function on the player which deals damage to it. However, this damage only gets applied once. To combat this, I've made a variable that triggers when the player enters the enemies attack range and then constantly runs every few seconds to attack the player until it leaves the enemies attack area. The issue with this is that when the while loop is activated, everything freezes.
My Code:
func _on_EnemyCollisionDetection_area_entered(area):
enemyInRange = true
if not area.is_in_group("player"):
pass
if area.is_in_group("player"):
while enemyInRange == true:
if get_node("AttackDelay").is_stopped() == true:
get_node("AttackDelay").start()
rng.randomize()
var randomNumber = rng.randf_range(2.0, 7.0)
var damageDealt = int(round(randomNumber))
area.takeDamage(damageDealt)
yield(get_tree(), "idle_frame")
func _on_EnemyCollisionDetection_area_exited(area):
enemyInRange = false
Everything works fine and I get no errors in the console, however once the while loop is activated the game and my computer freeze and the only way to get out of it (the way that I've discovered and seems to work) is to press ctrl+alt+delete
and just cancel from there (my school computer has task manager blocked.)
I see no reason for this to happen? It is an older school computer although it should run smoothly still.
Thank you for any help :)