Image: Showing the inspector for the boss behavior script, easily changeable values for different behaviors. (Note: the script isn’t 100% yet, but still needs checking what states we want it to use)
During the last week I’ve been kind of polishing and helping out where it was needed due to that our game is basically finished except for some smaller things here and there. This meant that I would be assisting our game designer in his work when he needed things fixed immediately or wanted to add something which was a very effective way of working for us. Except that I was mainly focusing on creating all the sprites and animations for the boss that I mentioned in the last post.
What?
What I’ll go through today though, is how I kept my code structured and following a coherrent pattern throughout the entire game, so that I would be able to easily change and rewrite code without any critical effects on the game. Also, trying to not nest code but have each script work solely by itself, but being able to use other scripts within it, without any actual need for it.
How?
The first thing for me when I started off with this project, was to learn how unity actually worked with its own structure. After I had gotten a good picture of it, I found that making as much as possible available to the designer in the inspector was to be prioritized highly. The things that I focused on here for the enemy, would for example be; the health, speed, damage and objects such as the player that it would need to be aware of. In the levelhandler that I made earlier had several states that the game could be in, in other words, I wanted the designer to be able to change and try out different states and combinations as well as throw in specific scenes to load and when as well as GUI objects on screen. There wouldn’t be any need to go into the code, but just throw in the necessary objects and type in the values for it to work in any other way. In other words I made as much as possible public, at the same time that nothing that wasn’t valuable for the designer, wasn’t.
Also, not just making the game changeable for the designer but also for me or anyone else going into the code – the code needed a good coherrent structure. What that would include is:
- Good naming conventions
- Structured functions and logic
- Common pattern throughout the entire solution
- No magic values and non declared variables.
Good naming conventions
I wasn’t very focused on this part, since ordinarily I’m using letters before the variable names such that if it’s a parameter in a function, I’d call it p_variable. But instead I focused for the entire scripts to always have the same way of thinking, like naming a counter just used for the attack – attackCounter and a counter for an ability – abilityCounter. The preferred general structure for variables would be using the before letter for telling where it is able to be used, but also using camel casing, meaning that I would begin each new word with a capital letter except for the first one. A typical one like this would be the following: p_playerHealth.
Structured functions and logic
As soon as there is something that needs to be done or calculated I would like to use a separate function within the script but especially if any code is used more than once. Also, each functions would typically do similar things and have naming that would make it obvious what it does.
Common pattern throughout the entire solution
What I mean by this is that if someone else would go into the code, they would find similarities throughout whatever script they went into, knowing that the structure is the same and can not harm any other script by adding or removing things.
No magic values and non declared variables
One of the most important things in the way I structured things is that everything, especially game objects that the script would use, is always set to “null” at the start so that the object wouldn’t have any weird data within it. Also, checking before using the object that it is not null to prevent any errors within the program(Null checking).
Also, not using a specific function in the code, shouödn’t put the processors up to action with it, so often using bools to define whether a script is using a certain behavior or method was preferred for me, in other words disabling an entire block of code depending on whther the designer wanted it or not by just unchecking a bool box.
Why?
Why I as a programmer would like to follow a certain pattern or structure throught the game development process would be that it’s so much easier for me to write the code as a start since I know where to look since each part has the same structure or naming conventions. Also, making changes in the future is a lot easier, since the code looks similar everywhere. But one of the best parts about it is that the designer can use the structured inspector to easily try out different settings and values so that the designer wouldn’t need that much help from me during this process.
The way I did this is also a very good way to prevent any errors or glitches in the game.