- Home /
How to store information in a Que correctly?
Good afternoon, this is the first time i Post on the forums so please excuse me if something is misplaced or the such.
I have a stockpile that needs to replenish its goods. To do this. it sends a request from a logistics depot for a worker and provides this information to a worker.
Location of where to Pick up the goods.
Location of where to Deliver the goods.
The amount of Goods to carry.
The Type of goods to carry.
If the logistics depot does not have a free worker available at the time, the stock pile needs to put the 'order' in a FIFO que to try again after a set amount on time ie 5 to 10 seconds later.
Now my problem is this. How to i store these values in a list? My idea was to create a small prefab with a script to hold these 4 lines of information and place the game object in a que. This game object would then be deleted when a worker is found to carry this out. But i am not sure if this is the right way to do it as i am still relatively new to unity so i dont know if there is a simpler way.
Thank you for reading.
Answer by Tsaras · Apr 07, 2019 at 01:51 PM
You can use an empty GameObject with a script or a general class (without Monobehaviour) for this, depending on other details of your project. Let's assume you attach a script to a gameobject to carry out this process.
using UnityEngine;
using System.Collections.Generic;
public class LogisticsOrder
{
Location pickUpLocation;
Location deliverLocation;
float goodsAmount;
GoodsType goodsType;
}
public class OrderManager: MonoBehaviour
{
List<LogisticsOrder> orderList = new List<LogisticsOrder>();
public void AddOrder(LogisticsOrder order)
{
orderList.Add(order);
}
public LogisticsOrder GetNextOrder()
{
if (orderList.Count > 0)
{
LogisticsOrder order = orderList[0];
orderList.RemoveAt(0);
return order;
}
else
{
Debug.Log("No orders in queue.");
}
return null;
}
}
Answer by Zefurion · Apr 07, 2019 at 02:17 PM
Brilliant, exactly what i needed thank you so much! :D