Archive for Tips and Tricks

Optimizing For Loops

I am pretty obsessed with getting every little bit of performance I can out of my code, especially when developing for mobile platforms. For loops, believe it or not, can be optimized using some very simple tricks. Normally you probably write a for loop like this:

for (int i = 0; i < some.Length; i++) {
    // Code
}

If you know that the length of whatever you're iterating is going to be less than 256 (0-255) you can use byte instead of int, if you aren't completely certain, you can probably get away with ushort (0-65,535). This will free up 24 and 16 bits as compared to int respectively, it's not much, but it can add up. You can also pre-lock the length into a variable to help speed things up. The reason this works is because for loop check their condition (second block) every single iteration. This means that you are accessing the length property every single iteration. Accessing a member of a class is more expensive than accessing a single variable, and thus adds up over many iterations. The performance change might be slight, but it adds up pretty quickly, especially if you are iterating through hundreds of pieces of data every single frame. So, what should the for loop look like? Well, like this:

byte length = some.Length;
for (byte i = 0; i < length; i++) {
    // Code
}

The Importance of Humor

Many people say that having a good sense of humor is beneficial. This is the case with coding as well. Having a good sense of humor will make your code more readable as it will provide a little chuckle here and there, as well as keep you in a good mood. Working while happier is a great way to avoid lots of little errors. Not only that but, since you are in a good mood, you will be less tempted to distraction making you more efficient. Who doesn't want to complete "8 hours of work" in 5 or 6? Below is a little snippet of some good humor that I put in my code that may or may not be funny, but is of non-detrimental nature, and gave me a bit of a smile.

Coding Humor

Why yes, public health is important.

Particular Problems with Public Variables in Unity

The Unity documentation it indicates that all public variables are made available in the inspector for direct editing. While this is handy, it is also dangerous. This article will cover a few things regarding public variables, and how to avoid them in Unity.

Read more

Avoiding Long Compile Times

Long compile times are a real chore when it comes to testing things. It is especially cumbersome when all you are doing is making small edits resulting in 2 seconds worth of editing, and 2 minutes waiting for compiling.

Read more

The Importance of Class and Object Oriented Programming

While class is certainly important to have in real life, it is equally important to have while programming. Taking advantage of an Object Oriented Programming method (or OOP method) may very well be one of the most important higher concepts to learn and understand. This article helps cover what OOP is, its benefits, and how to use it.

Read more

Finite State Machines

To expand on my earlier post about enumerations I'm going to look at Finite State Machines in a relatively general sense. Let's start with breaking down what exactly a Finite State Machine is. We can look at the descriptive words to see that it is a machine, or system, that consists of a finite number of states. Great, but what does that mean? In a sense it is a way to package your code so only the desired pieces run at the desired time, this makes them very useful for succinct organization. In the rest of the article we'll look at different ways to implement the system, as well as the pros and cons of each.

Read more

Enumerate! Enumerate!

Fans of Doctor Who will probably shun me for that titling. What are enumerators, or, more specifically, enums? To put it succinctly, enums are words that are actually numbers. What does this mean? It means that when you write your code you can check for words, but when it gets compiled the compiler takes those words and replaces them with numbers. You may wonder why to bother with such a thing. It's pretty simple really. Enums allow you to have readable code while keeping a small overhead. They are better than character strings because they are smaller, and they are better than just plain integers because they can be rearranged, and are much more readable. Below is an example enum use:

Read more

Important Notes on Touch Input in Unity

There are a few things that you will want to keep in mind when working with touch in Unity. Mobile devices pass click events with their touch events, platform dependent compilation, rotations are a special type all their own, sometimes Unity hands back one thing when you need another, collisions are odd, and much more. This is the first of many posts on the quirks of Unity.

Read more

Getting Trig Quadrant from Sine and Cosine with only 2 IF statements

So, I was working on a shader network today and needed to get the quadrant of an angle in a simple fashion. In most cases people will use a switch statement or an if, else tree. I wanted to do it using fewer steps, so I started to look at it a different way:

Read more

The Pain of Windows

Doing my server automation programs on the windows platform, it was inevitable that I would have to #include <Windows.h> at one point and deal with one of the many 12 argument functions it has. Alas that day has come.

Read more