How to check all things inside an array

Godot Version

func _process(delta):
var Raycasts = [$Up, $Down, $Right, $Left]
if [Raycasts].is_colliding():
print(“Collided”)

Question

Hey! Does anyone know how to use the Array name to check with all the things inside of the array? So in this code I have 4 diffrent raycasts and I want to have them in one array so that I dont have to write multiple lines that checks if one of the raycast hit.

use a for loop! The break keyword can also be helpful if you just want the first collision.

for ray in Raycasts:
    if ray.is_colliding():
        print("Collided")
        break #stops after first hit

Should the loop be in a func _process(delta): loop?

I tried it like this:

func _process(delta):
	var Raycasts = [$Up, $Down, $Right, $Left]
	for ray in Raycasts:
		if ray.is_colliding():
			print("Collided")
			break

But it didnt work

Why didn’t it work? Could you show more about your test scene, like the raycast properties and what it is supposed to be hitting?

It would be best to declare var RayCasts outside of the _process function so that it doesn’t re-create the Array every frame.

1 Like
	var Raycasts = [$Up, $Down, $Right, $Left]
func _process(delta):
	**for ray in Raycasts:**
		if ray.is_colliding():
			print("Collided")
			break

Then I get an error cause the func does not know what the Raycasts array is

Where the two ** is, is where it shows a red line

You need to remove the indentation:

var Raycasts = [$Up, $Down, $Right, $Left]
func _process(delta):
	for ray in Raycasts:
		if ray.is_colliding():
			print("Collided")
			break
1 Like

Now It I get an error that says that the .is_colliding is null in instance

Then add @onready, and make sure those nodes actually exist

1 Like

the lines is now red

got this error: Unindent doesn’t match the previous indentation level.

Please show your code

ah wait

@onready var Raycasts = [$Up, $Down, $Right, $Left]
func _process(delta):
	for ray in Raycasts:
		if ray.is_colliding():
			print("Collided")
			break

nothing happens when the raycasts hit a wall

And you are sure the raycasts are actually correctly set up?

Here is a picture:
image

I mean are they aimed correctly and are their layers set up?

I even made them a bit bigger: