Hi all I am trying to get this sorted out… I think I need a function queue system.
For instance my player asks to move forward, I would like to pass the function into a list (queue).
say the function is move(var x, var z) Can I pass this into a list to be called? How can I make is so I can pass multiple functions into this list with varying parameters?
I think you’ll have to build some sort of structure to hold the information, then have a queue of those structures.
Something like a gameobject which has a var for the object being affected, a string for the script to call (to plug into the sendmessage call) and something for the params to be passed along. The params bit might be tricky given that each action will need different things (positions, other gameobjects etc.).
so move to a position and shoot a target looks something like:
array[0] is {“playerobject”, “move”, “{position(x, y)}”)
array[1] is {“playerobject”, “shoot”, “{target}”}
then you’ll need a parser script that can read the queue and read the holding structures into a sendmessage call on the right object, for each step by step.
Not sure it’ll work but that’s how I’d be looking at solving it from a fairly high level. Interesting problem at least =)
Create a manager class derived from Queue that will process IWorkQueue items
public class WorkQueueManager : Queue
{
public void ProcessThisFramesWork()
{
while (Count > 0)
{
IWorkQueue QueueItem = Dequeue();
QueueItem.DoWork();
}
}
}
Then, create as many different classes as you need, one for each different type of work. Each class will need to store the properties for the work it needs to do, and be able to “do” the work. Implement the interface IWorkQueue, and have the DoWork function on each class read the parameters from the class instance to do the work.
Example:
public class MoveForward : IWorkQueue
{
float _x;
float _y;
GameObject _obj;
public MoveForward(GameObject o, float fx, float fy)
{
_x = fx;
_y = fy;
_obj = o;
}
#region IWorkQueue Members
public void DoWork()
{
// Move _obj by _x and _y here
}
#endregion
}
You will have to instantiate a WorkQueueManager somewhere. Probably on a singleton. Up to you. (Or you could make the WorkQueueManager a singleton, but that would of course limit you to one instance, and you may not want that - for example, if you have different threads processing different queues).
Assuming you have created a WorkQueueManager named MyManager, the code to enqueue an item of work would look like this:
EDIT: I managed to do what I asked below so that I can pass any function into a list and have it called with parameters
Thanks for the help, one last thing.
Is there any way I can pass a function call e.g… move(10, 5) into a list for calling later? The function call might be talk(“hello”) or move(0, 10) or anything really. I do not need to do anything to the function and it does not need to return anything but only needs to be called.