Factory design pattern is used very often in Unity, but you might not even realize that. Well, you might don’t know that this is even a thing! Don’t believe me? When was last time when you instantiate prefabs in your project from code?
Yup, you used factory design pattern there without even knowing! Congrats! 🥳
One of my favorite use of State Machine design pattern is using states as different controllers of a game or an app. And today I’m going to present to you how you can implement such a thing in your Unity project! 😉
As previously on AI subject with State Machine, we won’t change the implementation of State Machine and Base State scripts. They are staying the same. Maybe besides one reference and function call… 😜
Just a few days ago I made a post about implementing State Machine in Unity. I know that having design pattern implementation and nothing else is less useful, so I decided to make small AI that will use State Machine in a bigger context.
What this example is?
In the example that I’ve prepared we are making logic for units in some RTS game. By default, they are aggressive, and they are trying to get rid of the enemies nearby them.
We are going to have 2 types of units. First one will be a knight who will attack other groups. The second type will siege machine that will only be able to attack buildings. Both will have the same components, but parameters might differ a little bit.
Did you ever heard of term “design pattern”? Do you know what it means? Or do you know examples of it?
Today we will find out about one of the most important design patterns in game development! But beware, this might change you forever! 😱
What design pattern even is?
But first things first! What is the design pattern you might ask? I’m going to prepare a separate post only about that subject, so, for now, I have just a quick explanation.
A design pattern is a high-level idea or proposition on how you can write code. Each design pattern has its pros and cons, so you have to learn about them as much as you can to know where and when to use them in your project.
There is a lot of young people wanting to get into the game industry. Maybe even you are one of them! I was! And like everyone else I needed to start somewhere!
Who cares about finals? 😅
Back in 2012, when I was in the last class in high school, I should have to prepare for finals, but I had something completely different in my mind. 😵
That thing was “making games”. It was occupying my brain so much that after coming back home from school, I was looking around on the web for any available information about game engines. I was obsessed about that, and I bookmarked over 15 of game engines, and probably over 80 articles related to making games or game engines. But I had a problem. I found so many information that I couldn’t decide which one I want to use! 🙄