- Home /
RTS Unit Creation Bar Queue System
Hi, I'm creating an RTS game where you can build troops from a dropdown menu, I've set up little bars that show the progress of the current troop being made. I have one problem that I'm not sure how to fix, how can I make only one bar work at a time based off what I clicked? Right now if I click on any of them they start building like they are suppose too but I only want one troop to be built at a time then after its built it moves on to the next thing that was clicked after it started being built. I hope this question isn't too confusing ask questions If you don't know what I'm talking about and Ill get back to you otherwise thanks for reading!! Any help will be appreciated!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Queue : $$anonymous$$onoBehaviour {
//void Awake()
//{
List <GameObject> BarracksQueue = new List<GameObject> ();
//}
class TroopBuilderJob //Can go in its own script, or the bottom of another script.
{
public enum ToBeBuilt {Infantry}; //$$anonymous$$ake a public Enum including your troop types.
public int AmountToBeBuilt = 0; //The amount of the troop type to be built.
}
void Update()
{
Queue<TroopBuilderJob> TroopBuilderQueue = new Queue<TroopBuilderJob>(); //This will hold your build queue. You'll want to loop through this and pull info from it.
bool UseExisting = false; //Use existing TroopBuilderJob?
foreach (TroopBuilderJob i in Queue.BarracksQueue)
{
if (i.ToBeBuilt == Infantry) {
//If an existing TroopBuilderJob exists, we'll use it ins$$anonymous$$d of making a new one.
i.AmountToBeBuilt++; //Adding the amount by 1.
UseExisting = true; //Sets the "UseExisting" to true.
}
}
if (!UseExisting) {
//Since no previous build job matches the troop type we're attempting to make..we're going to make a new one.
TroopBuilderJob tb = new TroopBuilderJob ();
tb.ToBeBuilt = Infantry; //Set this to the type of unit you're building.
tb.AmountToBeBuilt = 1; //I assume you're going to increment by 1.
Queue.BarracksQueue.Add (tb); //Adds the newly created TroopBuilderJob to the master list...which you will parse through.
}
}
}
That's what I got so far in one script and its not working too well because theres errors everywhere what am I doing wrong..
I understand your frustration. But it looks like you don't understand the simplest of c#. I'm not going to write your script for you. I've done more than enough as is. I suggest you learn more core coding before you continue.
Answer by Cynikal · Oct 22, 2016 at 10:02 PM
I had built a similar system, but for an unrelated process.
You can make a class file..along the lines of:
class TroopBuilderJob //Can go in its own script, or the bottom of another script.
{
public TroopType ToBeBuilt = Whatever; //Make a public Enum including your troop types.
public int AmountToBeBuilt = 0; //The amount of the troop type to be built.
}
Then, on your actual "builder" script,
Utilize a Queue:
public static Queue<TroopBuilderJob> TroopBuilderQueue = new Queue<TroopBuilderJob>(); //This will hold your build queue. You'll want to loop through this and pull info from it.
Do something along the lines of:
bool UseExisting = false; //Use existing TroopBuilderJob?
foreach(TroopBuilderJob i in MasterBuilderScript.TroopBuilderQueue)
{
if (i.ToBeBuilt == Infantry) {
//If an existing TroopBuilderJob exists, we'll use it instead of making a new one.
i.AmountToBeBuilt++; //Adding the amount by 1.
UseExisting = true; //Sets the "UseExisting" to true.
}
}
if (!UseExisting)
{
//Since no previous build job matches the troop type we're attempting to make..we're going to make a new one.
TroopBuilderJob tb = new TroopBuilderJob();
tb.ToBeBuilt = Infantry; //Set this to the type of unit you're building.
tb.AmountToBeBuilt = 1; //I assume you're going to increment by 1.
MasterBuilderScript.TroopBuilderQueue.Add(tb); //Adds the newly created TroopBuilderJob to the master list...which you will parse through.
}
That'll basically search for an existing class, if it's in the queue.
You can modify it if you'd like, but i'd recommend deleting the TroopBuilderJob from the Queue once the AmountToBeBuilt is 0.
+1
However "TroopBuilder" seems a strange name for that class. "TroopBuildJob" would be more descriptive.
True. Which is actually I have it named in my game..
"TruckQueue", "TruckQueueJob", etc.
A little confusing but thank you Ill try to figure it out from what you gave me
m just not sure what everything is doing, Im trying to decipher it so I know what I need to do. Here are some questions I have though, where would the first part of the script go? The second part would go in some kind of manager for the troops, but what does the bool useexisting mean, not sure what the bottom part is about. To be honest a quick description of what each line does would help if you have the time sorry if im making you do too much but thanks for helping
I added some notes to further describe.
It's pretty basic man.
The bottom portion of code is the thing that either adds to an existing buildjob, or makes its own. You'd add that portion to your button or whatever.
Don't forget to mark this as an answer. As it's a highly valid answer.
Your answer
Follow this Question
Related Questions
RTS rectangle selection system 3 Answers
Examples of TBS/Heavily Gui-driven Games in Unity? 2 Answers
Selection and Deselection 1 Answer
GUI Button and for loop not working 2 Answers
to attach a Blood gauge to enemies 2 Answers