Builder Unit
We now need to configure units so that they are able to construct buildings. Load up the tutorial_unit prefab and find the unit_components child object. Now add the Builder component.
In the beginning of this component's inspector, you can see:
Entity Component (Source: IUnit - Target: IBuilding)
This means that this component is attachable to entities of type IUnit (which refers to the interface used by unit entities) and that it can have IBuilding (interface used on building entities) as its target. Let's go ahead and start configuring it.
1. General
- Code: Pick a code that is unique among other components attached to the entity.
- Is Active: Make sure this is enabled.
- Priority: The other entity component that allows for a target that is attached to the tutorial_unit is the Unit Movement component configured in [this part](xref:uid: Units_SetupUnitMovement) of the beginner's tutorials. We set the Priority of setting the movement target to 0 and we will be keeping it as the one with the top priority, therefore we set this field here to 1. What this means is that the movement will be considered for setting the target before the builder is when the player has the unit selected and uses the right-mouse click to set a target.
- Require Idle Entity: Enable this field to enforce that the unit must be idle and not have any other components with active targets when it is constructing a building.
2. Target Search/Picker
- Target Finder Data: This field allows the builder to automatically look for buildings to construct or repair within a specific range without the player commanding them to do so. When enabled, you need to set the following properties:
- Reload Time: Period of time (in seconds) before the unit searches automatically for a target in its range.
- Range: The size of the target search range.
- Idle Only: When enabled, the automatic target search executed by this component can only happen if the entity is idle, meaning that no other target component attached to the entity has an active target.
- Can Construct: Can the builder construct and repair buildings? When disabled, the builder will only be able to place buildings but not actually them. Keep this enabled.
- Construction Target Picker: This field defines what buildings the unit is allowed to construct. To keep things simple, we will leave the Type assigned to All to allow the tutorial_unit to construct all buildings.
3. Setting Target Tab
- Set Target Task UI: An entity component with an ability to set a target allows the player to set that target either by a right-mouse click on the target while the entity is selected or by a dedicated task that appears in the dedicated UI task panel when the entity is selected as well. The data that defines the task are assigned here. We went through setting up such a task in the Add Movement Task section. However we will leave this component without a dedicated target assignment task.
- Order Audio: This field allows you to assign one or multiple audio clips where one is played when the player assigns a new target. If you have multiple audio clips to assign, then the Fetch Type field allows you to determine how the clip is chosen each time. The Cooldown field allows you to input a cooldown time for playing the audio clip. In case you do not want the target assignment order audio to be played each time when you are spamming it, then a cooldown value of > 0 would allow for that.
4. Handling Progress
- Progress Max Distance: When the builder has a target building to construct, it gets a position assigned around that building to move to before starting to construct. The builder is only allowed to construct the target building as long as it stays within the target building's radius plus the value assigned in the Progress Max Distance field, which we will be setting to 0.5 to make sure that the builder stays close to the building it constructs.
Depending on the Construction Type value in the Building Manager component which we set in the previous guide before configuring the building prefab.
Construction Type set to Health:
The construction occurs periodically where the period is assigned in the Progress Duration field and for every period, a value of health assigned in the Health Per Progress field is added to the target building under construction. In this tutorial, we are using health based construction so we will set the Progress Duration to 1 second and the Health Per Progress is 10 health points. So every second, the builder add 10 health points to the target building.
Construction Type set to Time:
The Time Multiplier field represents how much the builder can speed up the required construction time. When a building is being constructed, the time required to complete the building is equal to the max total building Build Time (assigned in the Building Health) divided by (1 + the sum of the multipliers of all active builders).
- Progress Override Controller: To make the tutorial_unit have a unique animation that plays while it is actively constructing a target building (while in progress mode is enabled), we need to assign it a full animator override controller that replaces the default one only when the construction is in progress. We went through creating the animator override controllers for units in the unit creation tutorial in full details. But in short, find a path in the project tab and right click then choose: Create -> Animator Override Controller and assign the EmptyInProgressAnim with an animation clip to play for the builder while it is constructing a building. We will leave it as unassigned to keep things simple for now.
The rest of the fields in this tab are explained in the inspector's tooltip or in the Builder component documentation page.
5. Placement Tasks
In the Placement Tasks tab, we can define the actual tasks that would appear on the tutorial_unit task panel when it is selected to allow it to place new buildings on the map.
Go ahead and and add one new element to the Creation Tasks array:
5.1 General Task Properties
Task UI: In the Add Movement Task tutorial, we went through creating an Entity Component Task UI asset which defines data required to draw a movement task in the task panel for the tutorial_unit. The process of defining the UI data for the building placement task is fairly similar. Therefore, we will not be covering the creation of the UI task data in the same details so make sure to refer back to that tutorial for more details.
Right click on the Project tab and Create -> RTS Engine -> Entity Component Task UI Asset and call it tutorial_create_building_task.
Select the newly created asset and input its fields with the following:
- Code: Set to a unique code that differentiates it from other task UI assets.
- Enabled: Make sure this is enabled to allow the task to be drawn.
- Display Type: Choose Homo Multiple Selection because we want the task to be drawn when the tutorial_unit unit type is selected, even when there is more than one selected.
- Icon: This is the icon of the unit creation task that will be drawn on the task panel. Since we will be creating the tutorial_building using this task then we can use its own icon, which is available within the demo game files under the name house_icon.
- Panel Category: In the tutorial_map created and configured in the past tutorials, we kept the four default task panel categories. We can set this value to 0 which means that the task will be drawn on the first category of the tasks panel.
- Force Slot/Slot Index: We want to have this task drawn on the first task slot of the first task panel category, therefore we set the Slot Index field to 0 (first slot) and enable the Force Slot field. This is of course only an example of how you can configure these two fields.
- Tooltip: The descriptive text that appears when the player hovers their mouse over the task can be assigned in the Description field. Make sure that Tooltip Enabled is enabled for the text to actually appear and enable the Hide Tooltip On Click field so that the tooltip disappears when the player clicks on the task to launch building creation tasks.
- Reload Time: This field represents the time that is required between launching the unit creation task and the unit being actually created and available for the faction to use. This field is only relevant for pending tasks which is not the case for building placement tasks, therefore its value is irrelevant here.
- Cursor: This field is irrelevant for the building creation tasks. You can checkout the Add Movement Task tutorial where more details are provided on the use case of this field.
This how the inspector of the tutorial_create_building_task UI task asset should look after the modifications above. Now drag and drop it into the Task UI field, find and select it in the dropdown menu of that field or search for it in that field and select it.
Required Resources: We will get back to this field once we have gone through resources in later tutorials. For now keep this array empty.
- Faction Entity Requirements: This field allows you to define a series of faction entities (units or buildings) that the faction slot must have created prior to attempting to create the building associated with the task. This is especially useful when one of the requirements in your game is having certain units or buildings created and available for a faction before other types can be produced. To keep the tutorial simple, we will also be keeping this array empty.
- Missing Requirement Data: This field affects the visual aspect of the task on the task panel when the player does not have the required resources or faction entity requirements to launch it. The Icon field allows you to change the icon defined in the task UI asset in case the requirements are not met. When the Icon field is not assigned, the Color is then used to change the color of the original task UI to indicate to the player that the launch requirements are not met. In this case, we will want to have the task icon appear in red color when requirements are lacking. Therefore, keep the Icon field empty and set the Color field to red (make sure that the alpha channel is not 0 so that the task icon does not become entirely transparent).
5.2 Building Creation Task Properties
- Prefab Object: Drag and drop the tutorial_building prefab (one we created in past tutorials) into this field. This defines the actual building that will be placed through this building creation task.
This is how the Placement Tasks tab look after the above modifications.
As for the Upgrade Target Creation Tasks, that array is reserved for re-defining the building creation tasks' data when one of the buildings is upgraded. For now, we haven't dealt with upgrades so we will come back to this in later tutorials.
Download Files
You can download the results of this tutorial here.