The main game scene

Now it’s time to bring everything we did together into a playable game scene.

Create a new scene and add a Node named Main. (The reason we are using Node instead of Node2D is because this node will be a container for handling game logic. It does not require 2D functionality itself.)

Click the Instance button (represented by a chain link icon) and select your saved Player.tscn.

../../_images/instance_scene.png

Now, add the following nodes as children of Main, and name them as shown (values are in seconds):

  • Timer (named MobTimer) - to control how often mobs spawn

  • Timer (named ScoreTimer) - to increment the score every second

  • Timer (named StartTimer) - to give a delay before starting

  • Position2D (named StartPosition) - to indicate the player’s start position

Set the Wait Time property of each of the Timer nodes as follows:

  • MobTimer: 0.5

  • ScoreTimer: 1

  • StartTimer: 2

In addition, set the One Shot property of StartTimer to “On” and set Position of the StartPosition node to (240, 450).

Spawning mobs

The Main node will be spawning new mobs, and we want them to appear at a random location on the edge of the screen. Add a Path2D node named MobPath as a child of Main. When you select Path2D, you will see some new buttons at the top of the editor:

../../_images/path2d_buttons.png

Select the middle one (“Add Point”) and draw the path by clicking to add the points at the corners shown. To have the points snap to the grid, make sure “Use Grid Snap” and “Use Snap” are both selected. These options can be found to the left of the “Lock” button, appearing as a magnet next to some dots and intersecting lines, respectively.

../../_images/grid_snap_button.png

Important

Draw the path in clockwise order, or your mobs will spawn pointing outwards instead of inwards!

../../_images/draw_path2d.gif

After placing point 4 in the image, click the “Close Curve” button and your curve will be complete.

Now that the path is defined, add a PathFollow2D node as a child of MobPath and name it MobSpawnLocation. This node will automatically rotate and follow the path as it moves, so we can use it to select a random position and direction along the path.

Your scene should look like this:

../../_images/main_scene_nodes.png

Main script

Add a script to Main. At the top of the script, we use export (PackedScene) to allow us to choose the Mob scene we want to instance.

GDScript   C#C++

  1. extends Node
  2. export(PackedScene) var mob_scene
  3. var score
  1. public class Main : Node
  2. {
  3. // Don't forget to rebuild the project so the editor knows about the new export variable.
  4. #pragma warning disable 649
  5. // We assign this in the editor, so we don't need the warning about not being assigned.
  6. [Export]
  7. public PackedScene MobScene;
  8. #pragma warning restore 649
  9. public int Score;
  10. }
  1. // Copy `player.gdns` to `main.gdns` and replace `Player` with `Main`.
  2. // Attach the `main.gdns` file to the Main node.
  3. // Create two files `main.cpp` and `main.hpp` next to `entry.cpp` in `src`.
  4. // This code goes in `main.hpp`. We also define the methods we'll be using here.
  5. #ifndef MAIN_H
  6. #define MAIN_H
  7. #include <AudioStreamPlayer.hpp>
  8. #include <CanvasLayer.hpp>
  9. #include <Godot.hpp>
  10. #include <Node.hpp>
  11. #include <PackedScene.hpp>
  12. #include <PathFollow2D.hpp>
  13. #include <RandomNumberGenerator.hpp>
  14. #include <Timer.hpp>
  15. #include "hud.hpp"
  16. #include "player.hpp"
  17. class Main : public godot::Node {
  18. GODOT_CLASS(Main, godot::Node)
  19. int score;
  20. HUD *_hud;
  21. Player *_player;
  22. godot::Node2D *_start_position;
  23. godot::PathFollow2D *_mob_spawn_location;
  24. godot::Timer *_mob_timer;
  25. godot::Timer *_score_timer;
  26. godot::Timer *_start_timer;
  27. godot::AudioStreamPlayer *_music;
  28. godot::AudioStreamPlayer *_death_sound;
  29. godot::Ref<godot::RandomNumberGenerator> _random;
  30. public:
  31. godot::Ref<godot::PackedScene> mob_scene;
  32. void _init() {}
  33. void _ready();
  34. void game_over();
  35. void new_game();
  36. void _on_MobTimer_timeout();
  37. void _on_ScoreTimer_timeout();
  38. void _on_StartTimer_timeout();
  39. static void _register_methods();
  40. };
  41. #endif // MAIN_H

We also add a call to randomize() here so that the random number generator generates different random numbers each time the game is run:

