beanz Magazine

Godot 3

COM SALUD Agencia de comunicación on Flickr

It’s free, open source, and comes with a snazzy graphics and physics engine for both 2D and 3D games.

In the past, we’ve talked about how to make games with different frameworks: Scratch, WoofJS, and PyGame. Today we’ll talk about an up-and-coming system for making games: Godot.

Godot is a lot like Unity, which you may have heard of before: a system that’s becoming popular with developers because it lets them code both 2D and 3D games quickly without having to custom write a graphics and physics engine. Unlike Unity, Godot is free, open source, and runs on Linux. Those are all big wins in my book!

To get started, you can find the Godot engine at https://godotengine.org/download. It’s available pre-compiled for all three of the major operating systems: Windows, OS X, and Linux.

The source code is also available for download on GitHub. You probably won’t have a reason to download the source code if you’re just getting started, but if you find that you really enjoy coding in Godot and have ideas for features then you could take a crack at adding them.

Now, once you have Godot installed you’ll see something like this, though with fewer things listed:

If you click on the Templates tab, you should see a number of examples that people have created that you can use as a starting point. This is going to be a great place to learn once you get the basics down. The Godot site also has a really comprehensive first-game tutorial.

We’re going to do something even smaller than their first tutorial just to give you a feel for what Godot is about, taking some inspiration from HeartBeast’s video tutorials linked below.

First, open Godot if you haven’t already. Select New Project, give it a name, and make a folder for the project to live in. You should then be looking at a screen like:

Next, click on the 2D selector to tell Godot you want to make a two-dimensional scene. We’re starting with two-dimensional just because it’s simpler and involves fewer concepts to get started.

Now you’ll be looking at a blank screen. On it we’re going to create our first node. Nodes in Godot are like the basic building blocks that you connect together to make things in the game. Click on the + symbol under the tab on the right hand side that says “Scene”. Search for “kinematic” and choose the node type of KinematicBody2D.

In Godot’s parlance, a “kinematic body” is an entity that can move and interact with other objects but that isn’t subject to all the rules of Godot’s built in physics engine, rather you have to manually write the code for its movement. You’ll see a little square orange outline but nothing else.

This happens because the kinematic body doesn’t have anything visible inside it. We need to do two things to make it solid and visible: we’ll give the kinematic body a sprite sub-node and a collision shape so that it’ll be solid. If you’re coming from scratch this use of “sprite” might seem weird. In Scratch, a sprite is the whole bundle of properties and code that makes up an object in the game. Here, a sprite is just the visual component: more like a “costume” in Scratch’s language.

Now, if you right click on the kinematic body node in the node-list you can add a child node of Sprite. Still nothing appears yet! Now we need to give a texture to the sprite. Click on the sprite node in the Scene tab. Then, if you look in the inspector tab in the lower-right part of the screen you should see an entry called texture.

You can add an image to the resources for the project by dragging an image file into the left-hand panel under the icon.png file. You should choose an image just like you would in scratch: you want a transparent image and pngs are generally a good choice. When you have the image added to your resources you can drag the image from the resources list into the texture field in the inspector.

I chose my favourite marsupial:

Already we can make our character move! Go ahead and right-click on the kinematic body node in the scene tab and choose the Attach Script option then just click create on the pop-up that appears. Replace the code you see in the editor with the following:

extends KinematicBody2D
 
var motion = Vector2()
 
func _physics_process(delta):
	if Input.is_action_pressed("ui_right"):
		motion.x += 1
	if Input.is_action_pressed("ui_left"):
		motion.x -= 1
	if Input.is_action_pressed("ui_up"):
		motion.y -= 1
	if Input.is_action_pressed("ui_down"):
		motion.y += 1
	move_and_slide(motion)

Now! If you hit the play button in the upper right your code should run: you’ll have the picture you loaded moving in the direction you press with the arrow keys.

There’s one last thing I want you to try before we finish this brief introduction: go to the Scene menu in the left hand corner and select New Scene. Now make a generic Node, right-click on it, then select Instance child scene. From here, you’ll select the kinematic body you saved earlier. This is the “child scene” that we’re inserting into our new, empty, scene.

It might seem weird to call this a “scene”. In Godot, a scene is basically a collection of nodes and code that you can re-use later. For good measure, add another instance of the scene you already made and use your mouse to drag it somewhere else on the screen. If you hit the play scene button, the one with the clapperboard like on a movie set, you should see a couple of instances of your image moving around when you hit the arrow keys.

The way you can nest scenes inside each other is one of the things I actually really like about Godot: it makes it easy to create individual building blocks of game that you can stick together just like you would with the built in node types.

If this seems interesting, then you should definitely check out Godot and try making some games!

Learn More

Godot homepage

https://godotengine.org/

Godot first game tutorial

http://docs.godotengine.org/en/3.0/getting_started/step_by_step/your_first_game.html

Heartbeast’s platformer tutorial

https://www.youtube.com/watch?v=wETY5_9kFtA

Also In The June 2018 Issue

Code up your digital turtle mascot and watch him dash around the screen in this simple Python coding activity.

A phone and tablet app exposes the invisible waves that connect our computers.

How AI technology is helping fans keep the magic alive for one more chapter.

Use Scratch to become the architect of your very own digital metropolis.

Use SketchUp to create dizzying patterns and shapes, Escher-style.

Whiz around your computer’s folders and modify files at lightning speed like a pro.

Use micro:bit and cardboard to create a Jedi knight that sounds the alarm when evil approaches!

Learn about the infamous Enigma machine and how its “unbreakable” code was finally defeated.

Take your 3D-printed gizmos to the next level with harder, sleeker, and stronger material.

How daily coding puzzles with constant feedback can be a useful tool to help students master text-based languages.

Scientists draw inspiration from nature to create remarkable specialized robots.

Links from the bottom of all the June 2018 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for June 2018.