In my last article about Finite State Machines (FSM) for Unity, I showed a “pure code” way to create a state machine, states, and transitions between those states. It worked, but I wanted to create a simpler system. I’ll show you it today!
Posts Tagged coroutine
Unity code frequently makes use of the coroutine feature of
MonoBehaviour. It can make asynchronous code a lot easier to write, but runs into problems when exceptions are thrown. There’s no avoiding exceptions since they’re built into C# (e.g.
NullReferenceException) but we can cope with them, even when they’re in coroutines. Today’s article introduces a helper function or two that you can drop into your projects to help you handle exceptions thrown from your coroutines. Read on to learn how!
In asynchronous programming we’re constantly dealing with callback functions. Maybe you have to call some function in a third party library that takes a callback function. Regardless, Unity programmers often want to use coroutines for their asynchronous tasks. Today’s article show you how you can use callback-based code from your coroutines, all while being simple and easy to use. Read on to learn how!
Coroutines are a fundamental building block of Unity scripting. In 5.3, we got a new class to make them more powerful:
CustomYieldInstruction. Today we’ll look at it and see if we can make an arbitrarily-interruptible
YieldInstruction so our coroutines can abort the things they
yield. Read on to see how and to compare against the old 5.2 way!
At first glance an
Updater class seems unnecessary in Unity. All you have to do is inherit from
MonoBehaviour and add an
Update function. But what if you don’t want to inherit from
Updater, an easy way to still get an update event and cut your dependency on
MonoBehaviour. This works great for code in DLLs, “pure code” projects, or just projects that don’t want to put everything into
MonoBehaviours. Read on for the source code and how to use it!
Unity’s coroutine support is great. So great that it’s easy to go overboard and end up with too many of them. That could be for any number of reasons. Perhaps the coroutines are using too much memory or have too many files open at once. In any case, you’ll need to find a way to limit how many are running at a single time. Today’s article introduces a solution to the problem that queues coroutines so you never have too many running. Read on to learn about the solution and for the class that implements it.
Iterators (functions that
yield) are great for representing asynchronous processes such as web calls, updating state over time, and multi-step operations. One unfortunate downside is that it’s tough to return a result from them. Today’s article explores several workarounds to this problem before ultimately arriving at a clean, easy-to-use solution to the problem. Read on to learn how to make iterators more useful!
As you use iterator functions (and
yield) more and more, you’ll start to run into some limitations in the C# language. For instance, you can’t
yield inside a
try block that has a
catch block. And the
foreach loop doesn’t provide a very good way to catch exceptions when looping over an iterator function, either. Today’s article goes into detail to find solutions to these issues and make iterator functions usable in even the trickiest scenarios!
Unity’s coroutine support allows you to easily create pseudo-threads and write synchronous-looking code that doesn’t block the rest of the app. They can be very handy for a variety of tasks. Before using them, we should understand the performance cost. Today’s article takes a look at the cost of starting a coroutine as well as the cost of running it. Just how expensive are they? Read on to find out!