[C#] Setting up player/enemy classes, there has to be a better way

So I followed a tutorial online to set up player and enemy classes.

I have one file BasePlayer that sets up the template that the player will have like speed, etc, and then has a given Class.

I have another file BaseClass that sets up the template that classes will use like the base stats of the class, etc.

I then have two other files, an BaseAlienClass and a BasePlayerClass where each has within it multiple Public Classes that outline specific different classes like Alien Scientist, Alien Fighter, and player classes like Matt, Ashley, Steven (the main characters of the game).

And when I want to make a new enemy in battle, I just call the CreateFighter method and give it the level and class.

But I’m thinking long-term, and if I have a file for Aliens and a file for Players, then I’ll probably end up with a file for Animals, Thugs, Random stuff, etc, etc, etc. It feels like the system would get bloated with ten, twenty, thirty, maybe more files, depending on the number of different enemies I want in the game.

I’ve only been using C#, been making this game, for about a month. I can’t seem to track down a solid path to follow online.

First off, I’d love if enemies had some form of inheritance. So like:

ENEMY

  • Alien

  • Scientist

  • Fighter

  • Human

  • Criminal

  • Politician

  • Wildlife

  • Bear

  • Snake

  • Wolf

  • Random

  • Etc

  • Etc

  • Etc

So that it would be easier to set up classes. Alien Scientist and Alien Fighter would inherit from Alien so their race would automatically be “Alien” and perhaps the aliens would have something in common. From there, I could just setup the main differences between Scientist and Fighter instead of having to write a long version of both from scratch.

I know of the Inheritance that my BaseClass uses as like a template for actual classes, but I don’t think that’s the same kind of inheritance I’m looking to have.

I just really want a simple way to setup a lot of enemies that have a varying amount of stats/abilities/etc.

I’d appreciate any help anyone could give.

And like I put in the title, I’m using C Sharp C#.

I think it is a good idea to have a base class that can represent universal traits. Speed, health ect. Wether enemy or not can go into each unit.

I would then have a custom script per character, if required, ex, wizard might need a different script that swordsman. The call to use main wpn, aux wpn can be the same but contents with in, will be different. This means you can have one common mng that will instruct a wizard, warrior, spy, whatever to fire main wpn. Or defend, or use aux wpn or pick up etc.

Use this architecture. It will help. It will allow you to add entirely new character classes, frog, soldier, fairy, Orc… And so on with little adjustment beyond the characters code itself.

1 Like

You want to do a quick google search on composition versus inheritance.

The basic idea is to build up each entity with a bunch of sub components.

2 Likes

The question to ruin all your fine tuned class system is where to put Alien Bear? The Unity have nice component architecture. You should design components based on their responsibility. What attributes represent the unit in your game logic? One component. How does the unit moves - does it walk, fly or swim or maybe it’s an alligator who can both swim and walk? Another component or bunch of them.
You create a unit by combining some of those components on some game object and save it as a prefab you can instantiate later using GameObject.Instantiate method provided by Unity.
During unit lifetime you may activate or deactivate appropriate components when the unit decides to do some action.
I highly recommend you the books on design patterns, and especially game design patterns. Google for them.

2 Likes

Thank you for the replies. I will read and consider them and get back after I get done watching a bunch of scripting videos on Unity’s web site.

1 Like

A beginner who wants to look at the tutorials before asking us to solve all their problems? That’s so refreshing to hear. Sounds like you are off to a good start.

I can’t stress enough how important learning on your own is. Keep it up!

I guess what I’d like to see work would be a system like:

public class Character {
public int attack;
public int defense;
pubic string faction;
public string race;
public List<Spell> Spells = new List<Spell>();
}

class Enemy : Character {
//inherits attack, defense, race, spells but isn't supplying a value
faction = "enemy";
}

class Alien : Enemy {
//inherits enemy faction
//inherits spells but isn't supplying a value
race = "alien";
defense = 0; //naked alien
attack = 0; //no weapon
}

class AlienFighter : Alien {
//inherits enemy faction and alien race
//inherits lack of spells but isn't supplying a value
armorValue = 10; //fighter has better armor
armorValue = 5; //fighter has a weapon
}

class AlienScientist : Alien {
//inherits enemy faction and alien race
//inherits lack of armor and lack of weapon
Spells.Add(new Laser()); //adds ability to use Laser spell
}

I’m not sure if this code is right, but I tried to write it in the way I imagined it.

1 Like

i would embrace the component system for this, just more flexiable, expecially if you got interfaces in place and weapons all have somehting like iFireable etc

1 Like

This system can be done. But a better system is something like this.

public Life : MonoBehaviour {
    float HitPoints;
}

public Armour : MonoBehaviour {
    float armourValue;
}

public Weapon : MonoBehaviour {
    float damage;
}

// And so forth

Then to build an alien fighter you simply add Life, Armour and Weapon components, with appropriate values set in the inspector.

If you do need custom components for the alien and human players you can always create an AlienWeapon that inherits from Weapon. ITs generally better to keep your inheritance structure wide and shallow, rather then deep.

2 Likes

Thank you again for the suggestions and constructive criticism. I appreciate it greatly. I will take everything you all have said into consideration, do some more research/learning, and see what I can come up with.