24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Godot Tutorials
Create Complete 2D & 3D Games
Free Ebooks Free Courses All Courses
Build a First-Person Shooter with
Godot
ACCESS NOW
Online Courses
Godot Mini-Degree
2D Tutorials
3D Tutorials
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 1/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
How to Create an RPG in Godot – Part Search …
2
25/02/2020 by Daniel Buckley
I need help with …
Table of contents
1. Introduction Platformers FPS Games
2. Project Files
3. Camera Follow
4. Creating an Enemy
5. Scripting the Enemy Strategy
RPGs
6. Player Functions Games
7. Player Interaction
8. Chest Interactable
9. Creating the UI
Learning Game Design
10. Scripting the UI Coding
11. Conclusion
Introduction
Welcome back everyone! It’s time to nish our 2D RPG in Godot! Related Tutorials
Free eBook – Godot Game
For Part 1 of this tutorial series, we started setting up our 2D RPG project in the open- Development for Beginners
source and free Godot game engine. Some of the things we learned about include: Create a Top-Down 2D Character
tilemaps, player controllers, raycasting, sprite animations, and more! All in all, we How to Track Players with Godot’s
have a great base to work with and expand on! Camera
An Introduction to the Godot Editor
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 2/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
However, what’s an RPG without some enemies or items to loot? In this second part
of our 2D RPG tutorial for Godot, we’ll nish by adding these elements, as well as
adding the ability for our camera to follow the player.
Tags
So sit back, and prepare yourself to nish your rst RPG in Godot!
fps tutorial platformer
Project Files
tutorial rpg
For this project, we’ll be needing some assets such as sprites and a font. These will be
sourced from kenney.nl and Google Fonts. You can, of course, choose to use your own tutorial strategy game tutorial
assets, but for this course we’ll be using these. web class
Download the sprite and font assets here.
Download the complete Godot project here.
Don't miss out! Offer ends in 3 3 14
Hours Minutes Seconds
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 3/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Build a First-Person Shooter with
Godot
Master 3D game development basics with Godot while
building your own FPS game.
Access all 200+ courses
New courses added monthly
Cancel anytime
Certi cates of completion
ACCESS NOW
Camera Follow
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 4/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
With our big scene, there’s one problem which is the fact that the camera doesn’t
move. To x this, let’s create a new Camera2D node in the MainScene.
Then we can create a new script attached to the camera node. Call it CameraFollow.
All we’re going to do here is nd the player and move towards them every frame.
1 onready var target = get_node("/root/MainScene/Player")
2
3 func _process (delta):
4 position = target.position
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 5/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Finally, in the inspector make sure to enable Current so that the camera will be the
one we look through.
Now if we press play, you’ll see that the camera follows the player.
Creating an Enemy
Let’s now create an enemy scene. These will chase after the player and attack them
when in range.
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 6/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
1. Create a new scene with a root node of KinematicBody2D
2. Rename it to Enemy and save the scene
3. Drag in the player_s_0.png sprite to create a sprite node
4. Set the Modulate to red
5. Create a CollisionShape2D node with a capsule shape and resize it
6. Create a Timer node – this will be used for checking for when we can attack
Scripting the Enemy
On the enemy node, create a new script called Enemy. We can start with the variables.
1 var curHp : int = 5
2 var maxHp : int = 5
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 7/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
3
4 var moveSpeed : int = 150
5 var xpToGive : int = 30
6
7 var damage : int = 1
8 var attackRate : float = 1.0
9 var attackDist : int = 80
10 var chaseDist : int = 400
11
12 onready var timer = $Timer
13 onready var target = get_node("/root/MainScene/Player")
Inside of the _physics_process function (gets called 60 times per second) we want to
move towards the target if we’re further than the attack distance but closer than the
chase distance.
1 func _physics_process (delta):
2
3 var dist = position.distance_to(target.position)
4
5 if dist > attackDist and dist < chaseDist:
6 var vel = (target.position - position).normalized()
7
8 move_and_slide(vel * moveSpeed)
Back in the MainScene let’s drag in the Enemy scene to create a new instance. Now
we can press play and test it out. The enemy should move towards us.
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 8/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Now that the enemy moves towards us, let’s have it attack us.
1. Go to the Enemy scene
2. Select the Timer node
3. In the Inspector panel, click on the Node tab
4. Double click on the timeout() signal
5. Click Connect
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 9/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
This will create the _on_Timer_timeout function. Here, we want to check if we’re
within the attack distance and then attack the target. We’ll be creating
the take_damage function on the player soon.
1 func _on_Timer_timeout ():
2
3 if position.distance_to(target.position) <= attackDist:
4 target.take_damage(damage)
In the _ready function which gets called once the node is initialized, let’s setup the
timer node.
1 func _ready ():
2
3 timer.wait_time = attackRate
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 10/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
4 timer.start()
Finally for the enemy, let’s create the take_damage and die functions.
The target.give_xp function will be called over on the player script later on.
1 func take_damage (dmgToTake):
2
3 curHp -= dmgToTake
4
5 if curHp <= 0:
6 die()
7
8 func die ():
9
10 target.give_xp(xpToGive)
11 queue_free()
Player Functions
Now we want to go over to the Player script and add in some new functions to work
with the enemies. Let’s start with the give_gold function which will add gold to the
player.
1 func give_gold (amount):
2
3 gold += amount
For the levelling system, let’s create the give_xp and level_up functions.
1 func give_xp (amount):
2
3 curXp += amount
4
5 if curXp >= xpToNextLevel:
6 level_up()
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 11/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
7
8 func level_up ():
9
10 var overflowXp = curXp - xpToNextLevel
11
12 xpToNextLevel *= xpToLevelIncreaseRate
13 curXp = overflowXp
14 curLevel += 1
The take_damage and die functions will be called when an enemy attacks us.
1 func take_damage (dmgToTake):
2
3 curHp -= dmgToTake
4
5 if curHp <= 0:
6 die()
7
8 func die ():
9
10 get_tree().reload_current_scene()
Now if we press play, you’ll see that the enemy can attack and kill us – which will
reload the scene.
Player Interaction
With our player, we want the ability to interact with chests and enemies. In the Player
script, let’s create the _process function (called every frame) and check for when we’re
pressing the interact button.
1 func _process (delta):
2
3 if Input.is_action_just_pressed("interact"):
4 try_interact()
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 12/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
The try_interact function will check to see if the raycast is hitting anything. If it’s an
enemy, damage them but if it’s not – call the on_interact function if they have it.
1 func try_interact ():
2
3 rayCast.cast_to = facingDir * interactDist
4
5 if rayCast.is_colliding():
6 if rayCast.get_collider() is KinematicBody2D:
7 rayCast.get_collider().take_damage(damage)
8 elif rayCast.get_collider().has_method("on_interact"):
9 rayCast.get_collider().on_interact(self)
Chest Interactable
Now we need something to interact with. This is going to be a chest which will give the
player some gold. Create a new scene with a root node of Area2D.
1. Rename the node to Chest
2. Save the node
3. Drag in the rpgTile163.png to create a Sprite node
4. Create a CollisionShape2D node
5. Set the shape to a Rectangle and resize it to t the sprite
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 13/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Next, create a new script called Chest attached to the Area2D node. All we’re going to
do here, is have the on_interact function which gives the player gold then destroys
the node.
1 export var goldToGive : int = 5
2
3 func on_interact (player):
4
5 player.give_gold(goldToGive)
6 queue_free()
You might notice that interacting with the chest wont work but attacking the enemy
does work. So in the Player scene, select the ray cast node and enable Collide With
Areas.
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 14/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Creating the UI
Create a new scene with a root node of Control.
1. Rename the node to UI
2. Save the scene
3. Create a new TextureRect node and call it BG
4. Set the Texure to UI_Square.png
5. Enable Expand
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 15/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
6. Drag the 4 anchor points down to the bottom center
7. Set the Position to 386, 520
8. Set the Size to 250, 60
9. Set the Visibility > Self Modulate to dark grey
As a child of this BG node, we want to create three new nodes.
LevelBG – TextureRect
HealthBar – TextureProgress
XpBar – TextureProgress
For the TextureProgress nodes…
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 16/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
1. Enable Nine Patch Stretch
2. Set the Under and Progress textures to UI_Square.png
3. Set the Under tint to dark grey
4. Set the Progress tint to red or green
Now we need to setup our fonts. With the assets we imported into the project, we
have two font les. Right now, these are just .ttf les which need to be converted in a
format that Godot can read. For each font le…
1. Right click it and select New Resource…
2. Select the DynamicFont resource
3. Save this to the Font folder
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 17/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
4. Double click on the new resource and drag the .ttf le into the Font > Font
Data property
As a child of the LevelBG node, create a new Label node and rename it to LevelText.
Resize the rect box to t the level BG square
Set Align and Valign to Center
Drag the bold dynamic font resource into the Custom Fonts property
Set the font Size to 35
Set the Visibility > Self Modulate to grey
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 18/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Create a new Label node called GoldText.
1. Move and resize it to look like below
2. Set Align to Center
3. Set the Custom Font to the regular dynamic font resource
Scripting the UI
Next up, let’s create a new script attached to the UI node, called UI. We’ll start with the
variables to reference the children nodes.
1 onready var levelText : Label = get_node("BG/LevelBG/LevelText")
2 onready var healthBar : TextureProgress = get_node("BG/HealthBar")
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 19/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
3 onready var xpBar : TextureProgress = get_node("BG/XpBar")
4 onready var goldText : Label = get_node("BG/GoldText")
Then we have our functions which update each of these nodes.
1 # updates the level text Label node
2 func update_level_text (level):
3
4 levelText.text = str(level)
5
6 # updates the health bar TextureProgress value
7 func update_health_bar (curHp, maxHp):
8
9 healthBar.value = (100 / maxHp) * curHp
10
11 # updates the xp bar TextureProgress value
12 func update_xp_bar (curXp, xpToNextLevel):
13
14 xpBar.value = (100 / xpToNextLevel) * curXp
15
16 # updates the gold text Label node
17 func update_gold_text (gold):
18
19 goldText.text = "Gold: " + str(gold)
Now that we have the UI script, let’s go over to the MainScene and drag the UI scene
in. To make it render to the screen, we need to create a CanvasLayer node and make
UI a child of it.
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 20/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
In order to be able to access the UI script for the player, we need to move the canvas
layer up to the top of the hierarchy.
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 21/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Over in the Player script, let’s create a variable to reference the UI script.
1 onready var ui = get_node("/root/MainScene/CanvasLayer/UI")
Create the _ready function which gets called once the node is initialized. In there, we’ll
be initializing the UI.
1 func _ready ():
2
3 ui.update_level_text(curLevel)
4 ui.update_health_bar(curHp, maxHp)
5 ui.update_xp_bar(curXp, xpToNextLevel)
6 ui.update_gold_text(gold)
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 22/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Now we need to go through a few functions and call the UI functions at the end of
them. They go as following:
1 # give_gold function
2 ui.update_gold_text(gold)
3
4 # give_xp function
5 ui.update_xp_bar(curXp, xpToNextLevel)
6
7 # level_up function
8 ui.update_level_text(curLevel)
9 ui.update_xp_bar(curXp, xpToNextLevel)
10
11 # take_damage function
12 ui.update_health_bar(curHp, maxHp)
We can now press play and see that the UI is set at the start of the game and changes
as we get gold, take damage, get xp and level up.
With all of this, we’re nished with our RPG game in Godot. Let’s now go back to
the MainScene and place some enemies and chests around the place.
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 23/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Conclusion
Congratulations on completing the tutorial! Through perseverance, hard work, and a
little bit of know-how, we just created a 2D RPG in Godot with a number of di erent
features. Over the course of Part 1 and Part 2, we’ve shown you how to set up:
A top-down 2D player controller
Enemies who can attack and chase the player
Chests we can interact with to get gold
A UI to show our health, XP, level, and gold
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 24/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
And with that, you should now possess the fundamental skills to create even more
RPGs in the future! Of course, you can expand upon the game created here – adding
in new features, eshing out current ones, or even just improving certain aspects you
felt were lacking! There are endless directions to go from here, but with this solid
foundation, you’ll have quite the headstart!
Thank you very much for following along, and I wish you the best of luck with your
future games.
Related Posts
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 25/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
Free eBook – Godot Game Development for Create a Top-Down 2D Character How to Create an RPG in Godot – Part 1
Beginners
2D Tutorials
rpg tutorial
How to Create an RPG in Godot – Part 1
Create an Action RPG in Godot – Part 1
Mini-Degrees™ Zenva
Augmented Reality Jobs
EdTech A liates
Full-Stack Web Development Catalog
HTML5 Game Development Free Courses
Machine Learning Success Stories
MMORPGs Bulk Purchases
Python Help Center
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 26/27
24/11/2020 How to Create an RPG in Godot – Part 2 – Godot Tutorials
RPGs Terms and Conditions
Unity Privacy Policy
Unreal Engine Zenva app
Virtual Reality
Our Network Zenva Pty Ltd
Android Kennel Level 3 155 Queen Street
Codemurai Brisbane, 4000, QLD
Australia
Coding Degree
ABN 83 606 402 199
De Idea a App
GameDev Academy
Godot Tutorials
HTML5 Hive
Phaser Tutorials
Python Machine Learning
Swift Ludus
VR Game Development
© 2020
https://godottutorials.pro/rpg-godot-tutorial-part-2/ 27/27