Edogmonkey
Edogmonkey

Reputation: 55

Create superclass with abstract variables and nonabstract methods in C#

Basically, the superclass should contain variables (or properties, whichever one works) that will be set in the subclasses and should also contain methods that all the subclasses will use. I don't know if there is a way to do this without using 2 classes, one that's an interface that contains the variables and another class that contains the methods, but I'd assume that there is. I'm pretty new to C# as you might imagine.

Just to clarify, this is for a project in Unity and the superclass will be a general character class that all of the subclasses (characters) will use.

Edit: Many other variables and methods will be added later but here's a cursory preview of what it should be

using UnityEngine;
using System.Collections;

public abstract class CharacterClass : MonoBehaviour {

    int MaxHitPoints { get; set; }
    int ArmorRating { get; set; }
    int Speed { get; set; }
    int Strength { get; set; }
    int Agility { get; set; }

    void changeHP(int change)
    {
        MaxHitPoints += change;
    }

}

Upvotes: 0

Views: 720

Answers (2)

Xarbrough
Xarbrough

Reputation: 1461

Object oriented programming concepts are still usable in Unity to build entities from components.

using UnityEngine;

public abstract class Character : MonoBehaviour
{
    public int HitPoints { get; set; }

    public virtual void ChangeHP(int amount)
    {
        HitPoints += amount;
    }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("DeathTrigger"))
        {
            Die();
        }
    }

    protected virtual void Die()
    {
        Debug.Log("I'm dead.");
    }
}

public class LordOfTerror : Character
{
    protected override void Die()
    {
        base.Die();
        Debug.Log("But I also come back from the dead very soon.");
        HitPoints = 100;
    }
}

But it is true that this kind of programming only works for small inheritance trees and only with a few limitations or workarounds you would have to get used to in Unity. Generally, it makes sense to embrace components as much as possible. Here's a variation on the example showing how you could combine object-oriented with composition:

using System;
using UnityEngine;

public abstract class StatAttribute
{
    public int current;
    public int min;
    public int max;

    public void Change(int amount)
    {
        current += amount;
    }
}

public sealed class Health : StatAttribute
{
    public event Action tookDamage;

    public bool isAlive
    {
        get { return current > min; }
    }

    public void Damage(int amount)
    {
        base.Change(-amount);
        // also fire an event to make other components play an animation etc.
        if(tookDamage != null)
            tookDamage();
    }
}

public sealed class Speed : StatAttribute
{
    public int boost;

    public int GetFinalSpeed()
    {
        return base.current * boost;
    }
}

Usually, you start with individual components attached to GameObjects. All combined make up your Character. You have things like Health, CharacterMovement, PlayerInput, Animation, etc. At some point your notice that code is duplicated and this is where it might make sense to share certain base classes between components.

Upvotes: 0

Fattie
Fattie

Reputation: 12641

the superclass will be a general character class

? but wait, there's really no such thing - Unity is an ECS system.

All you can do is add components to GameObjects. (That is to say, behaviors - all you can do is add behaviors (renderers, bone animators, timers, cameras, colliders, whatever) to GameObjects

There is no inheritance or anything like inheritance in Unity; it is not even vaguely OO. Using Unity is much like using say Photoshop!

(Of course, the language that happens to be used currently as of writing, to write components in Unity is an OO language, but that's irrelevant, could change tomorrow. That doesn't make Unity in any way OO.)

Upvotes: 2

Related Questions