- Home /
Implement waiting but cannot use while() or coroutine?
I am creating a node based scripting system for my game, so when I need basic functionality I can just create it in the node-editor. I want it to work like the NodeGraph in CryENGINE, or the blueprint graph in Unreal 4. I have thought everything out, but there is one problem: I do not know how to implement a system that waits for all the input nodes to have completed their operation before the next one becomes active. Threading.Thread.Sleep freezes unity, while() freezes unity, and I cannot use coroutines because I am not using monobehaviour--I am using an abstract class that does not inherit from anything.
EDIT: Changed the name of the question, I mean't to say I tried using threads but it would make the editor freeze.
Answer by Tekksin · Sep 02, 2014 at 09:54 PM
Threading.thread.sleep sounds impossibly complicated, so I can only hope that my suggestion doesn't insult you, but why not just use a boolean?
Use a bool array and set the bool to true for each when the routine for whatever node is complete. The script will then read the node until that bool turns true. So you would have something like (JS)
if(!bool[1]){
//first node Active
bool[1] = true;
} else if(!bool[2]){
//second node Active
bool[2] = true;
} //etc
Is there a reason why you wouldn't do something like that, or have I misunderstood the question?
And if you want an artificial pause, do something like
if(bool[1]){
//first node
FakePause();
} else if(bool[2]){
//second node
FakePause2();
}
function FakePause(){
yield WaitForSeconds(1);
bool[1] = false;
}
function FakePause(){
yield WaitForSeconds(1);
bool[2] = false;
}
or you can of course do something like,
aNumber : int = 0;
if(aNumber == 0){
//first
FakePause();
} else if(aNumber == 1){
//second
FakePause();
}
function FakePause(){
yield WaitForSeconds(1);
aNumber++
}
Again, I apologize if I'm insulting you with this kind of solution, but I also may have totally read the question wrong.
"Threading.thread.sleep sounds impossibly complicated". That sounds like a very opinionated statement if you don't understand threads.
I can't use your solution Tekksin, I cannot implement yield WaitForSeconds. I am using a class that does not use $$anonymous$$onoBehavior. I also could not implement just if-else statements because it does not wait but checks ins$$anonymous$$d.
Answer by kacyesp · Sep 02, 2014 at 09:58 PM
It's a shame you're not willing to use threads because that's totally the best solution for something like this. On a high level, you would just create a "barrier" that puts all threads except the last one to sleep using a condition variable. When the last node shows up, you wake up all the threads.
A while loop could also work, but it's probably a bit slower. It shouldn't lock up unity though unless it's an infinite loop.
while loops are dangerous for that very reason. if the condition isn't met, or is ambiguous then you're looking at a lockup
I mean't to say that threading.sleep locked up the whole game. I also don't want several threads if I have more than one node. To the while loop, I implemented while(!input); and then did code after it, but it just froze.
Sounds like you just need to debug it then. For while loops that are potentially infinite ( and in your case it's more than "potentially" infinite ), I would add a "safety net". Just add a counter variable to your code that counts the number of iterations your while loop has executed. If it has iterated more times than what you'd expect in the worst case scenario, then break out of the loop. You may also want to print out some Debug statements to show why you were in the loop for so long. That would aid you in debugging why the loop is infinite.
Your answer
