- Home /
Move (and other actions) multiple characters 1 at a time?
Okay, so, I cannot quite figure this out. The concept is, I want to have multiple characters, give one orders, it will then execute those orders and the camera will follow it, and then the game will move on to the next character. (If you've ever played any turn-based tactical rpg such as Fire Emblem or Final Fantasy Tactics or something - basically, like that).
For example, let's say I have some AI player that has a reference to all its units, then I'd want to do something like
public Unit[] myUnits;
void GiveOrders(){
foreach(Unit u in myUnits){
//Do something to determine what unit should do,specifics dont really matter here
//Order is some kind of class that contains an order for a unit that it can execute
Order order = new Order(walkHere, attackTarget);
u.ExecuteOrder(order);
//Now I want to wait until this unit is done with its
//orders before I move on to the next one... somehow
}
}
and in Unit I suppose there would be something like
bool ordersCompleted;
NavMeshAgent agent;
public void ExecuteOrder(Order order){
//walk first then attack. Should probably put something in Order that tells
//if it should walk then attack or attack then walk, but again not
//really relevant here
agent.SetDestination(order.walkTo);
//Again here should wait until action is completed
Attack(order.attackTarget);
//and again wait until action is completed
//then do something that tells the AI that all orders are executed
//and this unit is done
ordersCompleted = true;
}
void Attack(Unit enemy){
//Some kind of attack method would be here
}
I'm fairly sure I should do something with coroutines here, but I am not getting them right :(
So in other words what I want to do is 'begin action A -> action A calls action B -> put action A on hold, do action B -> B is completed -> continue with A'.
Answer by siaran · Apr 03, 2015 at 04:57 PM
Ok, I figured this one out, turns out coroutines were not the way to go... (at least, my solution doesn't use any)
it sort of goes like this:
in AI class:
Unit[] myUnits;
int currentlyExecturing;
//call this when this AI player's turn starts
public void StartTurn(){
//start at unit 0
currentlyExecuting = 0;
GiveOrder();
}
void Update(){
//check if current unit orders are completed each frame
if(myUnits[currentlyExecuting].ordersCompleted){
//if so, give orders to the next unit or end turn if it was the last unit
if(currentlyExecuting < myUnits.Length-1){
currentlyExecuting++;
GiveOrder();
}else{
EndTurn();
}
}
}
//Give order fucntion
void GiveOrder(){
Order o = new Order();
//should do something to determine what the order
//actually is
myUnits[currentlyExecuting].ExectureOrder(0);
}
And in unit:
bool ordersCompleted;
void ExecuteOrder(Order orders){
ordersCompleted = false;
agent.SetDestination(orders.destination);
walking = true;
}
void Update(){
if(walking) Walk()
}
void Walk(){
if(agent.remainingDistance <= agent.stoppingDistance){
walking = false;
ordersCompleted = true;
}
}
Haven;t incorporated any attack action yet so these agents just do walking, but they do it sequentially which is what I wanted. So I'll mark this question as answered, but if anyone has a better way of doing this, I'd love to hear it.
Your answer
Follow this Question
Related Questions
Hold or Wait while Coroutine finishes 6 Answers
WaitUntil and WaitWhile don't work 1 Answer
Sequentially wait for multiple coroutines to finish 0 Answers
WaitForSeconds Question 2 Answers