Create NPC Faction Type
1. Guide Initial Files
For this guide, you can start by downloading the following files in your project, on which we will base the changes we will be making in this section and the next ones. Please note that those files are based on the Demo game files so make sure to have those in your project.
Once downloaded, you will have a new directory in the project path: Assets -> RTS Engine Manaul -> 13_NPC_Factions.
In these folders, we have:
- Unit and building entity prefab variants of the demo game prefabs. We will be using those throughout the section of this guide. They include: villager, healer, archer (level 1 and level2) units as well as town center, barracks, farm and house buildings.
- tutorial_map_NPCFactions scene under the Scenes folder where a new RTS Engine scene with only one local player controlled faction and a baked navigation mesh is present. In the scene, we also have resource instances straight from the demo game prefabs. Additionally, the demo resource types are defined in the Resource Manager component inspector under Game Manager -> Essential -> ResourceExtension, namely: faction_power, food, knowledge, metal, population, treasure and wood resource types: And to make sure the resource amount is displayed correctly in the UI elements. In the hierarchy, under the path GameManager -> Modules -> UI and specifically in the ResourcePanelUIHandlercomponent inspector, we have filled its Data field with the 5 visible resources, namely: population, wood, knowledge, food and metal:
Launch the game and make sure no errors appear and that the unit is movable.
2. Create NPC Type
The NPC Type is an asset file that allows you to define a NPC faction type to be used when assigning and defining faction slots in the Game Manager.
In the project tab, right click and choose: Create -> RTS Engine -> NPC -> NPC Type.
Select the newly created NPC type, rename it to tutorial_npc_type and let's configure it its fields:
- Name: The name that will appear on UI elements to identify the NPC type for the player. Set to Tutorial NPC Type for example.
- Code: The unique code that will be used to identify the NPC type in the RTS Engine components. Set to tutorial_npc_type for example.
- NPC Managers: Each NPC type can be handle the behavior of a NPC faction differently based on the faction type assigned to the NPC faction. This is done using NPC Manager. An NPC Manager is a prefab object that is a collection of components that regulate the behavior of the NPC faction. We can assign a NPC manager prefab that will be used on all faction types of the NPC faction type or choose specific variations of that prefab that is used with specific faction types. In this tutorial, we will stick to having one NPC Manager for all faction types. For now leave this field empty and we will come back to it in a bit.
3. Create NPC Manager
To import the RTS Engine components into the scene, click on the RTS Engine top bar dropdown menu and click on Basic NPC -> New NPC Manager.
That will create a new NPC Manager prefab in the current path in your project tab:
Rename the new NPC Manager prefab to tutorial_npc_manager for example.
Open the tutorial_npc_manager prefab and take a look at its children objects: You will find multiple child objects where each one holds a set of components dedicated to handling a specific aspect of the NPC faction behavior. For example, the components attached to the NPC.UnitExtension child object handle the creation and management of the NPC units during the game.
Now open the tutorial_npc_type that we created earlier and assign this new NPC manager to it: Now the tutorial_npc_type will be using the components in the tutorial_npc_manager prefab to handle the NPC faction behavior for any faction type.
4. Create NPC Faction in Map Scene
Open the map scene and in the Game Manager inspector, add a new faction slot for the NPC faction: Make sure the new faction slot has the following properties:
- Role: Set to NPC.
- Name: Pick a new name for it, like NPC.
- NPC Type: Assign to tutorial_npc_type, the type we created earlier.
- Is Local Player: Make sure this is disabled.
- Initial Faction Types: Create a copy of the town_center demo prefab and assign it to the NPC faction slot.
Now we have two faction slots in the map scene, one for the local player and one for the NPC faction with a town_center prefab for each.
Start the game and notice in the hierarchy that there is a new instance of the tutorial_npc_manager prefab that is created. That instance is now responsible for handling the behavior of the NPC faction in the map!
Download Files
You can download the results of this tutorial here.