RPG in a Box

RPG in a Box

Not enough ratings
Gatherable Resources || Visual Scripting Guide + Source Code
By [DGRT] Lespa
In this guide I will show you how you can create gatherable resources (ore deposits, trees, fishing spots, ...) for your game using the visual scripting tool. The source code for this project can also be found at the end of this guide.
2
3
4
3
   
Award
Favorite
Favorited
Unfavorite
Introduction and Prerequisites
PLEASE NOTE: This guide requires you to know only the basics of RPG in a Box.
We will not go into detail on the very basics like creating objects, tiles, characters or maps.
Following the tutorial on how to create your very first project (which can be found on the official wiki here[www.rpginabox.com]) should give you enough experience with the program to follow along this tutorial.

PREREQUISITES
Before we will start scripting we need to make sure we have all the necessary resources.

We will be using the following resources in this guide:

Tiles:
  • map tiles

Object:
  • gatherable resource (rock, tree, ...) => not passable

Character:
  • player

Additionally, we will also use the following animations:

Player:
  • idle
  • gather



Gatherable Resource:
  • default
  • gain

Creating a map
Before we do anything else, we need to make sure we have a map.
Go to the Map Editor, create a new map and fill in the ground with some map tiles.

We will be using a copper rock as our gatherable resource in this guide, so we'll place that somewhere on the map.
Now select the copper rock using the Edit Tool (F2) in the Map Editor and navigate to the Entity Properties tab.

Give the copper rock a unique Entity ID.
Optionally you can also choose a text to be displayed in the Tooltip Window.

Since we haven't yet defined any Navigation Types for players to interact with our copper rock we will do that now.
Select the Connect Tool (F3). You should now see the green lines indicating where the player can walk.



Select the Interact Only Navigation Type (orange) and connect these orange lines with the green lines so players are able to reach the copper rock and interact with it.

Creating the script (Part 1)
Navigate to the Script Editor.
Create a New Resource and select Script.

Step 1
We'll start by setting a new Entity Property for our copper rock.
Open the Expression Builder of the Set Entity Property node and choose Self as the type of entity.
The Property Name will be availableResources and we will give this a Property Value of 5, meaning the rock will contain 5 copper ores.
Now connect this node to the Starting Node.


Step 2
Now select the While Loop from the Available Logic tab and drag it onto the grid.
Connect the While Loop node to the Set Entity Property node.
Open the Conditional Expression Builder of the While Loop.
For the Element to inspect select Self Property.
For the Operator select greater than.
Value to check for should be a Number.
Now enter the correct data in the Expression field.

Since we want to check how many available resources there are left we should use the Self Property availableResources which we created earlier.
We only want this loop to run as long as the resources are not empty so the value to check for is 0.




Step 3
Since we don't want our player to run away and still receive resources we will lock the player movement while we're mining.
Add a Lock/Unlock Player Movement node to the grid and set the Movement to Locked.
Make sure this node is connected to the Do connector of the While Loop node.


Step 4
We want our Player to display the gathering animation we have created for him when he interacts with our copper rock.
Add a Play Animation node to your grid and connect it to the Lock/Unlock Player Movement node.
Open the Entity Expression Builder of the Play Animation node and select Player.
Now enter the corresponding Animation Name. This has to be the same as the name of your gathering animation for the player character!


Step 5
To make sure the player doesn't receive all of the resources at once we'll drag a Wait node onto the grid and set the timing to 5 seconds.
This will create some delay between each obtained ore.


Step 6
It would be nice to have some graphical cues to see whenever the player obtains an ore.
That's why I have created the gain_copper_ore animation.
Add a Play Animation node to the grid and open the Entity Expression Builder.
Select Self and press OK.
Now enter the corresponding Animation Name. This has to be the same as the name of your gathering animation for the copper rock object!


Step 7
If we start up our game right now we will see that our player character can interact with the copper rock, will display his gathering animation and the copper rock will display its gathering animation, however we will not receive any resources yet. That is something we will handle in the next step, but first we should make sure that our project works so far.

Save your current script and go back to the Map Editor.
Select the Edit Tool (F2).
Now select the copper rock and navigate to the Entity Properties tab.
Add your Script to the entity and save your map.

Make sure you have a startup script configured!

If you test your game now by using Quick Play you should have something similar to this:
Creating the script (Part 2)
Step 8

In order to give the player some copper ore we must first create an item.
Navigate to the Item Editor and Add a new Item.
Enter an Item Name and Item Description.
Make the item Stackable and choose a Stack Limit.
Optionally you can set some Tags for the item.
Save your changes and navigate back to the Script Editor.


Step 9
Once you have navigated back to the Script Editor, add a Give Item node to the grid and connect it to the Play Animation node.
Select the Item Name from the dropdown list and choose your resource item. (in our case copper_ore)
Set the Item Count to 1.


