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

Creating a list in order of character speed

Discussion in 'Scripting' started by UniKael, Oct 12, 2021.

  1. UniKael

    UniKael

    Joined:
    Sep 19, 2021
    Posts:
    11
    Im trying to make a turn based system where character speed determines the order in which characters attack. Im fairly new to unity and i tried looking around and fiddling with the code but i cant seem to figure out how to make a list getting my characters on screen and then creating a list of them in order of highest speed.
     
  2. SunnyValleyStudio

    SunnyValleyStudio

    Joined:
    Mar 24, 2017
    Posts:
    67
    Hey!

    Creating a turn based system is a bit of work so I can only try to outline a basic idea.

    I would start by giving each character that takes part in the turn based process a custom script like:

    Code (CSharp):
    1. public class TurnTaker : MonoBehaviour
    2. {
    3.     public int GetSpeed()
    4.     {
    5.         //ask some AgentSkills script about the speed and return it.
    6.         return 0;
    7.     }
    8. }
    Next the simplest thing to do would be to have some sort of TurnBasedSystem script:
    Code (CSharp):
    1. public class TurnBasedSystem : MonoBehaviour
    2. {
    3.     Queue<TurnTaker> turnQueue = new Queue<TurnTaker>();
    4.  
    5.     public void CreateAQueue()
    6.     {
    7.         //Get all turn takers
    8.         TurnTaker[] turnTakers = FindObjectsOfType<TurnTaker>();
    9.  
    10.         //Sort it by speed from highest speed to lowest speed
    11.         turnTakers = turnTakers.OrderByDescending((val) => val.GetSpeed()).ToArray();
    12.  
    13.         //Additional sorting to ensure that player and enemy moves interchangably ?
    14.  
    15.         //Create a queue that will allow you to get the correct character first
    16.         turnQueue = new Queue<TurnTaker>(turnTakers);
    17.     }
    18.  
    19.     //Check if turn is done
    20.     public bool IsTurnDone() => turnQueue.Count <= 0;
    21.  
    22.     //get the next character to move
    23.     public TurnTaker GetNextCharacter()
    24.     {
    25.         if (IsTurnDone())
    26.             return null;
    27.         return turnQueue.Dequeue();
    28.     }
    29. }
    Here we call CreateAQueue() method when we want to get all the characters and prepare them for movement - at the start of a new turn.

    Next all we need to do is to call GetNextCharacter() to get the TurnTaker that should go next. Having it you can set it as an active character or call on it some method.

    Next all you need to do is to ask at the end of the movement IsTurnDone() - if it is then create a new queue and if not GetNextCharacter().

    Of course you would add to it IsFightWon() or some check if there is even a point of taking the next turn.


    Basically that is the idea of how you would create this kind of system in code.

    I hope it helps!
     
    UniKael likes this.