NPC Building Creation
1. NPC Building Creator
Open the tutorial_npc_manager prefab and find the NPC Building Creator on the NPC.BuildingExtension child object. This component is the one responsible for the creation of buildings for NPC factions.
- Default Regulator Data: In case this component or any other NPC component attempts to create a building that does not have valid regulator data defined and this field is assigned, the regulator defined here will be used for that building.
- Independent Buildings: Drag and drop the building prefabs that you want the NPC faction to create. If the NPC Manager that this component belongs to is supposed to be used for different NPC types or different faction types then you can have buildings that play for those different types in this list because they will be filtered once this component is initialized (meaning only the buildings that fit the faction type and NPC type will be activated and used by the NPC faction). Here you can add the tutorial_house unit, for which we set up the NPC Building Regulator Data for the tutorial_npc_type. This means that the NPC faction will be creating the tutorial_house building based on the regulator data we set up in this previous section.
We also want to enable all the logs data so that when the game is running, we can follow the decisions that the NPC faction is making in regards to building creation.
2. NPC Building Placer
In the tutorial_npc_manager prefab, on the NPC.BuildingExtension child object, find the NPC Building Placer component. This component is the one responsible for the placement of buildings for NPC factions.
- Reserve Placement Resources: Reserve resources that will be used to place the placement buildings so that the player faction does not consume them before the placement is completed.
- Placement Delay Range (in seconds): NPC faction will only consider placing each new building after a delay sampled from this range has passed. This allows to introduce randomness into the building placement process of NPC factions.
- Rotation Speed: NPC pending building placement is a process where the building rotates around a build around position and gradually moves away from it until an appropriate placement position is found. This field represents how fast will the building rotation speed be?
- Placement Move Reload (in seconds): Time before the NPC faction decides to try another position to place the building at. Currently, this component just moves the to be placed building from its build around position a distance that can keep specified in the 'Move Distance' field.
- Move Distance: Every time the 'Placement Move Reload' time is through, the pending building will be moved away from its build around position by a distance sampled from this field.
- Placement Move Reload Increment (in seconds): Each time the NPC faction attempts another position to place a building, this value is added to the Placement Mvt Reload field value.
- Height Check Reload (between 0.0 and 1.0): How often is the height of a building sampled from the terrain's height per second? We do not need to do this every frame as it may become an expensive computation but we can do it often enough to get good results. This depends on the height variations in your map, so the more height variations you have, the more often you want to sample the height of the map when placing buildings.
You can assign values to this component like the ones in the screenshot or tweak them to your preference. And make sure to enable log-related fields in the Logs tab.
2. NPC Building Constructor
In the tutorial_npc_manager prefab, on the NPC.BuildingExtension child object, find the NPC Building Constructor component. This component is the one responsible for the construction of buildings for NPC factions.
- Builders: List of units with the Builder component that the NPC faction can use to construct its buildings. Here we can drag and drop the tutorial_villager unit.If the NPC Manager that this component belongs to is supposed to be used for different NPC types or different faction types then you can have units that play for those different types in this list because they will be filtered once this component is initialized (meaning only the units that fit the faction type and NPC type will be activated and used by the NPC faction).
- Construction Timer Range (in seconds): How often does this component check whether there are buildings for the NPC faction to construct/repare?
- Enforce Builders Timer Ticks: How many construction timer ticks are required for the NPC faction to enforce the builders amount by switching them from other tasks such as resource collection?
- Target Builders Ratio: Ratio of the amount of builders to send for construction to the maximum allowed amount of builders of a building.
- Construct On Demand: When enabled, external components can request to construct buildings.
You can assign values to this component like the ones in the screenshot or tweak them to your preference. And make sure to enable log-related fields in the Logs tab.
2. Testing
Now open the map scene and play it. In the hierarchy, find the the tutorial_npc_manager instance and find the NPC Building Creator, NPC Building Placer and the NPC Building Constructor components attached to its NPC.BuildingExtension child object:
Now go through each component's log tabs:
2.1 NPC Building Creator Logs
In the inspector, go to the Logs tab and you should see NPC Building Creator at work:
- Active Building Regulator Logs: Each element of this list represents a building that is being handled by the NPC Building Creator. Here we see we only have one element, which is the tutorial_house building. And we can also see the attributes assigned to this building's regulator such as the maximum pending amount, minimum and maximum allowed amounts. This also shows us the target amount that the NPC faction is aiming to create for this building as well as the current amount and the current pending amount. Finally we have a list of Creators which show us the currently available Builder units of the NPC faction that can create this type of building (in our case, the villager_unit) and the spawn timer which is the period at which the NPC faction considers creating a new instance of this unit type.
- Event Logs: This list shows us, with timesteps, the decisions that the NPC Building Creator is taking. In the first one, we see it activated the building center regulator for pre-spawned town center building and then we see it has recongized the tutorial_house building regulator and activated it. Then we see it successfuly launching building placement requests as soon as the tutorial_unit instances are created.
2.1 NPC Placer Creator Logs
In the inspector, go to the Logs tab and you should see NPC Building Placer at work:
- Placement Queue: Each element of this list represents a building that is being actively placed by the NPC faction. Here we see that it is placing the tutorial_house building.
- Event Logs: This list shows us, with timesteps, the decisions that the NPC Building Placer is taking. We see it accepting placement requests coming from the NPC Building Creator and reporting that it successfully completed their placements.
2.1 NPC Building Constructor Logs
In the inspector, go to the Logs tab and you should see NPC Building Placer at work:
- Target Buildings: Each element of this list represents a building that is under construction and being monitored by the NPC Building Constructor
- Event Logs: This list shows us, with timesteps, the decisions that the NPC Building Constructor is taking. We see it actively attempting to send builder units to construct buildings.
Your scene should look similar to this by this point:
Download Files
You can download the results of this tutorial here.