Excluding a group of spawned nodes when Raycasting

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AveryRe
:warning: Old Version Published before Godot 3 was released.

When raycasting using: var result = space_state.intersect_ray(get_global_pos(), global.PLAYER_POS, [ self ] ); where [ self ] is an array of objects to exclude from collision, how would I go about excluding player-spawned collision objects (such as bullets?).

I add them all to single node, “World Objects”, and I was wondering if there was a way to specify to exclude all collision objects who are children of that node?

Thanks!

:bust_in_silhouette: Reply From: avencherus

Your title doesn’t really match what you’re asking. It seems like you understand the idea of an exclusion array already.

It seems to me you want to know more about how to track references.

Two immediate options are:

A) Use an array that stays in scope, and at the point of code where all projectiles are created, append their reference to that array, and at their point of despawn you would then remove them.

B) If you just want to build the array every frame from children in a certain node you would do something like this:

var node = get_node("path goes here")

var exclusion_list = []

for child in node.get_children():
	if(child.is_type("PhysicsBody2D")): 
		exclusion_list.append(child)

print(exclusion_list)
var result = space_state.intersect_ray(get_global_pos(), global.PLAYER_POS, exclusion_list)