Posts Tagged callback

Closures Without the GC

Tags: , , ,

Closures allow you to save the local variables of a function and access them later in a callback. Think of how lambdas can access the local variables of the function they’re declared in, even though the lambda itself is another function. Unfortunately, creating a lambda like this creates garbage for the GC to collect and you have no control over that process. Today’s article introduces an alternative that allows you to take control over the GC and still use nice, type-safe closures. Read on to learn how!

Read the rest of this article »

No 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 »


Iterators vs. Callbacks: Performance and Garbage

Tags: , , , , , , ,

Iterator functions and their ability to yield return values then continue on really come in handy for a variety of situations. Unfortunately, they come with some pretty serious performance and garbage creation drawbacks. So today’s article explores alternatives in various forms of callbacks: delegates, interfaces, and classes. Can they perform better than iterator functions? Can they avoid garbage creation? Read on to find out!

Read the rest of this article »

1 Comment

The Many Types and Dangers of Callbacks

Tags: , , , , ,

Callbacks are a mainstay of the real-time games and apps we build in Unity. We’re constantly writing asynchronous code for every operation from walking a character to a destination to making a web call. It’s really convenient for these functions to “call back” and report their status so we know how the operation is going. Unfortunately there are also a lot of dangers that come along with this. Today we’ll look into the surprisingly large number of ways you can “call back” in C# and some of the ways you can get burned doing so.

Read the rest of this article »


Cancelable Function

Tags: , , ,

I recently had the need to cancel a callback function that I had passed to an API. The API had taken my callback function directly, so there was no way to remove the event listener. So I thought back to an old article I wrote and came up with a solution. Read on for a utility function that will allow you to cancel function callback in AS3 as well as JavaScript and AS2.

Read the rest of this article »