Tag Archives: c#

Local dev discovers one neat class to inject value types.

Programmers hate him!

But seriously, for most inversion of control containers, injecting a value type is a bit of a pain. You’ll get it, sure, but you can’t change it and have those changes mirrored back to where it came from. It is a value type, after all.

Well, no longer! Just use this class to wrap up any value type in a reference type you can take with you and alter to your heart’s content!

The most important extension method you’ll never write.

As game developers, we frequently need to randomly generate things, whether it’s picking a power up to spawn, or choosing between enemies. Regardless, of the use, one of the most common forms of random generation in a game is picking an object from a list of potential candidates at random. This happens so often that I wrote an extension method to do it for me, so if, like me, you’re a C# game developer, save yourself some time and headache and use this:

This will then pick a random item from any collection and return it (or null / the default value for a type if there are no items).

Usage is like so:

Best part is this will work on anything that implements IEnumerable , which is pretty much any C# collection, array, LINQ result, whatever.

Fire C# events safely with these handy extension methods

You may or may not know already that the following code is bad if you’re using threads (and you should use threads).

The problem is that something could subscribe or unsubscribe to your event during your event raise! There’s an easy solution though, just make a local copy like so:

That’s not really a code snippet you want to throw around all the time, so what else can you do? You can’t make it part of a base class, because events can only be raised by the class that owns them, and besides, everything inheriting from your EventsBase class would be weird and unintuitive. You can’t make it a static method in some helper class either, for the same reasons. Instead, it’s extension methods to the rescue!

We’ll start with normal instance events, which are more common than their static brothers (and which need a slightly different treatment).

Note that because we’re dealing with static events, we don’t get a sender object, so per guidelines we send null.

And the usage

And now for the statics, a shorthand:

Note that because we’re dealing with static events, we don’t get a sender object, so per guidelines we send null.

And the usage is as simple as:

Simple, easy to use, portable and thread-safe. All you have to do is put it in a static class, and reference that class’ namespace in your project.

Ultra useful Unity snippet for developers who use interfaces

Unity can’t serialize interfaces. But sometimes you want to have access to any component, as long as it implements an interface. They are incredibly useful in game development, particularly if you’re coding for reuse. So what can you do? There’s a fairly lengthy workaround. You create a serialized Unity property for a GameObject. Then you create a second property (this time not serialized) for your interface. You use the interface property’s getter to get a component from the GameObject property that implements your desired interface.

I found myself writing this fairly lengthy code so often, I’ve turned it into my own Visual Studio snippet. Since I use it so much, maybe you will too. The code you’ll need is as follows:

Obviously swap privateGameObjectName, PublicGameObjectName, privateInterfaceName and PublicInterfaceName property names with ones suitable to your situation, and swap InterfaceType to the interface you want to use. After that, usage is as simple as  PublicInterfaceName.DoWhateverInterfaceFunctionYouWant()