Step 10
Now that we have given the player some resources, we must also make sure there are less resources left to be gathered.
To achieve this we will first add a Assign Value node to the grid.
As always, connect this to the previous node, which is the Give Item node.

We will create a Variable called availableResources to store the amount of available resources and perform calculations with this variable later on.
We will give this variable the same name as the Self Property availableResources which we created in Step 1.

Open the Variable Expression Builder.
Select Self Property as the type of variable and enter availableResources as the property name.
Now we have the value of the Self Property which we created for our copper rock in Step 1.




Step 11
Add another Assign Value node to the grid and connect it to the previous Assign Value node.
Enter availableResources as your Variable.
Left click on the Equals sign (=) and change it to the Subtract sign (-=).
Open the Variable Expression Builder and select Number as the type of variable and enter 1 as the value.
Now the amount of available resources has been updated and has been stored in the availableResources Variable.
We will still have to store this value back into our Self Property availableResources later on. (See Step 14)




Step 12
We'd like to have the idle animation of our copper rock playing again when the gather animation has finished playing after obtaining a piece copper ore.
Since the animation takes a bit of time to complete we will first add a Wait node to the grid and enter a waiting time of roughly 2 seconds.
Make sure to connect this node to the previous node as always.




Step 13
Add a Play Animation node to the grid and connect it to the Wait node.
Open the Entity Expression Builder and select Self as the type of entity.
Enter the Animation Name of the default animation for your copper rock.




Step 14

Since our While Loop checks the value of the Self Property availableResources at the end of each iteration, we must make sure that we update this value to the new total of available resources to avoid an infinite loop.

Add a Set Entity Property node to the grid and connect it to the Play Animation node.
Open the Entity Expression Builder and select Self as the type of entity.

Enter availableResources as the Property Name.
Open the Variable Expression Builder and select Number as the type of variable.
In the Expression field of the Variable Expression Builder enter the name of our variable availableResources.
The Value field is not important because the value will be overwritten by the value of our Variable availableResources.


Step 15
Now we are done with Do connector of our While Loop.
The grid in your Script Editor should look like this:



If you save all your changes and test your game using Quick Play you should see that your character now also receives some copper ore every couple of seconds until all of the 5 copper ores have been gathered from the rock.
One slight problem is that the player character is still displaying a gathering animation.
It would also be nice to get a notification when we have gathered all resources there are to gather.
In the next step we will solve this by using the End connector of the While Loop node.
Creating the script (Part 3)
Step 16
Navigate to the Script Editor and find the While Loop node we created earlier.
The Do connector of the While Loop has been taken care of so now we will focus on the End connector which executes only after the conditions of the Conditional Expression have been met.

Since we have gathered all resources from our copper rock we must now re-enable the player movement.
Add a Lock/Unlock Player Movement node to the grid and place it near the While Loop node.
Set the Movement to Unlocked.
Make sure this node is connected to the End connector of the While Loop node!


Step 17
Add a Play Animation node to the grid and connect it to the Lock/Unlock Player Movement node we added in the previous step.
Open the Entity Expression Builder on the Play Animation node and select Player as the type of entity.
Enter the Animation Name of the idle animation of your player character in the corresponding field.


Step 18
Now the only thing left to do is to display a message when all resources have been gathered.
Simply add a Display Message node to the grid, connect it to the Play Animation node we added in the previous step and enter a message to be displayed to the player when all resources have been gathered.

Your complete grid should now look like this:



And that's the end of this guide.
If we test the game now our player character will return to his idle animation and a message will be displayed informing us that there are no resources left to gather.



Hope you found this guide helpful!
Let me know if something is unclear or if you need help with anything.
Source Code
set_entity_property(self, "availableResources", 5); while self.property["availableResources"] > 0 do set_player_movement_locked(true); play_animation(player, "gather"); wait(5); play_animation(self, "gain_copper_ore"); give_item("ITEM_0001"); availableResources = self.property["availableResources"]; availableResources -= 1; wait(2.2); play_animation(self, "default"); set_entity_property(self, "availableResources", availableResources) end; set_player_movement_locked(false); play_animation(player, "idle"); display_message("There are no resources left.")
5 Comments
Vuples 13 Dec, 2022 @ 4:46pm 
it works, however if I interact with the node after the display message appears it will still give me resources all I did was remove the animations (more focused on getting it to work before adding nonessentials) or is it supposed to "reset" after each time?
ogaldamez986 3 Dec, 2022 @ 3:47am 
This is really good, I was actually thinking on something like this but you made it first and better thank you so much.
WorkshopChangelog 28 Aug, 2022 @ 4:06am 
Amazing, thanks for sharing!
[DGRT] Lespa  [author] 12 Jul, 2021 @ 3:18pm 
@meltconsultancy
Thank you!
If you have any ideas for guides you would like to see than feel free to suggest them
meltconsultancy 5 Jul, 2021 @ 8:04pm 
Awesome detail guidance , thanks