When I first started developing Magic Market, I was extremely new to game development. I was new to coding in C#. And I was new to using Unity 3D. Yes, I completed my first game, but, needless to say, it is a giant mess of code. I had zero understanding of proper, efficient ways to organize the code. What I did was create one, large monolithic script that controlled 90% of the game's functions. To manage this, I had many bools and tons of if-statements to control the logic and flow of the game. As I said, it works, but getting it to function smoothly was rough. I spent months debugging issues. The main problems I found were that I could not easily control which aspects of the game were occurring at which times, and therefore, I often had multiple functions executing on top of each other when they should not be.
With my second game, I vowed to learn from my mistakes. Lately, I've been learning to develop and use a State Machine to control the game logic. The concept is fairly simply, but the implementation has taken me some effort to fully understand. Basically, the idea is to create unique "game states" that describe each individual part of the game. The graphic, shown above, gives a sense of what that might look like for my new game "The Apothecary". The State Machine still functions as the overarching controller for the game, shifting the code from one state to the other based on user input and game events. For example, the game starts in the Main Menu State which is fairly self-explanatory. In the Main Menu State, the user is limited to selecting a few buttons. Nothing else can be modified. The user cannot access the inventory. The user cannot accidentally sell any items. The user cannot see their next task. This prevents the other parts of the code from being modified "out-of-turn". If the user selects the Start Game button, the Main Menu State is closed and the Game State is opened. As each state is closed and opened, a function is called automatically and everything inside those functions are executed, including event calls that you have subscribed to. This is very convenient and has allowed me to set-up the scenes and GameObjects that are required for each specific scene, closing out and disabling all others so there is no cross-contamination. Anyways. This isn't meant to be an in-depth description of what a State Machine is, only my personal experiences with setting up and using one. I relied heavily on YouTube tutorials for learning about State Machines. There are several that I found useful and have included them here. I definitely recommend learning about them. Take the time now, before it's too late. Don't repeat my mistakes! See if they might be useful for you:
0 Comments
|
Author: Craig S.ArchivesCategories |