Sending signals from one scene to other

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Leandro

Greetings… I’m learning Godot (and trying to shape my brain from structured programming to object oriented programming) by creating a simple Asteroids like game.

So far I added ship movement, shots and a basic enemy (each one in different scenes, and the player ship adds instances of the shots one level above, on the main scene). But I still have no idea of how to deal with collisions. I cannot understand signals, even after the tutorials.

So, what I’m trying to do…

  • Player shots are “Area 2D” and enemy is Rigid Body 2D.

  • I added a “signal hit” after Extends Area 2D (trying to immitate the “Dodge the Creeps” tutorial on the Godot Learning.

  • I added a “body_entered” signal and clicked on “connect” and “connect’” again.

  • In the PlayerShot script, Godot automaticallt added a function “_on_PlayerBasicShot_body_entered(body):” where I made the shot invisible after hitting the collision of the enemy ship. It’s working so far until this point…

But it seems that the signal I did served only for the shot. Suppose that I have more than one instance of the enemy ship, how can I make the program recognize which one of them was hit, and how can I pass the signal to the enemy scene so I make a function to destroy it? Thanks in advance. :slight_smile:

P.S.: Why the heck I need to add that “signal hit” in code if the created signal connected automatically to another function Godot created?

:bust_in_silhouette: Reply From: p7f

Hi the signal body_entered(body) pass a parameter that has a reference to the object that entered the area. So, i would do this:

On Enemy script, add a function to handle the shot:

function got_hit():
    queue_free()

On BasicShot script:

func _on_BasicShot_body_entered(body):
    if body.has_method("got_hit"):
        body.got_hit()
        queue_free()

Basically, you add a function to recieve the shot in your enemy script. Then when the enemy enters the shot area, that function will be called, and enemy will be freed. You can also add a variable damage to your shot and life to the enemy and make it freed when life becomes lower to zero. If this works for you, i can expand.

Made it work. Many thanks! But I need to check if I really understood…

So, Whenever the shot enters an area it asks if that body/area has a function to handle the collision, if so, it calls that function from that method. In this case, it just removes the enemy instance. And of course, I could instead decrease an “energy” variable, and when it’s zero, call an explosion instance and then remove the enemy instance.

And about that “signal hit” (that I copied from Godot tutorial “Dodge the Creeps”)? Has it any use?

Leandro | 2018-12-05 18:51

Hi,
Yes, i think you got it!

I think in this case we do not need the signal hit. In the tutorial you mention, i think it uses it for ending the game or something.

BTW, have you created the drawings yourself? I like them.

Note: Please, if my answer worked, can you select it as correct? so other can see this question is solved!

p7f | 2018-12-05 19:02

I checked your answer as “best answer” (sorry, I’m a newbie in this forum). And yes, I did the sprites myself, using Blender 3D (awesome and free opensource software), and background layers with Photoshop. Once again, thank you! :slight_smile:

Leandro | 2018-12-05 19:13

Hey, are you just practicing with this or is it a full project? I’m looking for some project in which i could help and yours looks good!

p7f | 2018-12-05 21:45

Don’t be impressed with these screenshots, I’m a graphic designer, so I know some tricks. However, I’m not the best programmer.

I’m quite new to OOP and Godot (I’m programming in it for less than three weeks), so many concepts are still quite hard to fit into my brain (I did three games a long time ago in Blitz Basic and you can find them at www.leandrocorreia.com). Since Asteroids is a quite primitive and easy to program game, I decided to make an Asteroids clone from scratch to learn only.

However, Godot is really powerful and fast, so MAYBE I’ll make this into a full project. Maybe a hybrid of Asteroids and Geometry Wars. Anyway, by now I’m just learning.

Leandro | 2018-12-05 22:14

Ok, if you turn it into a project, don’t hesitate to contact me if you need help!

p7f | 2018-12-05 22:22

I’m a newbie at these forums. If you want to contact me, you can add me at Facebook:

Leandro Correia

Leandro | 2018-12-12 22:50