- Home /
Chess Promotion
I'm having trouble implementing chess promotion. When a pawn advances to the final row it can be promoted. My program has a panel pop up with buttons for the user to select the pawn's promotion. The Queen button calls this function
public void ChoseQueen() {
Debug.Log("Chose Q");
Chess.Promote('Q');
}
Chess.Promote does this
char promoteSelection = ' ';
public void Promote(char promoteToThis) {
promoteSelection = promoteToThis;
}
I need to wait once a pawn arrives at the last row for the user to press a button. I need something like the following pseudocode:
while (promoteSelection == ' ' ) {
Debug.Log("Waiting for button press");
sleep(1000);
}
Debug.Log("Pawn promotion button pressed!");
How to wait for a button press ? What would be the best way to do pawn promotion?
Answer by Nighfox · Feb 03, 2018 at 06:04 AM
Just use a Coroutine
and yield by WaitWhile
:
IEnumerator PromotionWait()
{
yield return WaitWhile(() => promoteSelection == ' ');
//promotion had been selected, add your code here
}
This will wait and loop while promoteSelection
is empty, and will only proceed to the next line if it now contains something. Also note that when calling this in Update
or other Unity event functions that are called multiple times, you may need to add a "lock" to execute this only once.