GDScript   C#C++

  1. func _ready():
  2. randomize()
  1. public override void _Ready()
  2. {
  3. GD.Randomize();
  4. }
  1. // This code goes in `main.cpp`.
  2. #include "main.hpp"
  3. #include <SceneTree.hpp>
  4. #include "mob.hpp"
  5. void Main::_ready() {
  6. _hud = get_node<HUD>("HUD");
  7. _player = get_node<Player>("Player");
  8. _start_position = get_node<godot::Node2D>("StartPosition");
  9. _mob_spawn_location = get_node<godot::PathFollow2D>("MobPath/MobSpawnLocation");
  10. _mob_timer = get_node<godot::Timer>("MobTimer");
  11. _score_timer = get_node<godot::Timer>("ScoreTimer");
  12. _start_timer = get_node<godot::Timer>("StartTimer");
  13. // Uncomment these after adding the nodes in the "Sound effects" section of "Finishing up".
  14. //_music = get_node<godot::AudioStreamPlayer>("Music");
  15. //_death_sound = get_node<godot::AudioStreamPlayer>("DeathSound");
  16. _random = (godot::Ref<godot::RandomNumberGenerator>)godot::RandomNumberGenerator::_new();
  17. _random->randomize();
  18. }

Click the Main node and you will see the Mob Scene property in the Inspector under “Script Variables”.

You can assign this property’s value in two ways:

  • Drag Mob.tscn from the “FileSystem” dock and drop it in the Mob Scene property.

  • Click the down arrow next to “[empty]“ and choose “Load”. Select Mob.tscn.

Next, select the Player node in the Scene dock, and access the Node dock on the sidebar. Make sure to have the Signals tab selected in the Node dock.

You should see a list of the signals for the Player node. Find and double-click the hit signal in the list (or right-click it and select “Connect…”). This will open the signal connection dialog. We want to make a new function named game_over, which will handle what needs to happen when a game ends. Type “game_over” in the “Receiver Method” box at the bottom of the signal connection dialog and click “Connect”. Add the following code to the new function, as well as a new_game function that will set everything up for a new game:

GDScript   C#C++

  1. func game_over():
  2. $ScoreTimer.stop()
  3. $MobTimer.stop()
  4. func new_game():
  5. score = 0
  6. $Player.start($StartPosition.position)
  7. $StartTimer.start()
  1. public void GameOver()
  2. {
  3. GetNode<Timer>("MobTimer").Stop();
  4. GetNode<Timer>("ScoreTimer").Stop();
  5. }
  6. public void NewGame()
  7. {
  8. Score = 0;
  9. var player = GetNode<Player>("Player");
  10. var startPosition = GetNode<Position2D>("StartPosition");
  11. player.Start(startPosition.Position);
  12. GetNode<Timer>("StartTimer").Start();
  13. }
  1. // This code goes in `main.cpp`.
  2. void Main::game_over() {
  3. _score_timer->stop();
  4. _mob_timer->stop();
  5. }
  6. void Main::new_game() {
  7. score = 0;
  8. _player->start(_start_position->get_position());
  9. _start_timer->start();
  10. }

Now connect the timeout() signal of each of the Timer nodes (StartTimer, ScoreTimer , and MobTimer) to the main script. StartTimer will start the other two timers. ScoreTimer will increment the score by 1.

GDScript   C#C++

  1. func _on_ScoreTimer_timeout():
  2. score += 1
  3. func _on_StartTimer_timeout():
  4. $MobTimer.start()
  5. $ScoreTimer.start()
  1. public void OnScoreTimerTimeout()
  2. {
  3. Score++;
  4. }
  5. public void OnStartTimerTimeout()
  6. {
  7. GetNode<Timer>("MobTimer").Start();
  8. GetNode<Timer>("ScoreTimer").Start();
  9. }
  1. // This code goes in `main.cpp`.
  2. void Main::_on_ScoreTimer_timeout() {
  3. score += 1;
  4. }
  5. void Main::_on_StartTimer_timeout() {
  6. _mob_timer->start();
  7. _score_timer->start();
  8. }
  9. // Also add this to register all methods and the mob scene property.
  10. void Main::_register_methods() {
  11. godot::register_method("_ready", &Main::_ready);
  12. godot::register_method("game_over", &Main::game_over);
  13. godot::register_method("new_game", &Main::new_game);
  14. godot::register_method("_on_MobTimer_timeout", &Main::_on_MobTimer_timeout);
  15. godot::register_method("_on_ScoreTimer_timeout", &Main::_on_ScoreTimer_timeout);
  16. godot::register_method("_on_StartTimer_timeout", &Main::_on_StartTimer_timeout);
  17. godot::register_property("mob_scene", &Main::mob_scene, (godot::Ref<godot::PackedScene>)nullptr);
  18. }

