Allow your player to harvest natural resources (e.g. chop wood) using the Event System.
The Event System and Object Game Variables can be used to create regrowing resources like wood or wheat. In this tutorial we’ll create an event that allows us to chop wood on a regular basis. For this we’ll:
- adda new item
- create a harvest event
- add the event to a scene
Let’s get to it!
Adding the resource item
First, we’ll add the item that will be havested – in our case that’ll be Wood. Open the ORK Framework editor, navigate to Inventory > Items, add a new item and change the following settings.
- Name
Set to Wood.
Item Settings
- Item Type
Select Special Items. - Item Prefab
Select Item Prefab (can be found in Assets/Tutorial Resources/Prefabs/Items/). - Dropable
Enable this setting. - Stealable
Enable this setting.
Price Settings
- Value Type (Buy Price)
Select Value. - Value (Buy Price)
Set to 100. - Sellable
Enable this setting.
Use Settings
- Useable In
Select None.
And that’s it for the new item – click on Save Settings to save the changes.
The harvest event
Navigate to Events and click on New Game Event to create a new event. We’ll handle everything in this event, but you can optionally also use the Event Interaction to check for the game variable conditions that need to be valid to harvest wood. We’ll use Object game variables in order to allow using the same event on different trees (game objects), otherwise all trees would have the same timeout to prevent harvesting.
Event Settings
- Blocking Event
Enable this setting. - Block Player Control
Enable this setting. - Block Camera Control
Enable this setting.
Click on Add Actor to add an actor.
- Type
Select Object. - Event Object
Enable this setting.
Click on Add Actor again.
- Type
Select Player.
Check Game Variable
Add > Value > Variable > Check Game Variable
First, we’ll check if the player can harvest wood here. We’re using Object game variables (i.e. the variable will be bound to one object) to check a timeout – if enough time passed since the last harvest, the object can be harvested again.
- Variable Origin
Select Object. - Use Object
Enable this setting. - Object
Select Actor. - Actor
Select Event Object.
Click on Add Game Variable to add a variable condition.
- Condition Type
Select Variable. - Value Type (Variable Key)
Select Value. - Value (Variable Key)
Set to timeout. - Is Valid
Enable this setting. - Type
Select Float. - Check Type
Select Is Less. - Value Type (Check Value)
Select Game Time. - Time Offset (s)
Set to 0. - Rounding
Select None.
Show Dialogue (Failed)
Add > UI > Dialogue > Show Dialogue
This step is connected to the Check Game Variable step’s Failed slot.
- Dialogue Type
Select Message. - Wait
Enable this setting. - GUI Box
Select Bottom Dialogue. - Text
Set to You’ve already harvested here..
Show Dialogue (Success)
Add > UI > Dialogue > Show Dialogue
This step is connected to the Check Game Variable step’s Success slot.
- Dialogue Type
Select Choice. - Wait
Enable this setting. - GUI Box
Select Bottom Dialogue. - Text
Set to Harvest wood?.
Click on Add Choice to add the first choice.
- Text
Set to Yes.
Click on Add Choice again to add the 2nd choice.
- Text
Set to No.
Add To Inventory
Add > Combatant > Inventory > Add To Inventory
This step is connected to the Yes slot of the choice dialogue.
- Object
Select Actor. - Actor
Select Player. - Type
Select Item. - Selection
Select Wood. - Value Type (Quantity)
Select Value. - Value (Quantity)
Set to 1.
Change Game Variable
Add > Value > Variable > Change Game Variable
Finally, we’ll set the object game variable on the event object. We’ll set the variable we used in the initial check to the current game time plus a minute as an offset.
- Variable Origin
Select Object. - Use Object
Enable this setting. - Object
Select Actor. - Actor
Select Event Object.
Click on Add Game Variable to add a variable change.
- Value Type (Variable Key)
Select Value. - Value (Variable Key)
Set to timeout. - Type
Select Float. - Operator
Select Set. - Value Type (Float Value)
Select Game Time. - Time Offset (s)
Set to 60. - Rounding
Select None.
And that’s it for the event – click on Save Event and save it as harvestWood in Assets/Events/.
Adding the event to a scene
Open the town scene (1 Town, can be found in Assets/Tutorial Resources/Scenes/). Create a new empty game object using the Unity® menu: Game Object > Create Empty
We’ll add several components to the new game object – change the following settings.
Transform
- Position
Set to X=26, Y=4.5, Z=24.
Capsule Collider
- Is Trigger
Disable this setting. - Radius
Set to 1. - Height
Set to 3.
Event Interaction
You can either use the usual component menu to add it or use the ORK Scene Wizard.
- Event Asset
Select harvestWood.
Click on Ok to use the selected event.
- Start Type
Select Interact.
Object Variables Component
This component is required to use object game variables. It will automatically create a unique Object ID to identify the object’s variables. All objects that share the same ID will also share the same variables. There are no further settings needed here.
And that’s it – don’t forget to save the scene.
Now you’ll be able to harvest wood in the town’s center tree – and it regrows a minute after harvesting it.
Tip: Animating the harvest
Since we’re using the event system for the harvest, you can just add new steps to animate harvesting, e.g. playing a wood chopping animation and a sound effect using the appropriate event steps.
Also, you can use a Formula to calculate the amount of wood the player gets, e.g. basing it on a gathering status value.