How to check if collision happend from outside?

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

Well on progress in the game there will bullets get generated and like in space invader, there will get enemies generated.

on my main Node i want to check if bullet and enemy are colliding or not.
i dont want to write that code into the bullet or the enemy, because they re not there all the time and in futur i want to reuse the scene maybe.

so how can i chack from my main-node, if node-bullet and node-enemy are colliding?

:bust_in_silhouette: Reply From: Mison

You’ll need to detect the collisions on the sprites (as per usual) with the node event ‘on_area_entered’ and have that use a function on the sprite to send a signal to the main script which will handle the logic.

:bust_in_silhouette: Reply From: Brinux

To communicate your collisions back to your main node without putting code on your bullets or enemies… for that, you will need signals.

Your bullets will need collision nodes, as will your enemies. You could use your bullet object with a child of Area2d, and the Area2d has a child of collision. Don’t forget to set your collision shape. Do the same setup for your enemies.

Now use your signals, like “body_entered”, and point them back to functions in your main node for evaluating those collisions.

Here’s a great video on explaining signals which should be a big help: Introduction to Signals in Godot (tutorial)

:bust_in_silhouette: Reply From: gruen

hi. thank you for your answers. i know how to put a signal. but you cant put a signal to an parent who doesnt exists.

my bullets and the enemys arent in the scene till they will get generate

I think you should have a main scene which will have a script attached. This script can receive signals generated by child objects (bullets/enemies). If you don’t have a main scene with a script then I think you might be making things difficult for yourself. Look at the tutorial for some inspiration:
Your first game — Godot Engine (3.1) documentation in English

Mison | 2019-08-19 21:13