All lessons
Lesson 07

Enemy Patrols

An enemy that walks a fixed route, switching to the next spot on its list each time it arrives.

Loading the game engine. This takes a moment the first time.

Build style

The enemies have no code in this file. Everything they do lives in patrol_enemy.gd. This file just holds the scene together.

HOW PATROLLING WORKS

The enemy keeps a numbered list of spots. It walks toward spot 0, and once it gets close it switches to spot 1, then spot 2, then back to 0. Wrapping back around to 0 is what makes it loop forever.

The scenes

UI node · 2D node · % unique name · .tscn instanced scene

enemy_patrols.tscn
  • EnemyPatrols Node2D enemy_patrols.gd
  • PatrolPlayer CharacterBody2D player.tscn
  • PatrolEnemy1 CharacterBody2D patrol_enemy.tscn
  • PatrolEnemy2 CharacterBody2D patrol_enemy.tscn
  • PatrolEnemy3 CharacterBody2D patrol_enemy.tscn
patrol_enemy.tscn
  • PatrolEnemy CharacterBody2D patrol_enemy.gd
  • ColorRect
  • CollisionShape2D
player.tscn
  • PatrolPlayer CharacterBody2D player.gd
  • Sprite2D
  • CollisionShape2D
Build style
extends Node2D