Posts Tagged event

C++ Scripting: Part 22 – Full Base Type Support

Tags: , , ,

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.

Read the rest of this article »

No Comments

C++ Scripting: Part 16 – Events

Tags: , , , ,

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++.

Read the rest of this article »

No Comments

An Alternative to Events

Tags: , , ,

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!

Read the rest of this article »

24 Comments

The Problems with Events

Tags: , , ,

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!

Read the rest of this article »

No Comments

Reduce Bugs by Pruning Your Object Graph with Temporary Objects

Tags: , , ,

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!

Read the rest of this article »

4 Comments

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 »

2 Comments

Easily Track Value Changes with Observable<T>

Tags: , , ,

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!

Read the rest of this article »

11 Comments

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

Event Performance: C# vs. UnityEvent

Tags: , ,

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!

Read the rest of this article »

42 Comments

Do Events and Delegates Create Garbage?

Tags: , , , ,

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!

Read the rest of this article »

16 Comments