- Home /
Halt function midway until player presses 'OK' then continue
I'm constructing a tutorial function that runs through a series of events and will occasionally put an 'OK' popup on-screen that the player will confirm before the events continue.
What's a good way to pause this function's process while waiting for the player to 'OK' the popup?
One way to do this is that you can have a singleton that maintains the global state of the game. It can have an enum with a Waiting state / Pause state. All other scripts in the project can register a callback function when the state changes.
Answer by proandrius · Apr 22, 2013 at 10:28 PM
You should use Coroutines for that, for example:
private bool isOkay = false;
void Start(){
StartCoroutine(PopMessage());
}
IEnumerator PopMessage()
{
// PART 1: do something here
if(isOkay==false) yield return; // function will wait while isOkay is false
// PART 2: do something after isOkay is true
}
void OnGUI() {
// when you click the button, function PopMessage will continue to PART 2
if(GUI.Button(new Rect(0,0,100,50),"Set Okay to True")) {
isOkay = true;
}
You meant:
while(isOkay==false) yield return null;
Right?
ah yyes, or yield return 0; it does not really matter at that point.
Hmm… I'm not well versed in C#. Can anyone translate what's happening into Javascript / English? :)
Won't declaring 'return' in the while loop just exit the function entirely? (I'm assu$$anonymous$$g yield return is just the same as return in JS)
in javascript ins$$anonymous$$d of "yield return 0;" just write "yield;". And is$$anonymous$$d of void Start, void OnGUI, IEnumerator Pop$$anonymous$$essage, it just function Start, function OnGUI, function PoPmessage.
Thanks! Just tested it out and it's working nicely :)
As a tip (although maybe you were doing it for the benefit of this being a learning resource?) you can use while( !isOkay )
rather than while( isOkay == false )
for the same effect.
Your answer
Follow this Question
Related Questions
Paused Menu backdrop 1 Answer
How to pause a game with a GUI button 1 Answer
individual object menu 0 Answers
changing from mouse click to keys 0 Answers
Pause Menu - Loading & Quitting 1 Answer