From GDScript to C#

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By VarionDrakon

Guys, help out, I don’t know how to rewrite this code in C#…

signal health_updated(health)
signal killed ()

export (float) var max_health = 100
onready var health = max_health



func kill():
pass



func _set_health(value):
var prev_health = health
health = clamp(value, 0, max_health)
health i= prev_health:
emit_signal("health_updated", health)
health == 0:
kill()
emit

I partially know how this miracle will look in C#, but it turns out crooked, can you recommend something?

Code by the way is not Mine, so do not scold :slight_smile:

:bust_in_silhouette: Reply From: cihad
   public class SampleNode: Node2D
{
    [Signal]
    public delegate void HealthUpdated(int health);

    [Signal]
    public delegate void Killed();

    [Export] public float MaxHealth = 100;
    public float Health;
    public override void _Ready()
    {
        Health = MaxHealth;
    }

    public void Kill()
    {
        
    }

    public void SetHealth(float value)
    {
        var prevHealth = Health;
        Health = Mathf.Clamp(value, 0, MaxHealth);
        EmitSignal(nameof(HealthUpdated),new[] {Health});
        Health = 0;
        Kill();
    }
}

Thank you friend, helped out! I apologize for my stupidity…

VarionDrakon | 2020-10-20 15:33