This hereās a machine-translated text that might have some errors in it!
Where does this information come from?
A whole heap of this is just a retelling of Godotās official documentation. Down below youāll find some useful links to the official documentation (which I reckon you oughta use over this here on Piggy, but if you prefer this one, go right ahead!)
Useful Resources:
What about graphics, do I gotta make āem myself?
Nope! Yāall can find a heap oā free stuff right here:
Other Useful Links:
Other game engines?
If you prefer, you can use other game engines. Examples of other game engines are:
There are many others too, feel free to search if you want to try something different!
What in Tarnation is Godot?
Godot (Pronounced Guh-doh), is a āGame Engineā. To put it plain and simple, itās a program that lets ya make games (or regular programs if ya feel like it). Godot can do most anything other game engines can do, both 2D and 3D.
Examples of games made in Godot: Godot Showcase
It aināt just Indie games thatāve been made in Godot, Sonic Colors Ultimate was made with Godot! š¦š¦š¦
How Do Ya Get Yer Hands on Godot?
Ya can either download Godot from:
- The official site: Godot Official Page.
- Or on Steam: Godot Steam.
Just download āer and install (or run āer on Steam).
Set Up a Project!
When you start Godot, you get this window:
Here youāre asked about programming language, GDScript or C#. GDScript is mighty similar to Python.
Differences between GDScript and Python (without colors):
def hello():
text = "Hello world!"
print(text)
func hello():
var text = "Hello world!"
print(text)
For variables in Python, you just gotta write the name of the variable. In GDScript, you gotta write var
first. To make functions, you write func
instead of def
.
Scenes & Nodes
One oā the most important concepts within Godot is Scenes and Nodes. We can start with nodes. A node is an object in Godot, and it can represent just ābout anything. It can be somethinā representinā a player, an enemy, a button in a menu, text on the screen, all sorts oā things. Scenes are a collection oā nodes.
Here weāre gonna make a real simple example.
Part 1a - Set Up the āplayerā Scene
At the top of the Godot window, press the ā2Dā button to change the view to a 2D view.
On the left side of the window, youāll see the following interface:
Press the āOther Nodeā button, and search for āCharacterBody2Dā, select it and press āCreateā. This hereās a node used for a 2D player. You might notice a warninā triangle ā ļø next to the āCharacterBody2Dā node. Thatās ācause itās missinā a few things itād like to have.
If you right-click on the node thereās a ā+ Add Child Nodeā¦ā button. Use that and add two nodes, Sprite2D and CollisionShape2D. Sprite2D is used to add some graphics to the player, while the other oneās used to check for collisions. You can also give āem names if it makes it easier to keep track of things. Iāve given my CharacterBody2D the name āPlayerā. Your scene should look like this now:
Part 1b - Fixinā ā ļø on CollisionShape2D
The warninā triangle in this here case is that the CollisionShape2D is missinā actual collision. You can fix this by clickinā on the node (on the left), then a panelāll pop up on the right side. Here you get a whole mess oā information ābout the node you can change. Feel free to mess around with whatās there. But the one we wanna focus on is āShapeā, set this to somethinā like RectangleShape2D. It aināt all that important, we aināt gonna be usinā the collisions here.
Part 1c - Addinā a Sprite, Graphics
If ya click on Sprite2D on the left, itāll bring up the fields on the right, where it says āTextureā. Here ya can put in a picture for the player. Just drag a picture in and drop it in the field.
Hereās what itāll look like after addinā a sprite.
Part 2 - Addinā Input, Controls
At the top oā the window, thereās a menu, āScene - Project - Debug - Editor - Helpā. Press on āProjectā and then āProject Settingsā. Here ya get a menu with a whole heap oā settings. Press on āInput Mapā. Here ya can set up buttons on the keyboard.
- In the āAdd New Actionā field, write āleftā then press āAddā.
- Then add ārightā, āupā and ādownā.
- These here are known as āActionsā
- On each āActionā ya can add buttons by pressinā the + button to the right.
- Here ya just gotta press on the keyboard, then press Add.
- Add buttons on all actions.
Part 3 - Addinā a Script to Control the Player.
To be able to add game logic, meaninā do somethinā with the player, the background, or whatever it might be in the game, we need a script. Scripts are code, and can be written in two languages, GDScript or C#, GDScript is the default.
- Press on the āCharacterBody2Dā node (I done called it āPlayerā).
- Press on the āAttach Scriptā button (see below)
- Here ya get a window where ya can choose a language (choose GDScript) and a path (just leave it as it is, but ya can give it a name), then press on further. Here ya get a āScriptā window. Aināt much here to start with.
extends CharacterBody2D
# Dette er hovedskriptet for spilleren.
# This here's the main script fer the player.
# Her defineres spillerens bevegelse og handlinger.
# Here's where we define how the player moves 'n' what they do.
var speed = 200
# Hastighet spilleren beveger seg med.
# How fast the player moves, ya see.
func _physics_process(delta):
# Beveg spilleren basert pƄ input.
# Move the player 'round based on what ya press.
var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
velocity = direction * speed
move_and_slide()
The only thang this here codeās sayinā right now is that the codeās gotta belong to a node of the āCharacterBody2Dā type, which is our player.
Weāre gonna add a function here that weāre gonna use to wrangle the player:
extends CharacterBody2D
# Dette er en tom funksjon.
# This here is a empty function.
func _physics_process(delta: float) -> void:
return
Physics Process?
_physics_process
is a function that gets updated every single āframeā, meaninā āround 60 times a second (default). Thereās another function just called _process
, which updates all the time. If ya want yer player movinā at a steady pace, ya use _physics_process
.
Right hereās where weāre gonna put the code that moves the player āround.
Part 4 - Basic Input
In Part 2, ya added input buttons, now weāre gonna use āem. Thereās a built-in object called Input
that we can use to check if the playerās pressed what weāve set up in the āInput Mapā.
Try addinā this here code into yer _physics_process
code:
if Input.is_action_pressed('right'):
velocity.x = 100
# Flytt og skli.
# Move 'n slide.
move_and_slide()
What is move_and_slide()
?
move_and_slide()
is a built-in function in Godot thatās used when we actually want to move whatever weāre applyinā it to. Without this here function, the player aināt gonna be movinā a muscle.
Warning
The spacing on the lines is mighty important, this here is the same as in Python.
What happens when ya start the program by pressinā the play button in the Godot window, then pressinā ārightā?
If nothinā happens now:
- Did ya remember to set somethinā up in the āInput Mapā?
- Did ya write
right
and notRight
? Meaninā, is what ya wrote in the code the same as the name in the input map?
All the code so far
extends CharacterBody2D
func _physics_process(delta: float) -> void:
if Input.is_action_pressed('right'):
velocity.x = 100
move_and_slide()
Try now to add code for 'left'
, 'up'
, 'down'
.
What must velocity.x
be for left
? What about up
and down
?
Hele koden nƄ
extends CharacterBody2D
func _physics_process(delta: float) -> void:
if Input.is_action_pressed('right'):
velocity.x = 100
if Input.is_action_pressed('left'):
velocity.x = -100
if Input.is_action_pressed('down'):
velocity.y = 100
if Input.is_action_pressed('up'):
velocity.y = -100
move_and_slide()
Part 5 - Fixinā the Code
Now, ya might notice the player donāt stop when ya let go of a direction. We can fix that right now!
Before all them if
statements, add a line that sets velocity to 0. Ya can do that by writinā velocity = Vector2()
All speeds and directions in Godot are vectors, this hereās a math concept we aināt gonna get into right now, but if yaāre wonderinā more ābout what that means, ya can mosey on over here: Wikipedia vectors.
The whole code now
extends CharacterBody2D
func _physics_process(delta: float) -> void:
velovity = Vector2()
if Input.is_action_pressed('right'):
velocity.x = 100
if Input.is_action_pressed('left'):
velocity.x = -100
if Input.is_action_pressed('down'):
velocity.y = 100
if Input.is_action_pressed('up'):
velocity.y = -100
move_and_slide()
When ya start the game now, ya can move the player around:
Further, yaāll can fix the code to make the speed not just be a number, but can be stored somewhere else.
Ya can, fer instance (before the function) add a constant that keeps track of the speed.
Hele koden til slutt med const
extends CharacterBody2D
const SPEED = 100
func _physics_process(delta: float) -> void:
velovity = Vector2()
if Input.is_action_pressed('right'):
velocity.x = SPEED
if Input.is_action_pressed('left'):
velocity.x = -SPEED
if Input.is_action_pressed('down'):
velocity.y = SPEED
if Input.is_action_pressed('up'):
velocity.y = -SPEED
move_and_slide()
Part 6 - Go On and Play Around!
If yāall head back to the first part, Useful Resources, you can find what you can continue to play with.
After this, you can try makinā yer own game. What you build is up to you! If you wanna make somethinā brand new, go right ahead! If you wanna try copyinā a game that already exists, do that too! Best way to learn is by tryinā!