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?