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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

how to structure multiple units with different unit abilities.

Discussion in 'Scripting' started by Walra, Mar 4, 2018.

  1. Walra

    Walra

    Joined:
    Mar 4, 2018
    Posts:
    2
    Hey, trying to sort out how to structure multiple units

    Let's say there are 2 types of units. Harvesters and Fighters. They are both derived from the Unit class.

    I want to be able to call PerformTick on the Unit class, and have HarvesterUnit or FighterUnit take an action.
    In this way I can have many different units with unique actions, just by writing a script for each.

    What is a good way to achieve this effect?

    What should I use to build a modular action system? (Inheritence? Scriptable Objects? Some sort of component based design?)


    public class Unit : MonoBehaviour {
    public void PerformAction(){
    //Tell the unit to take their action (somehow?)
    }
    }

    public class Harvester : Unit {
    public void PerformAction(){
    //DO HARVESTER STUFF
    //Walk 3 tiles
    //collect resource
    }
    }

    public class Fighter : Unit {
    public void PerformAction(){
    //DO FIGHTER STUFF
    //walk 10 tiles
    //fight enemy
    }
    }
     
  2. carl010010

    carl010010

    Joined:
    Jul 14, 2010
    Posts:
    139
    Please make sure you're using code tags https://forum.unity.com/threads/using-code-tags-properly.143875/
    The inline code tags are better than nothing, but inserting the code makes it a lot easier to read.

    Let me know if you have any questions.

    Code (CSharp):
    1. public MainClass : MonoBehaviour
    2. {
    3.   List<Unit> units = new List<Unit>(); //https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.90).aspx                          
    4.   //http://www.tutorialsteacher.com/csharp/csharp-list                                                                                  
    5.  
    6.   Awake()//Or Start() https://unity3d.com/learn/tutorials/topics/scripting/awake-and-start                                  
    7.   {
    8.     units.Add(new Haverster());
    9.     units.Add(new Fighter());
    10.   }
    11.  
    12.   Update()
    13.   {
    14.     foreach(Unit u in units)
    15.     {
    16.       u.PerformAction();
    17.     }
    18.   }
    19. }
    20.  
    21. public class Unit
    22. {
    23.   abstract public void PerformAction();
    24.   //https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract                              
    25. }
    26.  
    27. public class Harvester : Unit
    28. {
    29. override public void PerformAction()
    30.    {
    31.     //DO HARVESTER STUFF                                                                                                                
    32.     //Walk 3 tiles                                                                                                                      
    33.     //collect resource                                                                                                                  
    34.   }
    35. }
    36.  
    37. public class Fighter : Unit
    38. {
    39.   override public void PerformAction()
    40.    {
    41.     //DO FIGHTER STUFF                                                                                                                  
    42.     //walk 10 tiles                                                                                                                      
    43.     //fight enemy                                                                                                                        
    44.   }
    45. }
    46.  
    Quick mock code may or may not have the correct capitlization and spelling
     
    Walra likes this.
  3. Walra

    Walra

    Joined:
    Mar 4, 2018
    Posts:
    2
    Ah, I see what was going on!
    I was keeping a reference to UNIT for both fighter/harvester instead of keeping a reference to the actual inherited class.
    So I was calling the wrong method.
    Thanks!
     
  4. carl010010

    carl010010

    Joined:
    Jul 14, 2010
    Posts:
    139
    No problem man