- Home /
Block a function over several frames
I want to achieve the behaviour observed by Unitys EditorUtility Functions OpenFilePanel, SaveFilePanel and similar which blocks the function from which it is called, while remaining the main thread active and running.
Basically, you call a function from within yours, something happens (like a window popping up), the user interacts with it over several frames, an after he finished that, your function continues. But as he interacts with the windows, the main thread continues processing everything else.
Any idea how they've done that?
EDIT: I guess not THEY have done that, rather then Microsoft themselves: The SaveFilePanel for Example, is probably just a wrapper for SaveFileDialog. Every EditorUtilitys function thus probably uses a child of CommonDialogue, yielded when called ShowDialogue.
Can any UT Guy confirm that? If it's so, I'll probably have to find another way of doing that "Block", f.E. with delegates/actions passed to the function of code that should be executed after the operation finishes.
Answer by siaran · May 11, 2015 at 05:33 PM
with a coroutine?
something like
bool notDone;
IEnumerator Foo(){
notDone = true;
//"stops" this function until notDone is set to false
while(notDone){
yield return null;
}
//code to execute once notDone is false here
}
how you set notDone to false after you start this function is up to you.
No Coroutines are not the way to go, I was looking into that before extensively enough;) The problem is, they're not blocking. They perform an action over many frames, but do not wait to finish before they return.
You say
"Basically, you call a function from within yours, something happens (like a window popping up), the user interacts with it over several frames, an after he finished that, your function continues. But as he interacts with the windows, the main thread continues processing everything else"
Wont the "main thread" be the update function, then you function would be function controlled by a while() ?
Then from there call the other function (like a window popping up) again controlled by a while( after he finished that) it returns to your function ?
But as he interacts with the windows, the main thread (function Update()) continues processing everything else ?
But a while loop on the main thread will block the whole main thread until it's finished, doesn't it? All involved functions (calling and blocking function, all other functions like Update) run on the main thread, so a while loop would block them all, I guess. Hm, If I think a moment about this, the only solution I could think of is somehow switching the thread for both the calling and switching function, but that's not possible I suppose...
This question searched for sth similar. No answer there though...