Today we’ll complete our ability to use C++ classes to derive from C# classes and implement C# interfaces. So far we’ve been able to override methods, properties, and indexers. Today we’ll add the ability to override events and derive from classes that don’t have a default constructor.
Those are the last two pieces of the puzzle that will allow us to derive from any C# base type with a C++ class. Read on for all the details about how this works.
Posts Tagged event
Last week’s article covered delegates, so it’s only natural that we follow up this week by covering events. Supporting delegates has laid a good foundation for supporting events, so let’s dive in and see how to implement and use them in C++.
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!
One of the biggest source of bugs in our apps is state: all of that persistent data we keep around in memory. When things change we need to make sure to update all of it at the right times and with the right new parts of the state that changed. Inevitably things get out of sync and our app is in “a bad state”. Today’s article discusses some ways we can prune the “graph” of objects that we create in OOP so that there’s less state to maintain. Read on for some interesting techniques that could help you prevent bugs!
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.
A lot of times we want to take some action when a value changes. If the player’s level increases I want to play a “ding” sound effect. If a player loses health points I want the screen to flash red. Today we introduce Observable<T>
: an easy way to track these value changes. Read on for the source code and examples!
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 MonoBehaviour
s. Read on for the source code and how to use it!
Unity programmers have their choice of two kinds of events. We could use the built-in C# event
keyword or Unity’s UnityEvent
classes. Which is faster? Which one creates more garbage? 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!