- Home /
'pausing' a game to wait for user to select object before proceeding
I'm creating a card game, and I have run into an issue of 'pausing' my code while I wait for a player to make a decision in-game. As a background, I create a list of all the plays for the turn, and they are stored (and sent to opponent) with six pieces of information -- who's playing it, the card itself (so I know what the card does), the location of the play (hand/field/etc), the position in that location (card #1 in hand, etc), as well as where it's being played and that position. This is all saved in a list, and I evaluate the list when until it's empty.
So for example: (true, card1, Hand, 1, Field, 1) (true, card2, Hand, 2, Field, 2) (false, card1, Hand, 1, Field, 3)
Would play the first card in my hand to first field position, then second card in my hand to field position, and finally the first card in an opponent's hand to position #3.
This works great if I know all plays ahead of time -- I just have a laundry list of what to do and I evaluate through it, no problem.
BUT I'd like to add in the ability to have responsive effects -- say for example, I have a card that when it dies I can deal 3 damage to target unit. I'd need to 'interrupt' the flow on both my client and the opponent's client when we reach this point until the controlling player makes his decision on who'd he'd target, send it to the opponent, and then we can both proceed. As sample code, here's what I currently do
function EvalStack() : IEnumerator {
//step through the order
while (stack.length > 0) {
var toplay : GameObject = stack.Shift();
yield StartCoroutine(sAbilities.EvalCard(toplay));
yield WaitForSeconds(0.75);
}
}
This will play out every card in the stack array (it has a script component that stores all relevant information) one at a time. EvalCard snippet looks like this:
function EvalCard(card : GameObject, isplayer : boolean) : IEnumerator {
CV = card.GetComponent(cardvars);
if (CV.ID == 1) {
//If you have less than 4 resources, draw a card.
if (isplayer) {
if (Pvars.Rtotal < 4) {
sDeck.DrawCard();
}
}
else {
if (Pvars.oRtotal < 4) {
sDeck.oDrawCard();
}
}
}
}
What I would like to do is something like this:
var choice : int;
var opponent_choice : int;
var choicemade : boolean;
var opponent_choicemade : boolean;
function EvalCard(card : GameObject, isplayer : boolean) : IEnumerator {
CV = card.GetComponent(cardvars);
if (CV.ID == 1) {
//When you play, choose a unit and deal 3 damage to it.
if (isplayer) {
yield StartCoroutine(WaitForChoice());
yield StartCoroutine(DealDamage(choice));
}
else {
yield StartCoroutine(WaitForOpponentChoice());
yield StartCoroutine(DealDamage(choice));
}
}
}
function WaitForChoice() : IEnumerator {
choicemade = false;
while (!choicemade) {
var hit : RaycastHit;
var itsaray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (itsaray, hit) && Input.GetMouseButtonDown(0)) {
choice = hit.collider.gameObject.GetComponent(cardvars).slot;
choicemade = true;
}
}
}
But this will freeze unity due to the while loop without 'foreseeable' end. So, is there any way to feasibly make that function work?
You should concentrate on what you want to achieve and not on your attempts which doesn't work ;)
A Corouine can't return anything. That's mainly because a coroutine isn't a function, it's an object. The coroutine-function acts like a constructor for the coroutine. It always returns an IEnumerator.
As i said, it would be more important to know what exactly you wanna do. You want to evaluate card and pause a coroutine, but what the coroutine does or what you actually want to evaluate isn't clear at all.
Ask specific and detailed questions ;)
Feel free to edit your question at any time.
I'll post here before I edit -- you are totally right I can't return from a coroutine, that was a bad error on my part. I'm going to put up a hypothetical scenario and code, and where my issue arises. Let me know if it's unclear still.
Edit: done
Answer by TheDavil86 · Aug 11, 2012 at 03:23 PM
Put 'yield;' in the end of your while loops, this allows other processes to continue running without waiting for the while loops to finish.
haha i should have looked at the comments before I updated! this works perfectly -- so simple!