Reader small image

You're reading from  Creating an RTS Game in Unity 2023

Product typeBook
Published inOct 2023
Reading LevelN/a
PublisherPackt
ISBN-139781804613245
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Bruno Cicanci
Bruno Cicanci
author image
Bruno Cicanci

Bruno Cicanci is a software engineer and game developer with professional experience on different technologies and platforms. Since 2009, Bruno helped to develop and publish many games, mainly using Unity for mobile devices. He writes about game development on his personal blog, which led him to present many talks at universities and events. Bruno holds a Computer Science BS. Degree and a specialization and Game Production and Programming. In the last decade, he worked at prestigious game studios such as Glu Mobile, Electronic Arts, and Aquiris. He is currently developing games and reusable technologies at Ubisoft. Originally from Sao Paulo, Brazil, Bruno currently resides in London, UK with his wife and two adorable cats.
Read more about Bruno Cicanci

Right arrow

Spawning an Army of Units

RTS games have a gameplay mechanic that allows you to spawn units and create large armies to battle enemies. But having so many objects in the same scene that can be easily created and destroyed requires good memory and CPU management, to avoid problems such as frame rate drops due to CPU spikes, out-of-memory issues, and memory leak issues due to inefficient memory usage. There is one programming pattern that works well in this scenario, which is called Object Pooling, and we are going to learn how to implement and use it to spawn our units.

In this chapter, we will create the configuration files for our units using the ScriptableObject class from Unity, which will have all the attributes we need to use a unit in our game. We are also going to see how to update the UI using a decoupled solution by learning how to implement and use the Message Queue pattern, which provides a great way for scripts or systems to communicate with each other without creating...

Technical requirements

The project setup in this chapter with the imported assets can be found on GitHub at https://github.com/PacktPublishing/Creating-an-RTS-game-in-Unity-2023 in the Chapter05 folder inside the project.

Configuring the unit ScriptableObject

In Chapter 3, Getting Started with Our Level Design, we were introduced to the ScriptableObject class, which we used to configure the level and map elements of our game, and saw how we can use it to create data files that are easily manipulated in the Unity Editor and with C# scripts. We are going to continue using this powerful yet lightweight class to configure the entire game, starting with the units that the player will generate and train before commanding them to gather resources, defend the settlement, attack enemies, and explore the map.

Even though we will have different units, such as warriors and mages with different stats, their properties (Level, LevelMultiplier, Health, Attack, Defense, WalkSpeed, and AttackSpeed) will be the same. Creating a solid foundation for the base stats will allow us to rapidly iterate the values for better game balancing and further customizations.

Let us start with a few scripts to define the UnitType...

Spawning units using the Object Pooling pattern

The Object Pooling pattern was created to optimize memory usage and CPU performance by reusing objects that are pre-allocated in a pool, instead of instantiating a new object when needed and then destroying it. When the ObjectPool script is created, a fixed number of instances will be allocated and added to a list. Then, when an object is required, the Object Pool will give you an existing copy. When you do not need the object anymore, for example when a unit is destroyed by the enemy, the object is returned to the Object Pool.

We can use the Object Pooling pattern when we frequently need to create and destroy objects that are all the same or very similar. A good use case is bullets that are fired from a weapon; they are all the same and created and destroyed a lot. In this case, the Object Pooling pattern will reduce the overhead on the CPU to create and destroy an object as well as making it faster to reuse an object instead of instantiating...

Updating the UI using the Message Queue pattern

The Message Queue pattern, also known as the Event Queue pattern, consists of a list of messages that are sent to all other scripts that are listening for a specific message. This is a powerful pattern that helps to decouple different scripts since you can just send a message with an update and the listener will receive and process it, without having shared variables across the whole project.

It also has good performance since the listeners are not checking for new messages on every loop. When a message is received, the message queue will trigger the listeners that are assigned to the type of message received.

We are going to implement the Message Queue pattern and use it to update the game UI and spawn new units. The idea is that when a new unit is required to be added to the game, a message will be sent by the script requesting it and received by the script that will then get a unit from the Object Pool. Both patterns work together...

Creating a debugging tool for the Editor

One of the easiest and fastest ways to test things while we are developing is by creating debugging tools to help us execute specific methods and trigger events so we can validate that our systems are working without having to play the game. This can be used for simple things such as adding more gold for the player or even spawning units and upgrading buildings.

Creating the Object Pool for the Warrior unit

Before we move to the actual debugging tool, let us first add the scripts we created so far to the Prefabs and GameObjects that will need them:

  1. Open the Level01 scene.
  2. Left-click on the + button in the top-left corner of the Hierarchy view, select Create Empty, and name it ObjectPools. In the Inspector view, in the Transform component, set X, Y, and Z to (0, 0, 0).
  3. Right-click on the new ObjectPools GameObject, select Create Empty, and name it BasicWarriorObjectPool. In the Inspector view, in the Transform component...

Summary

Well done on reaching the end of the chapter. We have built a brilliant foundation for the systems that we are yet to develop in the upcoming chapters of this book.

In this chapter, we learned how to implement the Object Pooling pattern, the Message Queue pattern, and even the Singleton pattern. All these patterns are widely used in professional games, and it is very important to learn about and implement them so we understand what game systems can benefit from them.

Even though we are building an RTS game, everything we saw in this chapter can be used and implemented in any kind of game. In fact, most of the scripts are generic enough that we could just copy them to a different project and use them out of the box. This is the beauty of well-designed classes and decoupled solutions.

In Chapter 6, Commanding an Army of Units, we will be getting more into the gameplay mechanics. We are going to see how to script actions that will spawn multiple units and how to select...

Further reading

Although we learned the basics of the three patterns introduced in this chapter, I recommend you check out the following links and learn more about them as well as see other examples and usages. We also saw a few new features from C# and .NET that are worth reviewing if you have any doubts about them, or just want to see more example usages. Here are the links:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Creating an RTS Game in Unity 2023
Published in: Oct 2023Publisher: PacktISBN-13: 9781804613245
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $19.99/month. Cancel anytime

Author (1)

author image
Bruno Cicanci

Bruno Cicanci is a software engineer and game developer with professional experience on different technologies and platforms. Since 2009, Bruno helped to develop and publish many games, mainly using Unity for mobile devices. He writes about game development on his personal blog, which led him to present many talks at universities and events. Bruno holds a Computer Science BS. Degree and a specialization and Game Production and Programming. In the last decade, he worked at prestigious game studios such as Glu Mobile, Electronic Arts, and Aquiris. He is currently developing games and reusable technologies at Ubisoft. Originally from Sao Paulo, Brazil, Bruno currently resides in London, UK with his wife and two adorable cats.
Read more about Bruno Cicanci