• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DJGhostViper · Oct 22, 2016 at 09:09 PM · guirtsqueueunitscreation

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DJGhostViper · Oct 23, 2016 at 02:26 AM 0
Share
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Queue : MonoBehaviour {
 
     //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}; //Make 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 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.
             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..

avatar image Cynikal ♦ · Oct 23, 2016 at 04:30 AM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 22, 2016 at 10:06 PM 0
Share

+1

However "TroopBuilder" seems a strange name for that class. "TroopBuildJob" would be more descriptive.

avatar image Cynikal ♦ Bunny83 · Oct 22, 2016 at 10:08 PM 0
Share

True. Which is actually I have it named in my game..

"TruckQueue", "TruckQueueJob", etc.

avatar image DJGhostViper · Oct 22, 2016 at 10:45 PM 0
Share

A little confusing but thank you Ill try to figure it out from what you gave me

avatar image Cynikal ♦ DJGhostViper · Oct 22, 2016 at 10:47 PM 0
Share

Which part is confusing?

avatar image DJGhostViper · Oct 22, 2016 at 11:31 PM 0
Share

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

avatar image Cynikal ♦ DJGhostViper · Oct 23, 2016 at 12:32 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Many units, best performance practices 2 Answers

Examples of TBS/Heavily Gui-driven Games in Unity? 2 Answers

GUI Button and for loop not working 2 Answers

unit selection issue 0 Answers

RTS Style Game - Handling units, ideas? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges