First thing, as always, we'll do some refactoring.
Open the asteroid.script, and in the init function, delete the part about randomness.
Why?
Because this part is now handled in the asteroid_factory.script, and the randomness should only be set once, ideally in a main script or manager.
Currently, each time we spawn an asteroid, we reset the random seed, which is unnecessary.
[91–94] – This is the part we need to delete 🙂
Then open asteroid_l.go, add a factory component, and set its prototype to asteroid_s.go.
Go back to the asteroid script. We’ll add a function to handle what happens when the asteroid explodes. Add this function at the top of the script, just before the init function.
Then, we need to know when to make the asteroid explode.
Currently, it’s the laser.script that handles the deletion of the game object. Now, we want the laser.script to send a message to the asteroid.script, telling it to delete itself.
So, we need an on_message function!
Go to the bottom of the asteroid.script, and just after the update function, write this code:
[140–145] – This is the on_message function we’ve already used before. Here, if the message is "explode", we simply call our explode() function.
Now we need to send a message from the laser.script to the asteroid.script.
We’ll use msg.post() for that.
Remember, msg.post() is Defold’s way of sending asynchronous messages between components or to engine systems (like sprite, label, GUI, etc.).
Open the laser.script, and inside its on_message function, delete this part of the code
Write this new one instead:
[35] – msg.post allows us to communicate with the other_id and send it the "explode" message.
Go to the main collection and add an asteroid_l.go to quickly test the modification.
It should work as expected!
Open the asteroid.script again and let’s modify the explode function. Now, if the asteroid is a big one, we’ll spawn some small asteroids.
Write the following code:
[91–94] – Just before deleting the game object, we check if it’s a big asteroid.
If it is, we call a function (which we haven’t created yet) to handle spawning the smaller asteroids.
Let’s now create the spawn_small_asteroids() function.
We want to spawn 2 or 3 small asteroids, you can add more if you like. For each new asteroid, we’ll give it a random direction.
Write the code below, placing it just before the explode function:
[91] – We save the URL of the factory component.
[92] – Generate a random value to decide how many small asteroids to create.
[93] – Save the position of the current asteroid to assign it to the new small asteroids.
[95] – Use a for loop to create the desired number of small asteroids.
[97] – Create the asteroid and store its ID (not the asteroid itself) in a variable.
[100] – Here, math.random() alone returns a floating-point number between 0 and 1. We multiply it by 2 * math.pi (where math.pi is the value of π).
[101–103] – We declare rand_direction as a vector using math.cos and math.sin to calculate its components.
[106] – Send a message "move_away" to the new asteroid, passing the rand_direction vector in the message data.
Wow, that was a lot to cover! I hope my explanations were clear for you.
In case you’re wondering why it’s necessary to tell the asteroid to “move away”:
Normally, when an asteroid spawns, it moves toward the center. After spawning, we tell the small asteroids to change direction so they don’t just cluster in the middle.
Now, let’s implement this behavior.
Go back to the on_message function and add the following lines of code:
[172–174] – We check the asteroid type and if the message is "move_away".
If so, we call the (yet to be created) move_away function, passing self and the move_away_direction parameter.
Now, just before the on_message function, write the move_away function:
[170] - we juste change the direction to the new new_direction
And that’s it! Take a moment to admire what you’ve achieved.
This last lesson concludes this part of the course 🎉.
I hope you learned a lot! I’ll keep improving this lesson based on your feedback.
In the next course, we’ll explore how to use the Defold tilemap editor and much more!