The method of initializing rocket transform

Instead of creating an empty object in the player scene, does doing this :

func _shoot_rocket():
var offset = Vector2(20,0)
var new_rocket = rocket.instantiate()
new_rocket.global_position = global_position + offset
get_parent().add_child(new_rocket)

Not make more sense? you add the child to the scene instead of the player, where it belongs?

1 Like

Having not looked into the surrounding code very deeply, I remember thinking something similar and making a change along these lines in my own project, although not exactly this one.

Your logic makes a lot of sense and I’m pretty sure it would work fine. Don’t be afraid to change things around and make extensions along the way, that’s half your learning right there. If you’re already thinking like this, that’s fantastic! In this particular case however, because I know what’s coming, I would actually just make a personal note about it and save the code for later.

The reason for this is because Kaan will soon (if he hasn’t already) introduce signals, and even a signal chain in one spot. Signals are one of several ways to achieve inter-scene communication. While they aren’t without their pitfalls and shortcomings, signals are a very valuable concept to grasp, and they are probably the most challenging feature Alien Attack has to offer. I didn’t fully understand signals until after this course, and the practice/examples were very helpful in getting there.

More than likely, Kaan’s design decision was just to showcase signals a little better.

1 Like

If im thinking of signals as interfaces, am I more or less correct?

Sort of. You have actually already used signals by this point, just in a different way: connecting them in the Inspector.

Without getting into too much detail, signals behave just like their real-world counterparts:

  • Object A has a signal and some condition that causes it to emit
  • Object B has a reference of some sort to Object A. Object B has code that connects Object_A.signal to a callback (“signal response”) function on Object B.
  • When the signal is emitted, the connection causes the callback function to be called, and therefore, you can now use some action on Object A to do something on Object B.

The one problem with signals is that they can make it a little difficult to follow your control flow (the step-by-step of your code), and that adds another layer of complexity when you have to debug something, but you can probably already imagine how useful these can be =)

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms