Lesson 7
Globals, Enums, Player State
Download the week 6 project
Globals
There are many circumstances where every node in your scene should reference the same property or object, such as getting access to the player, the global game speed, or the global settings (sound levels, difficulty, etc). To do this, Godot lets you define Globals in the Project Settings dialog.
Globals
...
...
...
...


Homework
Create a Wave
Create a new scene with a Node2D root, save it as Wave1 (since you will be making several waves)
You might want to copy the background and player from the main scene to use them as references for placing obstacles and collectables. Just be sure to remove them before saving the scene!
Add obstacles and collectables in any pattern you want.
Add a VisibleOnScreenNotifier2D child that covers everything on the screen (all obstacles and collectables).
Add a script to the root node and name it Wave.gd.
Add a signal named finished.
In the ready() function, connect the VisibileOnSceenNotifier2D's screen_exited signal to your finished signal.
(OPTIONAL) Add a Difficulty property to your script so you can define different waves as Easy, Normal, or Hard.
Create multiple waves with different patterns of collectables and obstacles.
Create Wave Spawner
Create a new scene with a Node2D root, save it as WaveSpawner.
Add a Marker2D child node and rename it SpawnMarker
Position it just off screen to the right, but make sure the y-coordinate is 0!
Add a Timer child node and rename it SpawnTimer.
Check the checkbox to make it a One Shot timer, meaning it doesn't continue running after it times out.
Add a script to the root node, name it WaveSpawner.gd.
As always, you can download this script from the class resources folder, but try to write it yourself first.
Add export vars to the script for spawnTimer (time until a new wave is spawned), spawnTimerVariation (adds variation to the spawn timer so it's not always the same), and waves (an array that will hold all of the Wave scenes you can spawn).
Add local (not export) var for currentWave (the current wave).
Add the following functions:
pick_wave(): Pick a random scene from the waves array. If you want, you can add additional logic to only pick waves based on the current difficulty.
spawn_wave(): Calls pick_wave() and creates a new instance of the selected scene at the Marker2D's position. Connect the wave's finished signal to your despawn_wave() function.
despawn_wave(): Destroys the current wave and starts the timer until the next wave is spawned.
move_wave(delta): Moves the current wave based on the game's (player's) speed. Be sure to check if there IS a current wave before moving it!
_on_spawn_timer_timeout(): Connect to the SpawnTimer's timeout signal, use it to call spawn_wave() when the timer times out.
In your _process() function:
Call move_wave(delta) to move the current wave.
