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!
As you know, making your game data-driven is good idea. So you make all kinds of configuration files with numbers in them: starting health, damage amount, XP reward, etc. But what do you do when those values aren’t just constants? Today’s article presents a little one-file class that you can use to evaluate simple formulas instead of just constants. What if your data for “XP reward” wasn’t just “100” but instead a formula like “10+MMRDifference*50”? That’s a great tool you can hand to game designers to data-drive your game. Read on to learn how!
One of the advantages we get when we use unmanaged memory is a huge increase in flexibility. For example, we can easily allocate a whole array of objects at once instead of one-at-a-time when we
class instance. We can also create a memory pool with one allocation then divide it up ourselves. It turns out that can really speed up memory allocation and, at the same time, actually reduce memory fragmentation on top of the fragmentation we’re avoiding by not creating any garbage for the GC. Today’s article shows you how memory pools work and provides an implementation of one you can use in your own projects!
Two facts are at odds in Unity programming. First, delegates like
EventHandler are extremely common with or without events. Second, the garbage collector is a huge source of CPU spikes and memory fragmentation in our games. Why are these facts at odds? Because code that uses delegates is almost always written in a way that creates garbage. It’s an extremely easy trap to fall into, but this article will show you how to get out of it!
C# has built-in events and they work fine in Unity projects. End of story, right? Not so fast! Have you ever wondered why the Unity API doesn’t have any C# events in it? Or why Unity made their own
UnityEvent class for UGUI? Maybe there are some valid reasons to avoid C#’s events. Today’s article discusses an alternative with some serious upsides. Read on to learn more!
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.
Unity’s garbage collector is super slow and the bane of our programming life. It’s the reason we can’t use
foreach, have to make pools of objects, and go to great lengths to avoid boxing. It’s also seemingly mandatory, but that’s not quite true. Today’s article shows you a way that you can skip the GC and still allocate memory!
In previous articles I’ve compared the performance of various JSON libraries. Unity’s built-in
JsonUtility usually comes out on top, but that conclusion loses sight of the bigger picture.
JsonUtility is only really fast when you compare it to other JSON libraries. Compared to non-JSON alternatives, it’s ludicrously slow and oversized. Today’s article compares JSON to an alternative format to highlight just how bloated JSON is.
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!