NPC Resource Collection
1. NPC Resource Manager
Open the tutorial_npc_manager prefab and find the NPC Resource Manager on the *NPC.ResourceExtension child object. This component is the one responsible for managing and monitoring resource entities in the NPC faction's territory.
- Resource Need Ratio Range: How safe does the NPC faction use their resources? The higher, the safer. For example, if the need ratio is set to 2.0 and the faction needs 200 woods. Only when 400 (200 x 2) wood is available, the 200 can be used. Must be >= 1.0!
- Resource Default Exploit Ratio Range: Ratio of the resources to be exploited by default by the NPC faction. 0.0 means no resources to exploit by default and 1.0 means all available resources will be exploited by default.
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 managing resources.
2. NPC Resource Collector
In the tutorial_npc_manager prefab, find the NPC Resource Collector on the *NPC.ResourceExtension child object. This component is the one responsible for managing the collection of the resources inside the NPC faction's territory.
- Collectors: List of units with the Resource Collector component that the NPC faction can use to collect resources. 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 resource collectors that play for those different types in this list because they will be filtered once this component is initialized (meaning only the collectors that fit the faction type and NPC type will be activated and used by the NPC faction). We drag and drop the tutorial_villager unit in this field.
- Collection Data: A list of resource types and settings regarding regulating their collection by the NPC faction. In this field, we want to add the food, metal and wood resource types and define the following properties for each:
- Type: The resource type whose collection will be regulated by the next settings.
- Instance Collectors Ratio (between 0.0 and 1.0): How many collector will be assigned to collect this resource type? (Per resource instance!).
- Max Collectors Ratio Range (between 0.0 and 1.0): The total maximum amount ratio of collectors that can collect this resource at the same time.
- Min Collectors Amounts (at least 0): To ensure that the NPC faction collects this resource type, you can set a minimum collector amount which the NPC faction will prioritize.
- Collection Timer Range (in seconds): How often does the NPC faction check for resources to collect?
- Enforce Min Collectors Timer Ticks (in seconds): How many collection timer ticks are required for the NPC faction to enforce the minimum collectors amount for each resource type by rearranging collectors?
- Collect On Demand: When enabled, other NPC components can request to collect resources.
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 collecting resources.
3. Testing
3.1 Adding Farm Building
First, we want to add a NPC Building Regulator Data for the tutorial_farm building. We follow the same steps described in this previous guide guide to create one and assign it to the tutorial_farm building. An example of the regulator data fields is shown in the following screenshot:
We assign the tutorial_npc_farm_regulator data to the tutorial_farm prefab:
And finally, in the tutorial_npc_manager prefab, on the NPC Building Creator attached to the NPC.BuildingExtension child object. We add the tutorial_farm building prefab to the Independent Buildings field so that the NPC faction will be able to create it. We went through NPC building creation in a previous guide.
3.2 Testing Resource Collection
Now play the map scene and notice that the NPC faction will start placing and constructing tutorial_farm instances and sending tutorial_villager units to collect food from them. Notice also how the NPC faction is managing and dividing its tutorial_villager instances between the available resources and how it is able to shift them from constructing buildings to then going to collect resources so that no tutorial_villager unit remains idle for a long time. We will now go through some of the logs:
3.2.1 NPC Resource Manager Logs
In the inspector, go to the Logs tab and you should see NPC Resource Manager Logs at work:
- Target Resources Logs: This field includes all building center instances with a Border component and for each one, we can see the resources in that border's territory divided into the ones that the NPC faction is actively attempting to collect from and the ones that are not being considered to be collected.
- Faction Resources Logs: Each resource type in the map is shown here with tracking the current amount/capacity and the reserved amount/capacity values. So here you can see what resources the NPC faction has and what is reserving/holding to spend on creating units/resources.
- Event Logs: This list shows us, with timesteps, the decisions that the NPC Resource Manager is taking.
3.2.1 NPC Resource Collector Logs
In the inspector, go to the Logs tab and you should see NPC Resource Collector Logs at work:
- Collection Tracker Logs: This field includes all the targeted resource types that the NPC faction is using resource collectors to gather. And for each type, it shows you the amount of the currently active collectors and a list of the resoruces that are actively being collected.
- Event Logs: This list shows us, with timesteps, the decisions that the NPC Resource Collector is taking such as sending resource collectors to collect certain resources.
3.2.2 NPC Resource Collector Logs
Your scene should look something like the following:
Download Files
You can download the results of this tutorial here.