Godot: 2D development tips & tricks

I’ve been digging deep into game development with Godot to find home to the mountain of pixel art I’ve been making. However, since every time I need to find a code snippet or a little guide I have to go on a scavenger hunt through my server and drives – I decided it’s time to just use my website as a development resource. You’ll be able to find things that I find useful for working in this absolutely amazing development game development engine relating to the pixelart 2D workflows.


Basic parallax background setup

(OP Michelle M. – found on HeartBeast’s YouTube channel)

  1. Create a ParallaxBackground node. (it doesn’t need to be a child of the Camera2D node, but making it one won’t break it either)
  2. Create a ParallaxLayer node as a child of the ParallaxBackground
  3. Add your sprite as a child of the ParallaxLayer node and make sure Centered is off
  4. Go back to the ParallaxLayer node
  5. Under Motion, if you want to tile your BG – set Mirroring to the size of your sprite (in the case at hand the sprite was 640×640)
  6. Under Motion, fiddle with the Scale setting. This is how fast that ParallaxLayer will move in relation to the camera. OP found (0.1, 0.1) to be a good starting point for something like a sky.
  7. Run your game and you should have a background that infinitely loops and also has a parallax effect

Play animation when you run the game rather than in the editor

extends Node2D

onready var AnimatedSprite = $AnimatedSprite

func _ready():
    AnimatedSprite.play("Animate")

Basic scene transition with a fade

This one requires a little bit of an explanation.

  1. We are exporting the next_scene_path and player_spawn_location in order to be able to access them from the scene instance (you did make this into a scene in the first place right?)
  2. The animation on _ready() is a fade-in when we enter the new scene – called when the function itself is called – best with a “when body enters signal” or however you like
  3. The _get_configuration_warning() is just a spot check for typos essentially.
  4. When the signal is triggered (_on_SceneTransition_body_entered(body)) in my case we among actually changing the scene also play the animation, this time in the right direction, not backwards like in the _ready()
tool

extends Area2D

export(String, FILE) var next_scene_path = ""
export(Vector2) var player_spawn_location = Vector2.ZERO

# Reference to the _AnimationPlayer_ node
onready var animation_player := $AnimationPlayer

func _ready() -> void:
	# Plays the animation backward to fade in
	animation_player.play_backwards("fade")
	yield(animation_player, "animation_finished")

func _get_configuration_warning() -> String:
	if next_scene_path == "":
		return "next scene path must be set for transition to work"
	else:
		return ""

func _on_SceneTransition_body_entered(body):
	Global.player_initial_map_position = player_spawn_location
	if get_tree().change_scene(next_scene_path) != OK:
		# error handling
		print ("Error! Unavailable scene!")
	animation_player.play("fade")
	yield(animation_player, "animation_finished")

Door open/close setup

I’m using Area2D for this.

The idea is simple, you make a collision area and when your player collides with it – you play the “door_open” animation. When he clears the area, you play the “door_close” animation.

The more advanced method would require a “force field” setup, essentially disallowing the player to go through the door if it’s not finished opening. This I will have to do for the airlock doors (coming soon).

extends Area2D

onready var animation_player = $AnimationPlayer

func _ready():
	animation_player.play("door_idle")
	animation_player.stop()

func _on_area2d_door_up_body_entered(body):
	animation_player.play("door_open")

func _on_area2d_door_up_body_exited(body):
	animation_player.play("door_close")

Indie game development & digital art by Aleksandar Djordjevic