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.
Area2Dnode 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
Area2D. The only difference is that the
Exittile won’t replenish any
energyand you’ll have to reload the scene. This can be done by calling
- you should verify that the
energyvalue is indeed set correctly when the scene gets reloded. That is, if the
Exittile with an
15let’s say, then, after the reload of the scene is triggered, the
Playershould start with an
15instead of the default
100. You should check this with a
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
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!
As always, let me know what you think about the lessons/tutorials so I can improve them.