Setup Faction Slots
Each map scene must have a list of faction slots defined within the Game Manager component which sets the maximum amount of factions allowed to play in that map scene and sets the default data and configurations for each faction slot, taken only into consideration when you directly launch the map scene and play it without coming from a lobby menu first.
1. Inspect Default Faction Slot
Below is a screenshot from the Game Manager component inspector where we have one default faction slot assigned:
1.1 Faction Slot Role
The Data includes information about the slot that defines its permissions within the game. For example, the Role field is assigned to Host which means that the player using that slot is the host of the game and has full administrative permissions (which is particularly more important in a multiplayer game than in a singleplayer game). In addition it defines basic information such as the Name and Color that will be used to identify the faction entities in UI elements. As for other faction slot Roles, we will discuss them when we create a new faction slot later.
1.2 Faction Type
The Type field in Data allows you to define a specific type for the faction occupying the slot which would allow other RTS Engine components to behave differently depending on that type. For example, you may have a building that you want to share between different faction types but the same building allows to create different units depending on the faction type. This can be done by specifying which unit creation tasks would be visible via the same building depending on the faction type and whenever that building is placed by a faction, it initializes its unit creation tasks by querying the type of the faction it is part of and only enables the unit creation tasks associated to that type. You can also leave this field unassigned, meaning that the faction slot does not posses any particular type.
A faction type is defined using the Faction Type Info scriptable object. In this section, we will create a new faction type and assign it to the default faction slot.
In the Project tab, right click and: Create -> RTS Engine -> Faction Type. This would create a new scriptable object for the faction type, rename it and select it to configure its fields:
The Name field will be used in case it is required by UI elements while the main Code field here must be defined uniquely for each faction type. In this tutorial, we will assign it to tutorial_faction. As for the Limits field, it allows to define amount limitations for the faction type for specific units and buildings (for example, you might want limit the amount of a specific unit type differently for different faction types) but we will be keeping this field empty for now.
Next, go back to the Game Manager inspector and assign the faction type to the default faction slot. This now represents the default faction type of this faction slot. When the game launches through a lobby menu that allows to pick a different faction type (in the lobby) then the faction type field will be overwritten by the one assigned in the lobby.
1.3 NPC Type
The NPC Type field allows to input a NPC Type scriptable object (which is created similarly to the Faction Type Info) that defined a non-player controlled type allowing to setup certain NPC-related components to handle the behavior of the faction in case its Role is set to NPC. Since this is a Host faction slot and for the sake of the simplicity of this tutorial (NPC related configurations would be handled later) then we will be leaving this field unassigned.
1.4 Local Player
Each faction slot Data allows to mark the slot as the local player through the Is Local Player field. When that is enabled for a slot then that would be the one controlled by the player. The Host Role in a single player setting is synonymous with the slot being controlled by the local player. However, in a multiplayer setting, the Host slot can be controlled by a different client than the local player. In the latter case, the client would have their slot Role assigned to Client instead of host and will have the Is Local Player field enabled for them.
When there is no faction slot that has Is Local Player field enabled then the player will not be able to control any of the faction entities in the game. This can be useful when you want to simulate the behavior of NPC factions playing exclusively against one-another without the intervention of the player.
In our case here, the local player is the Host of the game and therefore make sure that the Is Local Player field is enabled for the default faction slot.
1.5 Initial Camera Position
The Initial Camera Look At Position field allows you to drag and drop a Transform from the scene which the camera would initially look at when the game starts when the player is controlling the corresponding faction slot. In this case, we have it assigned to the initial building entity. You can play around with this field and set it to a different object, such as the initial unit entity in the scene and see how the camera changes its initial position when the game starts. Note that it does not have be assigned to the Transform of a faction entity game object as it can be assigned to any game object in the scene.
1.6 Initial Faction Entities
We have a unit and building spawned for the default faction slot. They are only properly initialized because they are assigned to the Initial Faction Entities field of the faction slot. No faction unit or building entity can be correctly initialized unless it is assigned to one of the faction slots.
This field also offers a way to customize the initial unit and buildings of a slot depending on the assigned faction type. For example, we will now allow the faction on this slot to have a secondary unit entity if its faction type is tutorial_faction (the one we created earlier). To do this, first select the new_unit entity object and duplicate it (CTRL + D), rename its game object to new_unit_secondary and move it away from the other unit object so that they are not stacked in the same position. Other than that, we will not be messing with any of its components for now, we want an exact replica.
Now back to the faction slot in the Game Manager inspector and specifically back to the first slot's Initial Faction Entities field. Leave the new_building and new_unit entities assigned under the All Types list, meaning that no matter the faction type assigned to this faction slot, those two faction entities will always be initialized. Now open the Type Specific array field, add a new element and do the following:
- Faction Types: Add one element and assign it to the newly created tutorial_faction
- Faction Entities: Drag and drop the new_unit_secondary object to this array field.
The above steps are telling our faction slot to only initialize the entities within the Faction Entities array field, which only includes the new_unit_secondary unit entity in this case, if the type of the slot is one of the types assigned in the Faction Types field, which only includes the tutorial_faction here.
Start the scene and you will now be able to select and move both of the unit entities, this is because we have assigned the faction type of this slot to tutorial_faction.
Now what happens when the slot's faction type is not assigned to tutorial_faction? Let's try that and see. Go back to the faction's slot Type field and set that to Unassigned and start the game while leaving the same inputs for the Initial Faction Entities field.
This results into the new_unit_secondary unit object immediately destroyed when the game starts since its faction type requirements does not match the type of the faction slot. This is one example of how the components can behave differently depending on a factor such as the faction type.
2. Create new Faction Slot
Next step would be to create a new faction slot. First, go back to the Game Manager and under the Faction Slots field, increase the size to 2. This would add a new element representing a new slot which in the case inherits the same configurations from the first default slot.
2.1 NPC Faction Slot Configurations
Update the fields of the second faction slot to the following:
- Role: Set to NPC as we want this second slot to not be controlled by the player.
- Change Name and Color so we can distinct it from the first faction slot.
- NPC Type: Leave unassigned as we only need this slot as a dummy NPC faction that does not have any type that regulates its behavior.
- Is Local Player: Disable this field as we only want the first faction slot be controlled by the player.
2.2 NPC Faction Slot Initial Entities
If you open the Initial Faction Entities field of the second slot we created, it points to the same entities of the first player-controlled faction slot which needs to change here. We want to create different entities to assign them to the new slot instead. For example, I have duplicated the new_building and new_unit entities of the first faction and renamed the duplicates to new_building_npc and new_unit_npc respectively then moved their positions away from the entities of the first faction slot.
Then we go back to the NPC faction slot field and specifically to the Initial Faction Entities and assign the newly duplicated NPC entities instead of the ones copied from the first slot and remove the type-specific initial entity (which is assigned to the new_unit_secondary, a unit entity of the first player-controlled slot). A last step would be to update the Initial Cam Look At Position to the new_building_npc Transform.
2.3 Test the NPC Faction Slot
Now you can start the game and you would notice the new NPC faction slot unit and building initialized with their different faction slot. Also notice that while you are still able to control and move your faction's unit, you can select but can not move (or make any other commands) to the NPC entities.
2.4 Swap NPC and Player Slots
If you want to control play using the second slot we just created then do the following:
- First slot:
- Role: NPC
- Is Local Player: Disabled
- Second slot:
- Role: Host
- Is Local Player: Enabled
In fact, when coming from a lobby menu which has a set of configurations for the faction slots, all the values in the Data field are updated according to these configurations.
Now start the game after swapping the Role and Is Local Player fields and notice that your initial camera position is now on the new_building_npc entity now and that you can control the new_unit_npc now and not the one from the first slot.
Download Files
You can download the results of this tutorial here.