Create a Catch the egg game in Godot engine – 5

In this part we are going to spawn the eggs randomly. Right now our basket detects the egg dropped inside. Before going to spawn the eggs, we are going to remove the dropped eggs from the scene. So let’s start.

We already attached a script to the basket node. It looks like this.

extends KinematicBody2D

func _ready():
    print("test")
    pass


func _on_Area2D_body_enter( body ):
    print("Dropped inside")
    pass # replace with function body

In the signal callback _on_Area2D_body_enter, we get the body which entered into the area. This body will be the egg.

So change the callback to look like this.

func _on_Area2D_body_enter( body ):
    body.queue_free()

This will remove the egg when it is dropped inside the basket. Great! the major part of our game is done.

But what happens to the egg which are dropped to the ground. It will stay there because we didn’t code any logic to handle that situation. To do that, create a new script for the egg node.

Similar to how we added the area_enter signal callback for the basket, add a body_enter signal callback on the egg node.

After doing that, we get a callback method in the egg script. Add a print statement to check the collided body name.

Make sure you have set the Contacts Reported to 1 and enable the Contact Monitor to true for the egg node. Otherwise the print won’t work.

func _on_egg_body_enter( body ):
    print(body.get_name())

Now, when the egg drops to the ground, we get the print “ground”. Replace it with this

func _on_egg_body_enter( body ):
    if(body.get_name()=="ground"):
        queue_free()

and we get the result we wanted, the egg disappears on hitting the ground.

Also Read:   Create a Catch The Egg game in Godot Engine - 1

Controlling the basket

We need to check the mouse x position and assign it to the basket, so that it moves when we move the mouse. The basket script looks like this now,

 

extends KinematicBody2D

func _ready():
    set_process(true)


func _process(delta):
    var mouse_pos = get_viewport().get_mouse_pos()
    var this_pos = get_pos()
    this_pos.x = mouse_pos.x
    set_pos(this_pos)


func _on_Area2D_body_enter( b ):
    if(b.get_name() == "egg"):
        b.queue_free()

We can see that the signal callback now includes a check if it is colliding with the egg. This will not work when we spawn multiple eggs or if the name changes. To avoid that we will be adding this egg to a group. Select the egg node and click on the Group button on the top of the hierarchy. Type “eggs” and add the group. Now change the above callback like this,

func _on_Area2D_body_enter( b ):
    if(b.is_in_group("eggs")):
        b.queue_free()

Great, it works the same as before but this time, it checks the group instead of the name.

Creating the egg subscene

To spawn multiple eggs, we need to convert our egg node to a packed scene. To do that, select the egg node, and click the Create Scene From Node(s) button.

Convert to subscene

Name the scene as egg.tscn inside the scenes folder. We can now delete the egg node from the hierarchy. Then create a new ‘Node’ as the child of ‘main’ and rename it to EggSpawner. Also add a script to it. This script is responsible for the spawning of the eggs.

Also Read:   Animated particles in Phaser

 

extends Node

export var delay = 1
export (PackedScene) var egg

var timer = 0

func _ready():
    set_process(true)

func _process(delta):
    timer += delta
    if(timer >= delay):
        timer = 0
        create_egg()

func create_egg():
    print("Creating egg")
    var e = egg.instance()
    e.add_to_group("eggs")
    add_child(e)
    e.set_pos(Vector2(rand_range(0,get_viewport().get_visible_rect().size.x),0))

The script is self explanatory. We export two variables to edit its behaviour inside the editor. The first one is spawn delay which is in seconds and it is default to 1. The next one is to select a PackedScene. This PackedScene is instanced multiple times. Select the EggSpawner node and we can see it in the properties panel. Browse and select the egg.tscn scene. The create_egg function create instances of the egg scene and add it to the scene as well as to the “eggs” group. The x position is a random one ranging from 0 to the viewport width. That’s all, press Play and enjoy the game.

This part concludes the Catch the egg game tutorial series. It was only meant to show the workflow and just the basics to quick start into the Godot engine. You can always apply your thoughts and extend this with other features. Thanks for reading.

The full source code is available here.
https://github.com/vinod8990/godot_example_catch_the_egg

[Total: 6    Average: 3.5/5]