Awkward Objects

Tags: , ,

We create objects out of structs and classes all the time, but oftentimes these evolve to the point where using them is really awkward. Today we’ll learn to recognize the telltale signs of an overextended object design and how to easily fix it.

Read the rest of this article »

1 Comment

Collections Without the Boxing

Tags: , , , , ,

In last week’s tips for using collections like List<T>, we saw how struct types are sometimes boxed resulting in GC allocations. This week we’ll see how to avoid boxing and learn some of the clever tricks that .NET collection types use to make this possible.

Read the rest of this article »

2 Comments

5 Common Misuses of Collections

Tags: , , , , , , ,

Collection types like List<T> and Dictionary<TKey, TValue> are fundamental tools in C#. Sadly, I keep seeing the same misuses of them in codebase after codebase. Today we’ll look at the top 5 problems and learn how to easily avoid them!

Read the rest of this article »

12 Comments

What are Your Latency Requirements?

Tags: , ,

Unity’s Mike Acton gave a talk this week at GDC titled Everyone watching this is fired. One point he made was regarding the importance of knowing the latency requirements of our code. When does the result need to be ready? Today we’ll talk about the ramifications of answering that question with anything other than “not immediately” and see how that can lead to better code.

Read the rest of this article »

2 Comments

Fast Structs

Tags: , ,

Structs are great for controlling memory layout and avoiding the GC, but we can go a lot further to get even more speed out of them. Today we’ll look at a simple tweak that can dramatically speed up the code using the structs without even changing it!

Read the rest of this article »

7 Comments

BitArray32 and BitArray64

Tags: , , , ,

C# already has two bit array types, but both are lacking. BitArray is a class so it requires heap allocation and GC. BitVector32 is a struct, but it’s usage is bizzare, it’s implemented inefficiently, it’s not enumerable, and there’s no 64-bit version. Today we’ll create a new, simple type to remedy these issues and add a new tool to our toolbox!

Read the rest of this article »

4 Comments

A Journey Through a P/Invoke Call

Tags: , ,

Today we’ll go through everything that happens when you make a P/Invoke call. We’ll see how native libraries are loaded and how marshaling works. We’ll touch on calling conventions and character sets. In the end, we’ll have a better understanding of what goes on when we call into native code.

Read the rest of this article »

3 Comments

How to Wrap a Real Native Library

Tags: , , ,

There are many posts that’ll tell you the “hello world” of calling native code like C++ from a C# Unity project. These tend to be simple examples though, so it’s hard to go from that to wrapping a real life useful native library. Today we’ll wrap SQLite, a popular C library that implements a database, and talk about the challenges in doing so and how to end up with a pleasant C# API for the rest of the game to use. Read on to learn how!

Read the rest of this article »

4 Comments

Tutorial: Using F# with Unity3D 2018.3

Tags: , , ,

Today’s tutorial gives step-by-step instructions on how to use F# as a programming language in Unity. It updates an older tutorial from 2015 that used Unity 5.2 because a lot has changed in Unity since then. With an improved IL2CPP and support for .NET Standard 2.0, it’s easier than ever to simply drop in F# support. Read on to learn how!

Read the rest of this article »

2 Comments

SmallBuffer: A Stack-Based Array

Tags: , , ,

Sometimes you just want a small array without the heap allocations and GC. Existing solutions like stackalloc require unsafe code, don’t allow for dynamic growth, and don’t support foreach loops. So today we’ll design and build a code generator that puts a new tool in your toolbox!

Read the rest of this article »

8 Comments