Building a Character In Godot

Posted by Zach on September 09, 2020

Building a Character In Godot

Games built with Godot can be broken down conceptually as such:

  • A Node is the basic building block of Godot
  • We use Nodes to build Scenes, each Scene is a Node itself (the root node of the scene), and everything in the scene is a child of that node.
  • We compose Scenes together to build a Game.

It's really intuitive, a scene can be built on its own, and then imported into another scene as a child node which allows for all of the complexity and depth that you need to make a game, but also makes it really approachable and easy to express the intent of your games design through composition.

So, today I'm going to be working on the main character Scene for Timmy. I'm going to throw down a root node in my Godot window and get started.

There are node types for everything you can think of, and you can make your own custom nodes too, since I'm making a 2D game I'm mostly only concerned with the 2D node types, and in this case I want to use the KinematicBody2D node because it has a lot of features that we'll need to make 2D characters interact with the game world.

Another cool thing about nodes is that they are basically a declarative way of coding, you drop a node into the tree, and you can load it in your code, and access any property on the node.

Nodes can have scripts attached to them that define behavior, and messages can be passed between nodes using Signals which are basically like Events, nodes can subscribe to each others signals to be made aware of things that are happening in different parts of the scene. Example, when Timmy is low health we can fire a Signal that the overall game world can respond to in different ways.

I'm also going to need to add some child nodes to Timmy to represent his different functions.

  • CollisionShape2D for determining when we're colliding with things
  • StateMachine
  • AnimationPlayer
  • Pivot point for his body
  • Base sprite
  • Label for displaying the current state
  • Tween node for simulating a z axis during jumping

Next time I'll dig into the scripts that get attached to the different nodes to make the player do things.