Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by matthewr, Jan 20, 2015.

  1. matthewr

    matthewr

    Joined:
    Dec 9, 2014
    Posts:
    21
    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#.
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    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.
     
    matthewr likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    TonyLi and matthewr like this.
  4. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    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.
     
    matthewr and Kiwasi like this.
  5. matthewr

    matthewr

    Joined:
    Dec 9, 2014
    Posts:
    21
    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.
     
    Kiwasi likes this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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!
     
  7. matthewr

    matthewr

    Joined:
    Dec 9, 2014
    Posts:
    21
    I guess what I'd like to see work would be a system like:

    Code (csharp):
    1.  
    2. public class Character {
    3. public int attack;
    4. public int defense;
    5. pubic string faction;
    6. public string race;
    7. public List<Spell> Spells = new List<Spell>();
    8. }
    9.  
    10. class Enemy : Character {
    11. //inherits attack, defense, race, spells but isn't supplying a value
    12. faction = "enemy";
    13. }
    14.  
    15. class Alien : Enemy {
    16. //inherits enemy faction
    17. //inherits spells but isn't supplying a value
    18. race = "alien";
    19. defense = 0; //naked alien
    20. attack = 0; //no weapon
    21. }
    22.  
    23. class AlienFighter : Alien {
    24. //inherits enemy faction and alien race
    25. //inherits lack of spells but isn't supplying a value
    26. armorValue = 10; //fighter has better armor
    27. armorValue = 5; //fighter has a weapon
    28. }
    29.  
    30. class AlienScientist : Alien {
    31. //inherits enemy faction and alien race
    32. //inherits lack of armor and lack of weapon
    33. Spells.Add(new Laser()); //adds ability to use Laser spell
    34. }
    35.  
    I'm not sure if this code is right, but I tried to write it in the way I imagined it.
     
    MilenaRocha likes this.
  8. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    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
     
    matthewr likes this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This system can be done. But a better system is something like this.

    Code (CSharp):
    1. public Life : MonoBehaviour {
    2.     float HitPoints;
    3. }
    4.  
    5. public Armour : MonoBehaviour {
    6.     float armourValue;
    7. }
    8.  
    9. public Weapon : MonoBehaviour {
    10.     float damage;
    11. }
    12.  
    13. // 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.
     
    TonyLi and matthewr like this.
  10. matthewr

    matthewr

    Joined:
    Dec 9, 2014
    Posts:
    21
    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.