In _on_MobTimer_timeout(), we will create a mob instance, pick a random starting location along the Path2D, and set the mob in motion. The PathFollow2D node will automatically rotate as it follows the path, so we will use that to select the mob’s direction as well as its position. When we spawn a mob, we’ll pick a random value between 150.0 and 250.0 for how fast each mob will move (it would be boring if they were all moving at the same speed).

Note that a new instance must be added to the scene using add_child().

GDScript   C#C++

  1. func _on_MobTimer_timeout():
  2. # Create a new instance of the Mob scene.
  3. var mob = mob_scene.instance()
  4. # Choose a random location on Path2D.
  5. var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
  6. mob_spawn_location.offset = randi()
  7. # Set the mob's direction perpendicular to the path direction.
  8. var direction = mob_spawn_location.rotation + PI / 2
  9. # Set the mob's position to a random location.
  10. mob.position = mob_spawn_location.position
  11. # Add some randomness to the direction.
  12. direction += rand_range(-PI / 4, PI / 4)
  13. mob.rotation = direction
  14. # Choose the velocity for the mob.
  15. var velocity = Vector2(rand_range(150.0, 250.0), 0.0)
  16. mob.linear_velocity = velocity.rotated(direction)
  17. # Spawn the mob by adding it to the Main scene.
  18. add_child(mob)
  1. public void OnMobTimerTimeout()
  2. {
  3. // Note: Normally it is best to use explicit types rather than the `var`
  4. // keyword. However, var is acceptable to use here because the types are
  5. // obviously Mob and PathFollow2D, since they appear later on the line.
  6. // Create a new instance of the Mob scene.
  7. var mob = (Mob)MobScene.Instance();
  8. // Choose a random location on Path2D.
  9. var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
  10. mobSpawnLocation.Offset = GD.Randi();
  11. // Set the mob's direction perpendicular to the path direction.
  12. float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
  13. // Set the mob's position to a random location.
  14. mob.Position = mobSpawnLocation.Position;
  15. // Add some randomness to the direction.
  16. direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
  17. mob.Rotation = direction;
  18. // Choose the velocity.
  19. var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
  20. mob.LinearVelocity = velocity.Rotated(direction);
  21. // Spawn the mob by adding it to the Main scene.
  22. AddChild(mob);
  23. }
  1. // This code goes in `main.cpp`.
  2. void Main::_on_MobTimer_timeout() {
  3. // Create a new instance of the Mob scene.
  4. godot::Node *mob = mob_scene->instance();
  5. // Choose a random location on Path2D.
  6. _mob_spawn_location->set_offset((real_t)_random->randi());
  7. // Set the mob's direction perpendicular to the path direction.
  8. real_t direction = _mob_spawn_location->get_rotation() + (real_t)Math_PI / 2;
  9. // Set the mob's position to a random location.
  10. mob->set("position", _mob_spawn_location->get_position());
  11. // Add some randomness to the direction.
  12. direction += _random->randf_range((real_t)-Math_PI / 4, (real_t)Math_PI / 4);
  13. mob->set("rotation", direction);
  14. // Choose the velocity for the mob.
  15. godot::Vector2 velocity = godot::Vector2(_random->randf_range(150.0, 250.0), 0.0);
  16. mob->set("linear_velocity", velocity.rotated(direction));
  17. // Spawn the mob by adding it to the Main scene.
  18. add_child(mob);
  19. }

Important

Why PI? In functions requiring angles, Godot uses radians, not degrees. Pi represents a half turn in radians, about 3.1415 (there is also TAU which is equal to 2 * PI). If you’re more comfortable working with degrees, you’ll need to use the deg2rad() and rad2deg() functions to convert between the two.

Testing the scene

Let’s test the scene to make sure everything is working. Add this new_game call to _ready():

GDScript   C#C++

  1. func _ready():
  2. randomize()
  3. new_game()
  1. public override void _Ready()
  2. {
  3. NewGame();
  4. }
  1. // This code goes in `main.cpp`.
  2. void Main::_ready() {
  3. new_game();
  4. }

Let’s also assign Main as our “Main Scene” - the one that runs automatically when the game launches. Press the “Play” button and select Main.tscn when prompted.

Tip

If you had already set another scene as the “Main Scene”, you can right click Main.tscn in the FileSystem dock and select “Set As Main Scene”.

You should be able to move the player around, see mobs spawning, and see the player disappear when hit by a mob.

When you’re sure everything is working, remove the call to new_game() from _ready().

What’s our game lacking? Some user interface. In the next lesson, we’ll add a title screen and display the player’s score.