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

making a Queue of commands

Discussion in 'Scripting' started by NatalieBaldwin, Mar 31, 2017.

Thread Status:
Not open for further replies.
  1. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    I basically want to make a Queue<Function> that will run the first item in the queue repeatedly until the function tells it that it is done. Think about this as if it were a line of customers. Customers tell the clerk what they need and the customer leaves when they're done. The clerk doesn't tell the customers anything, it just helps them until they're done.. I want my functions to act like the customers, they know what they want and they know when they are done. The problem is that Queue<Function> doesn't exist. How do i go about putting functions in a queue? I essentially want to make a list of commands inputted by the player that the game will execute in order if the player doesn't cancel the queue early. All RTS games have this feature but no RTS tutorial shows this feature. I've looked through so many tutorials and can't find it anywhere. An example of how i want this to work:

    player holds shift and clicks 2 locations on the map, putting 2 move commands into the queue. The Update loop says "do first thing in queue forever and always" move command says "go to location and if you are close, dequeue" dequeue will remove the front-most item from the queue, which would be the currently running function. Then the 2nd instance of "move" will start running. Commands that should be able to be placed into the queue can be movement, attack, casting spells, using items, etc. I know there are several ways to execute this but i can't find actual examples of any of them. I've found not even a single line of code towards doing what i want. There are so few forum posts about this subject that a google search using similar key words keep leading me back to my own forum post.
     
  2. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    I've thought of a way of doing this. If i can boil down all my functions to using the same parameter (which i think i can do) then i just enqueue the parameter in 1 and the name of the function in a 2nd queue. Then write a few if statements to check the 2nd queue and run the function with the parameter from the 1st queue. This isn't optimal and i'd still like to know how to run commands through a queue. But it'll work for my game, i'm pretty sure i can boil all unit function parameters into 1 RaycastHit.
     
  3. Kwinten

    Kwinten

    Joined:
    Jan 25, 2015
    Posts:
    49
    It most definitely does exist. A Queue<Func<bool>> for example, could achieve exactly this. Func<bool> is simply a function which returns a boolean. You could say that when the function returns true, you can dequeue the next function in the queue.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
  5. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    Because it's tourist season and i'm overworked and got confused and made a new post because i didn't realise my last post was for the exact same thing.
     
  6. joker_yash96

    joker_yash96

    Joined:
    May 9, 2019
    Posts:
    7
    How to Execute the Function stored in Queue?
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    Please don't hijack and necro threads.

    Thanks.
     
    joker_yash96 likes this.
Thread Status:
Not open for further replies.