#4.1 Setting SyncVar Initial values inside an Inherited class?

3 messages · Page 1 of 1 (latest)

tender steeple
#

I have base class of Creature.
Creature has variables for Health and MaxHealth.
In a derived class of TestMob, is there a way for me to set their initial values?
fishnet version: 4.1.0R.Pro

public abstract class Creature : NetworkBehaviour
{
    private readonly SyncVar<float> _health = new();
    public float Health { get => _health.Value; 
        [Server] set => _health.Value = value; }

    private readonly SyncVar<float> _maxHealth = new();
    public float MaxHealth { get => _maxHealth.Value; 
        [Server] set => _maxHealth.Value = value; }

    protected virtual void Awake() {
        _health.OnChange += OnSyncHealthChange;
    }

    private void OnSyncHealthChange(float prev, float next, bool asServer) {
        CallOnHealthChange(next);// client side event, not shown for brevity 
    }

    [Server]
    public virtual void TakeDamage(float amount) {
        if (!IsSpawned) { return; }
        Debug.Log($"{this.gameObject.name} was hit for <color=red>{amount}</color>");
        if ((Health -= amount) <= 0.0f) {
            KillCreature(); // not shown for brevity 
            return;
        }
    }
}
public sealed class TestMob : Creature
{
    [Server]
    protected override void Awake() {
        base.Awake();
        Health = 50;
        MaxHealth = 100;
    }
}
charred dock
#

Hey, you could use OnStartServer to set initial values

tender steeple
#

I'll give that a shot @charred dock Thank you