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!
Posts Tagged delegate
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!
There’s no question that the
for loop is a good idea, but events are much more complex. They’re enshrined into C# by the
event keyword, but not everything about them is good. Today’s article shows some considerations you should take into account when deciding whether or not to use an event. Bonus: it includes some little extension methods to make using events and delegates easier!
By request, today’s article follows up on my Unity Function Performance article from a year and a half ago using Unity 5.0. It adds on
virtual functions to get a more complete picture of how various function calls in Unity perform. Of course it runs these tests using Unity 5.4 to see if there have been any changes in the engine. Read on for the results!
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!
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.
Today’s article takes a break from the iterator series to investigate an interesting anomaly with the
List.ForEach function: it’s surprisingly fast! So fast that it’s actually competitive with regular old
while functions. How can it be so fast when it has to call a delegate that you pass it for every single loop iteration? Read on for to find out!
In reading others’ C# code I consistently see some programmers call delegates like a function—
del()—and others use the
Invoke method of the
del.Invoke(). Is there any difference between the two? Is one better than the other? Today’s article finds out!
Unity’s garbage collector can be disastrous to our games’ framrates when it runs so we’d best not incur its wrath. We’ve seen that
foreach loops usually create garbage, so the natural followup question is “what other language features create garbage?” Events and delegates are extremely handy features of C#. They serve as the function pointers and
Function objects of the language. They replace signals and slots and allow for flexible callbacks. But a lot of what they do is behind the scenes. Are they creating garbage back there? Today’s article puts them to the test to see if creating and calling delegates and events creates any garbage. Read on to find out!