Lam Cao's Blog

A Journey in Game Dev

[Unity Tips] Tint color in PlayMode

We all have been there tweaking a scene, making a ton of changes just to realize we are in PlayMode for the whole time. It is frustrating. Let’s not make the same mistake over and over.

We can set Playmode tint in Edit -> Preferences -> Colors to something extreme like red. Our eyes will just stop us from making any serious changes during PlayMode.

(more…)


[Unity Tips] Use CompareTag instead of string.Equals

Comparing tags is a thing we all do in every game project at some point but not everyone seems to know about the existence of CompareTag. Unity could really have done a better job documenting this useful method.

In short, use obj.CompareTag("someTag") instead of string comparisions such as obj.tag=="someTag". CompareTag is much faster and will log an error if a tag is not defined, which is a very good thing.

In case of comparing against a list, use list.FirstOrDefault(t=>obj.CompareTag(t))!=null rather than something like list.Contains(obj.tag) for the same reasons.

(more…)


A singleton utility for gameObjects

I use a fair number of singletons in my game projects. And I think I have written something similar the code block below for too many times now. So have you, probably.

void Awake() 
{
    if (instance == null)       
        instance = this;
    else if (instance != this)  
        Destroy(gameObject);
    DontDestroyOnLoad(gameObject);
}

Getting tired of copy&pasting that code snippet over and over, I want to archive the same thing in just one line of code. When someone looks at my code, I want them to recognize the singleton pattern in less than half a second – “Oh hell, singleton again!”

(more…)


How I learned to use State Design Pattern in my game

I learned finite state machine (FSM) in model of computation class at a university. I’ve loved it ever since. It turns out to be one of the most commonly used patterns in game development. State design pattern is not only easy to understand and implement but also very flexible. I think every game programmer should know about it, and ideally how to employ it.

I believe the best way to learn programming patterns is to apply them. In a recent game project, I learn faster by doing than reading or watching even though I enjoy good reads and tutorials from time to time. In a recent project, I had a chance to look closely at and evaluate (many of) the design choices that make FSM either very efficient or extremely painful to debug and maintain.

(more…)