The Magic of LINQ Expressions

Tags: , , ,

LINQ expressions aren’t the same thing as LINQ queries. They’re more like reflection for the syntax of C# itself. It’s a fascinating—and powerful—area of the language and I’ll be exploring it a little in today’s article.

Read the rest of this article »

No Comments

Efficiently Keeping Lists Sorted

Tags: , , , , ,

List<T> (and SafeList) have a great feature for fast lookups: BinarySearch. However, the list needs to be sorted in order to use it. You could call Sort() first, but that would give back all the performance you got with BinarySearch. It’s better to just keep the list sorted all the time. Unfortunately, there is no function on IList<T>, List<T>, or SafeList to efficiently insert an item into a list that’s already sorted. Today’s article presents an extension function that adds this functionality on to IList<T> and even the non-generic IList so your list will always be sorted for quick lookups with BinarySearch. Read on for the code, unit tests, and a performance test showing the advantages you stand to gain.

Read the rest of this article »

2 Comments

SafeList 2.0

Tags: , , , ,

The first version of SafeList tried to address a common problem: inserting and removing elements into a List<T> while you loop over it. It had a lot of problems though and ended up being pretty much useless. Today’s article presents SafeList 2.0, a radically-improved version that really solves the problem so you can actually use it as a drop-in replacement for List<T>. Read on for the details, the source code, and even the unit tests that prove it handles all the nasty corner cases for you!

Read the rest of this article »

No Comments

Delegates As Function Pointers: Performance Boost?

Tags: , , ,

C# delegates can be used like function pointers. Assign it once and you don’t have to use an if over and over. But is the overhead of the delegate worth it? Today’s article puts it to the test to see if this a valid performance boost versus just using an if over and over. Read on to see if a delegate is worth your time.

Read the rest of this article »

4 Comments

For vs. Foreach

Tags: , , , , ,

foreach loops are really convenient, but are for loops faster? It’s a simple question, but one that has really wide implications in almost any codebase. Today’s article tests them out to see which is faster for looping over arrays and Lists. Read on to see which is quicker!

Read the rest of this article »

9 Comments

How Closures Work

Tags: , , ,

C#’s support for closures includes lambdas ((a, b) => a+b) and delegates (delegate(int a, int b){return a+b;}). They’re extremely handy tools that many developers use on a daily basis. For example, it’s really convenient when using List.Find to pass in a lambda like item => item.Id == IdToFind. They also make great callbacks for asynchronous operations. However you’re using them, understanding how they work behind the scenes will help you understand how they behave and give you insight when optimizing your code. Today’s article delves into the topic to gain just this understanding, so read on to learn some more about closures!

Read the rest of this article »

No Comments

DateTime Performance

Tags: , ,

Unity’s Time class is an easy way to get the relative time. You can find the time since the app started with Time.time or the time between frames with Time.deltaTime. But what if you want to know the absolute time? You may need to display a clock to the user, send a timestamp over a network, or record when a game was saved. This is where System.DateTime comes in. It’s powerful and offers so much functionality that it’s natural to worry about about how slow it’ll be. So today’s article puts it to the test to find out how much time is being spent in operations like DateTime.Now which gets the current date and time. Is it quick enough that you shouldn’t worry? Read on to find out.

Read the rest of this article »

No Comments

A Finite State Machine (FSM) for Unity

Tags: ,

Today’s article introduces a small but capable and extensible finite state machine (FSM) that you can use to organize your Unity applications’ code. FSMs are commonly used to represent the different modes the app can be in—main menu, world map, gameplay, game over—and how the app transitions from one state to another. The FSM in today’s article follows the pure code methodology by not tying the code to game objects, MonoBehaviours, or scenes. So read on to learn about and make use of this “pure code” FSM for Unity!

Read the rest of this article »

4 Comments

Getting the Result of an Iterator

Tags: , , ,

Iterators (functions that yield) are great for representing asynchronous processes such as web calls, updating state over time, and multi-step operations. One unfortunate downside is that it’s tough to return a result from them. Today’s article explores several workarounds to this problem before ultimately arriving at a clean, easy-to-use solution to the problem. Read on to learn how to make iterators more useful!

Read the rest of this article »

No Comments

A Model-View-Controller (MVC) Pattern for Unity

Tags: , , , , ,

What do you do if you want to use the Model-View-Controller (MVC) design pattern in your Unity app but you don’t want to use a framework like StrangeIoC? With a little thinking about the problem I think I’ve come up with a simple yet effective pattern to follow that doesn’t require you to use any framework. In today’s article I’ll talk about each part, how the parts fit together, and how you can use MVC to cleanly organize your “pure code” app. Whether you’re an MVC newbie or just want to see a new take on MVC in Unity, you’re sure to learn something today!

Read the rest of this article »

4 Comments