disclaimer

Although I have quite a bit of programming experience in high level languages, I may not be qualified to teach you anything about game development as I am learning myself and I probably do not have a sense of how code should be written just yet. So please keep in mind that I am very new at this as well and don’t take it as the definite way to do it. It is only my way

assignment

with Godot v2.1.stable.officiali-godot

This is going to be a very simple assignment or so I hope because by this stage you should have been exposed to a lot of Godot elements and should be comfortable with scripts, certain decision making steps.

Given what I said above, in this section, the task this time is to remove the setter function __set_energy() and instead set the player_energy key on Globals only when the Player reaches the Exit tile. Furthermore, when the Player collides with the Exit tile, the scene should be reloaded.

Hints:

  1. the Exit Area2D node will need a new script, instead of the generic Area2D.gd. You’ll need to pretty much do the same as we did for the Food, create a new script and extend Area2D.gd, then check collision with Players Area2D. The only difference is that the Exit tile won’t replenish any energy and you’ll have to reload the scene. This can be done by calling self.get_tree().reload_current_scene()
  2. you should verify that the energy value is indeed set correctly when the scene gets reloded. That is, if the Player reaches the Exit tile with an energy value of 15 let’s say, then, after the reload of the scene is triggered, the Player should start with an energy of15 instead of the default 100. You should check this with a print statement inside of the _ready() method from Player

solution

As said in the assignment statement, this one was very simple. And just to make it clear, this is only one way of doing it.

In short, let’s see the changes. First hint says to create a new script for the TileSet/Exit/Area2D node instead of using Area2D.gd. And in this script we’ll check the collision with Player/Area2D and save the information here:

extends "Area2D.gd"

func ready():
    self.connect('area_enter', self, '__on_area_enter')

func __on_area_enter(area):
    var area_parent = area.get_node('..')
    if area_parent.is_in_group('player'):
        Globals.set('player_energy', area_parent.energy)
        self.get_tree().reload_current_scene()

Very simply we inherit from Area2D.gd script, and just like in Item.gd we connect the area_enter signal to the __on_area_enter() function. This function gets triggered any time a collision is detected with another Area2D node. Remember that it gets passed in a parameter by Godot. This parameter is the colliding object (the other Area2D node).

Just in case, we check if the parent node of the colliding Area2D is in the player group (remember we set this when creating the TileSet) and then save the Player.energy variable to the appropriate key in the Globals object and finally reload the current scene as instructed in the given hint.

Inside of the Actor.gd script, we remove __set_energy() altogether as we don’t need it any longer. We also make the proper changes to the energy variable declaration and finally add a print statement inside of the __ready function to test out our solution:

# ...
var energy = 100
# ...

func _ready():
    # ...
    print('energy: ', self.energy)

That’s it! Super easy!

closing remarks

As always, let me know what you think about the lessons/tutorials so I can improve them.

project files

Get the zipped finished project for this part with annotated scripts and see if you could follow along! Or if just want to consult them.