Posts Tagged coroutine

C++ For C# Developers: Part 39 – Language Support Library

Tags: , , , ,

Some parts of C++ require parts of the C++ Standard Library. We’ve lightly touched on classes like std::initializer_list and std::typeinfo already, but today we’ll look at a whole lot more. We’ll see parts of the Standard Library that would typically be built into the language or are otherwise strongly tied to making use of particular language features.

Read the rest of this article »

4 Comments

C++ For C# Developers: Part 36 – Coroutines

Tags: , , ,

In today’s final article covering the C++ language, we’ll explore a new C++20 feature: coroutines. These are analogous to both C# iterator functions (i.e. those with yield) and C# async functions. There are a lot of interesting aspects of coroutines, so let’s dive in explore!

Read the rest of this article »

3 Comments

DIY Iterators and Coroutines

Tags: , , ,

Iterators aren’t magic. We’ve seen the IL2CPP output for them and it’s not complex. It turns out we can just as easily implement our own iterators and gain some nice advantages along the way. Read on to learn how!

Read the rest of this article »

No Comments

Easy Threading With Coroutines

Tags: ,

Coroutines are great for tasks that are easy to break up into little chunks, but we still need threads for long-running blocking calls. Today’s article shows how you can mix some threads into your coroutines to easily combine these two kinds of asynchronous processes.

Read the rest of this article »

8 Comments

A Simpler Finite State Machine (FSM)

Tags: , , ,

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!

Read the rest of this article »

11 Comments

Catching Exceptions in Coroutines

Tags: , ,

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!

Read the rest of this article »

13 Comments

How to Easily Use Callback Functions in Coroutines

Tags: , ,

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!

Read the rest of this article »

5 Comments

An Interruptible YieldInstruction

Tags: , , ,

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!

Read the rest of this article »

1 Comment

Updater: An Easy Way to Get Updates Without Inheriting MonoBehaviour

Tags: , , ,

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 MonoBehaviour? Enter 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!

Read the rest of this article »

1 Comment

Too Many Coroutines: A Queue Solution

Tags: , , ,

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.

Read the rest of this article »

19 Comments