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!
One of C#’s most unique features is its SQL-style LINQ syntax. It’s a powerful and expressive way to treat data structures like a database and perform all kinds of actions on them. LINQ can also be used without the SQL-style syntax via various extension methods of
IEnumerable<T> defined in the
System.Linq namespace. Due to C#’s extension method feature, we’re free to add on our own LINQ-style functions to extend its power. Today’s article introduces some extension methods to do just that. Read on for the source code and power up your LINQ!
Sometimes a tiny amount of code costs a huge amount of performance. This is especially true of built-in language features, which many programmers assume to be extremely cheap if not free. Today we’ll look at
if and see just how much performance it can cost your app. Read on to see!
While little utilized, C#’s
struct type can come in really handy sometimes. Today’s article shows how to use it to get a lot more mileage out of modern CPUs’ caches to really boost your app’s performance. Read on for some quick tips!
As we know, foreach loops create garbage when used with a
List<T>. This happens the first time you iterate over one and it happens every time thereafter. A comment on that article shared a link to a class called
FastList that was written expressly to solve the GC issue. Does it? How does its performance compare to plain old
List? Today’s article puts it to the test to find out!
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
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!
One typical piece of advice for dealing with the slowness of Unity’s garbage collector is to periodically force a garbage collection, such as every 30 frames. Even Unity advises this. The idea is that you’ll spread out the garbage collection work across many frames rather than having a big spike that causes your frame rate to stutter. But the question remains- what’s the best rate to force the GC? Today’s article tries out various intervals to see which is best. Read on for the results!