Creating A Turn-Based Strategy Game in Unity
- Apr 21, 2021
- 3 min read
Updated: Apr 27, 2021
One of a number of projects I created during my second year on the games course was 'The Twilight Trials', a turn-based strategy game set in a fantasy world with voxel-style art created in Unity. This was the first time I got really into the C# programming side of development and took extra classes to improve my knowledge and experience in this area, not just because it was fun and interesting to learn something new but also because it has many uses in audio implementation side which I definitely benefited from since then. This was also a project in which I contributed to almost every area, from creating animations to the sound effects and music, as well as design, balancing and testing - though I'll focus on the programming here.
Having played games like Final Fantasy, TemTem and of course Pokemon, I was familiar with how I thought the battle scripting would play out as all of these examples use essentially the same format. Just go through the motions, and use your attacks each turn and then someone wins. Well, it turned out to be a bit more complex than that.

To create the overall structure, the battle is handled through a Switch statement which starts when a battle is entered. This is obviously much more beneficial than a whole load of if statements and long-winded conditions so it made a nice framework to outline the steps of the battle and check when a player has died etc, which could then be explored individually.
A new concept to me at the time was using Inheritance, Virtual Functions and Overrides. I personally struggled to understand the rabbit-hole-esqe nature of them but soon found out how simple and helpful they are. Essentially, instead of writing out each possible attack or item usage in the main script, you create neatly contained individual scripts which tell the parent on a need-to-know basis. For example, if its the players turn and they click an action, the main script follows a chain of virtual functions which is overridden by the exact attack that the player wanted and the effects that occur because of it. It almost plays like the "telephone game" where each person asks the next person for information until it gets back to the start looking something like: Action > Attack > Sword > Effect & Calculations.

Another new concept to me was scriptable objects which almost revolutionised the way I thought about about programming. They are honestly very simple when you think about it, especially for a strategy game when there can be loads of items, abilities, weapons and armour. In short: you can treat a scriptable object/scriptable variable (SVar) as if it were an item. You write a script which defines the properties of the, lets say weapon and then you can create as many new weapons of the same type quickly and easily in the inspector and just add the name or stats for example. These are objects in memory which means they can also be used in lists and counted which makes them great for an inventory system and lets designers create new weapons without needing to know any scripting.


Overall I learnt a lot in a short space of time and was able to flesh out the inventory, shop and battle systems quite well which gave more time for new features like a map, combat animations, sounds and also allowed for enough playtesting sessions from friends and peers. You can download the demo at Itch.io and try it for yourself here: The Twilight Trials.

Comments