In C#, just about everything is an
IEnumerable. Since LINQ syntax,
foreach loops, and the
System.Linq namespace are all designed to work with
IEnumerable, you’ve got lots of tools to use. Unfortunately, the core of
IEnumerable is the
GetEnumerator function which usually creates garbage and eventually causes memory fragmentation and GC framerate spikes. Do we simply stop using all of these nice tools? Normally the answer is “yes”, but today’s article shows you another way.
In C#, just about everything is an
Last time we saw that calling a non-default constructor on a generic struct (
MyStruct<T>) causes garbage creation. That garbage creation is subtle, but can have big impacts on framerate and memory usage. Today we’ll see two more ways that structs can create garbage and hopefully avoid some pitfalls. Read on to find out how!
As Unity programmers, the garbage collector is sadly our enemy. C#
structs are often a great tool to avoid allocating objects that need to later be garbage-collected. This isn’t always the case though. Sometimes even a
struct can allocate garbage. Today’s article points out one of those ways so you won’t be fooled into thinking you’ve stopped the GC just because you’re using a
struct. Read on to learn more!
When writing code for a library, there is invariably some of it you want to hide from the users of the library. You want to keep the public API clean, but Unity makes this tough. Today’s article discusses a strategy for laying out your code so that users of the library aren’t burdened by classes, functions, and properties that they don’t need to know about. Read on to see how!
Today’s article is not about the
const keyword that C# already has. It’s about the
const keyword that C++ has and how we can approximate it in C# to make our code safer. It’s a really powerful tool that’s often the default for C++ programmers, but we can take advantage of a similar strategy in C#. Read on to learn how!
Every programmer has heard that global variables are bad practice and should be avoided in favor of other techniques. Yet you’d be surprised how often global and pseudo-global variables are used. Today’s article reveals some of these usages and present some alternative ways to structure your code so it’s easier to read, write, and maintain. Read on to learn how!
Last week I presented a problem: how do you iterate over multiple lists of multiple types in the order of some common field? For example, how would you iterate over a list of
Player and a list of
Enemy by both of their
Health fields? In that article I showed two solutions to iterate over two lists in this way. What I didn’t show were any solutions to handle more than two lists. What if you needed to also iterate over a list of
NPC? Today’s article discusses how to tackle this problem and ends up with a handy utility class that you can use for your own types no matter how many lists you have. Read on to see how!
Suppose you have two sorted lists, one holding
A elements and another holding
B. Both types have a common field and you want to iterate over both lists according to that common field. For example, if you have
[1, 3, 5] and
[2, 4, 6] then you want to get the
1 then the
2 then the
3 and so on. How do you do this at all? How do you do it efficiently? Read on for my answers to the puzzle!
Coroutines are a fundamental building block of Unity scripting. In 5.3, we got a new class to make them more powerful:
CustomYieldInstruction. Today we’ll look at it and see if we can make an arbitrarily-interruptible
YieldInstruction so our coroutines can abort the things they
yield. Read on to see how and to compare against the old 5.2 way!