Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unit training Queue

Discussion in 'Scripting' started by jonny99, Nov 13, 2017.

  1. jonny99

    jonny99

    Joined:
    Oct 12, 2017
    Posts:
    3
    Hi Guys!
    I am trying to make an RTS (Real Time Strategy) game with a soldier training queue. There will be a GUI button that when pressed adds a soldier to a training queue, it will take 3 seconds to train the soldier. If you click more than once it will queue the soldiers for training, 2 soldiers is 6 seconds, 3 soldiers is 9 seconds etc.

    The issues that I am having are that 1. The soldiers all spawn immediately instead of on a timer, and 2. They don't queue. Any help you could give me would be greatly appreciated, thanks!

    Here is the code I have written...

    Code (CSharp):
    1. public class GUIButtonUnitSpawn : MonoBehaviour
    2. {
    3.     public Rect buttonPosition;
    4.     public Texture btnTexture;
    5.     public GameObject spawnUnit;
    6.     public GameObject spawnObjPos;
    7.     public float spawnTime;
    8.  
    9.     private Queue<GameObject> unitQueue = new Queue<GameObject>();
    10.     private enum SpawnState { TRAINING, IDLE };
    11.     private SpawnState state = SpawnState.IDLE;
    12.  
    13.     private void OnGUI()
    14.     {
    15.         if (GUI.Button(buttonPosition, btnTexture))
    16.         {
    17.                 unitQueue.Enqueue(spawnUnit);
    18.                 if (unitQueue.Count >= 0)
    19.                 {
    20.                     state = SpawnState.TRAINING;
    21.                     if (state != SpawnState.IDLE)
    22.                         StartCoroutine(TrainingUnit());
    23.                 }
    24.             }
    25.         }
    26.  
    27.     IEnumerator TrainingUnit()
    28.     {
    29.         for (int i = 0; i < unitQueue.Count; i++)
    30.         {
    31.             SpawnUnit(unitQueue);
    32.             yield return new WaitForSeconds(1.00f / spawnTime);
    33.         }
    34.         unitQueue.Dequeue();
    35.         state = SpawnState.IDLE;
    36.         yield break;
    37.     }
    38.  
    39.     void SpawnUnit(Queue<GameObject> spawnUnit)
    40.     {
    41.         Instantiate(spawnUnit.Peek(), spawnObjPos.transform.position, spawnObjPos.transform.rotation);
    42.     }
    43. }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I haven't tested this, but is this what you're going for?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class GUIButtonUnitSpawn : MonoBehaviour {
    6.     public Rect buttonPosition;
    7.     public Texture btnTexture;
    8.     public GameObject spawnUnit;
    9.     public GameObject spawnObjPos;
    10.     public float spawnTime;
    11.  
    12.     private Queue<GameObject> unitQueue = new Queue<GameObject>();
    13.     private enum SpawnState { TRAINING, IDLE };
    14.     private SpawnState state = SpawnState.IDLE;
    15.  
    16.     private Coroutine trainingCoroutine;
    17.  
    18.     private void OnGUI() {
    19.         if(GUI.Button(buttonPosition, btnTexture)) {
    20.             unitQueue.Enqueue(spawnUnit);
    21.             if(trainingCoroutine == null) {
    22.                 trainingCoroutine = StartCoroutine(TrainingUnit());
    23.             }
    24.         }
    25.     }
    26.  
    27.     IEnumerator TrainingUnit() {
    28.         WaitForSeconds trainingTime = new WaitForSeconds(spawnTime);
    29.         state = SpawnState.TRAINING;
    30.         while(unitQueue.Count > 0) {
    31.             yield return trainingTime;
    32.             SpawnUnit(unitQueue.Dequeue());
    33.         }
    34.         state = SpawnState.IDLE;
    35.         trainingCoroutine = null;
    36.     }
    37.  
    38.     void SpawnUnit(GameObject spawnUnit) {
    39.         Instantiate(spawnUnit, spawnObjPos.transform.position, spawnObjPos.transform.rotation);
    40.     }
    41. }
     
    jonny99 likes this.
  3. jonny99

    jonny99

    Joined:
    Oct 12, 2017
    Posts:
    3
    @jeffreyschoch Yes this is exactly what I was looking for, this is a really big help thank you!!
     
    LiterallyJeff likes this.
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    and thank YOU for using code tags ;)