- Home /
How to prevent Unity to execute all the code in the first frame.
So, i'm creating a card game with Unity3D and im stuck in a logic issue: in Start() i call a function called SinglePlayer(); and the function is the core of the game.
Singleplayer() has inside it another function NextTurn(); that is called to decide who shall play first on the next turn.
When the AI starts first it does its turn instantly and this is good, but when it's my time to play a card the code doesn't wait for me to Drag and Drop a card into the table, but tries to get instantly my card from the table and fails.
There's a way to pause the code from executing? I tried with infinite loops but Unity crashes, i tried with CoRoutine but it doesn't work. There's anything else i should try?
There are many ways to pause the code as you can make bool and check in update whether you have complete your turn or not. You can make unity event and when you complete your turn he invoke event and do something with the AI. As in your case you can use timer and wait for time to elapsed then take decision.You can make callbacks function for executing code.
you can use WaitUnitil, it Suspends the coroutine execution until the supplied delegate evaluates to true. WaitUntil can only be used with a yield statement in coroutines.
I tried WaitUntil(); and It doesn't work. I'll try with writing a bit of my program, maybe it'll be more clear.
public class SinglePlayer : $$anonymous$$onoBehaviour {
private void Start()
}
GiocatoreSingolo(); //aka singleplayer in italian lang.
}
private void Update()
{
targetTime -= Time.deltaTime;
if (CountCards(table) == 2)
{
2CardsOnTable = true;
}
if (CountCards(table) == 1)
{
1CardOnTable = true;
}
}
private void GiocatoreSingolo()
{
if($$anonymous$$yTurn==true)
{
StartCoroutine( try1(1CardOnTable,targetTime));
if(1CardOnTable==true) anotherFunction();
}
else
somethingElse();
}
public IEnumerator try1(bool Cards, float timer)
{
yield return new WaitUntil(()=>Cards==true || timer ==0);
}
}
The problem is that the CoRoutine doesn't work and is skipped so AnotherFunction() is called even if the time, or the bool are not right.
You are still approaching this from the wrong perspective.
You shouldn't try to "stop" the execution of code from progressing to the next line (you can, with an infinite loop, but as you noticed, it's not what you want).
If you completely block the execution from progressing in a single threaded program, ALL of the code in the program will stop executing until you unblock, including the code implemented by Unity that reads keyboard input and redraws the screen etc. etc. That's why Unity "crashes" (the right word is "hangs" since it doesn't exit to desktop) It is "supposed" to do that.
What you want to do is "not do anything in Update()" when you are waiting. This can be as simple as
void Update() {
if (waitingForPlayer$$anonymous$$ove) return;
Coroutine can't be used to make the rest of the app pause. Yielding means exactly that the coroutine "yields" and gives the rest of the app a chance to do all it wants. You could use coroutines and yielding to delay execution of code that's inside the coroutine until a condition is met, but why do that when you can use the same condition in an IF in Update() to prevent execution of the delayed code.
As a side note, timer==0
will pretty much never be true. Let's say you start from timer=2f
... Time.deltatime is the time between screen redraws and depends on your apps performance It's something like 0.027153f for example. It's extremely unlikely that a bunch of frame times combined would amount to exactly 2f. The condition should be timer<=0
. Actually you should never trust a floating point number to be exactly something. You can google "floating point precision" to read more.
what you are asking is super simple. Use an if statement! function update continuosly reads its code every frame. and is the perfect place to check if conditions are met.
void Update () {
If(playerjustdragged==true) {
playerjustdragged=false;
//this will execute once everytime the bool becomes true
}
}
Answer by Fanttum · May 18, 2017 at 03:55 PM
void Update () {
If(doNotExcute) {
// code
}
else {
// code
}
}
Your answer
Follow this Question
Related Questions
How to trigger udpate in editor mode only when I modify component parameters 1 Answer
Game over funcition isn't being called accurately? 2 Answers
Animating GUI Texture - Help 4 Answers
FixedUpdate limits: consistant 0.01 s on mobile devices? 1 Answer
How can I change ApiCompatibilityLevel before Unity start? 0 Answers