Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | smavanat |
I have a variable health for a bat enemy and I want to set it to maxHealth within a stats node attached to the main bat scene. I am trying to use get and set to see if the bat’s health reaches zero, and then to send out a signal to the main Bat node to destroy itself
Stats code:
using Godot;
using System;
public class Stats : Node
{
[Export] public int maxHealth = 1;
public int health { get {return health;} set { health = value; setHealth(health);}}
public override void _Ready()
{
health = maxHealth;
}
private int setHealth(int value)
{
if(value <=0)
{
EmitSignal("noHealth");
}
return health;
}
[Signal]
delegate void noHealth();
}
Bat code (getting the signal):
public override void _Ready()
{
area2D = GetNode<Area2D>("Hurtbox");
area2D.Connect("area_entered", this, "onHurtboxEntered");
stats = GetNode<Stats>("Stats");
stats.Connect("noHealth", this, "onStatsNoHealth");
}
Bat code(Method I want to execute):
private void onHurtboxEntered(Area2D area)
{
if(area is SwordHitbox swordHitbox)
{
this.swordHitbox = swordHitbox;
stats.health -= 1;
knockback = this.swordHitbox.KnockbackVector * 60;
}
}
private void onStatsNoHealth()
{
QueueFree();
}
There are no errors in the IDE, but the engine tells me that the setHealth method in the stats script creates a StackOverflowException error.
Can you try without using QueueFree()
?
Put a GD.Print in there or something.
I think the destroying could cause problems.
juppi | 2022-08-05 13:39
Thanks, but I have found a solution by placing the if statement in setHealth into the set for health
smavanat | 2022-08-05 19:07