Quantcast
Channel: Answers by "Duckocide"
Viewing all articles
Browse latest Browse all 25

Answer by Duckocide

$
0
0
@Thelulz Look at Coroutines. They are a great way of running things in parallel to the main unity lifecycle. So, in your Monobehaviour component add the following to the "OnEnabled" method, something like (all this code is untested and typed in to the forum straight!)... StartCoroutine(MyTurnEngineCR()); Then add a private IEnumerator method with something like... public bool turnComplete = false; public int turnCount = 0; private bool _turnEngineActive = true; private IEnumerator MyTurnEngineCR() { while(_turnEngineActive) { yield return new WaitForEndOfFrame(); turnCount += 1; Debug,Log($"Turn {turnCount} started..."); // Has the turn completed? while (!turnComplete) { yield return new WaitForEndOfFrame(); } turnComplete = false; Debug,Log($"Turn {turnCount} Completed!"); } This will sit there waiting every frame to see if the boolean "turnOver" has been set. If it has, it will clear that flag and increment the turnCount. The yield lines are really important as they hand processing back to the main unity thread. Generally (other than what you do in them), Coroutines have little performance cost (I often use them for enemy AI's). The private _turnEngineActive bool is simply to keep the coroutine looping (if you set it false, it will stop once a turn is over). Hope this helps. Coroutines are important for anything that can happen outside of the Update() etc unity cycle loop. Recommend you read up on them. Once you get used to them, they are so so useful for all sorts of things like AI, monitoring and decision making in a game.

Viewing all articles
Browse latest Browse all 25

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>