Problem with passing collision mask as Array[bool]

Godot Version

4.2

explanation :

hello :3
im trying to make a tank game , and i want to manage projectile(bullet , … ) spawns via signals and a spawn_projectile() method in each level (level scenes are also in group called “levels”) .
here is spawn_projectile() function :

#note: collision_layers => 1 :environment , 2 :player , 3 :enemies

func spawn_projectile(spawn_position:Vector2,projectile_rotation:float,projectile_scene:PackedScene,collision_array : Array[bool] = [true,false,false]):
	var new_projectile = projectile_scene.instantiate()
	new_projectile.position = spawn_position
	new_projectile.rotation = projectile_rotation
	for i in range(collision_array.size()) :
		new_projectile.set_collision_mask_value(i , collision_array[i])
	add_child(new_projectile)

each scene must connect their projectile_shot() to level.spawn_projectile() , and they do it like this :

signal projectile_shot(position:Vector2,rotation:float,projectile:PackedScene,collisisn_mask:Array[bool])

var is_shooting = false
@onready var level = get_tree().get_first_node_in_group("levels")

func _ready():
	if level != null :
		projectile_shot.connect(level.spawn_projectile)

func shoot():
	if is_shooting : return
	is_shooting = true
	$FireCooldown.start()
	projectile_shot.emit($BulletSpawnPoint.global_position,global_rotation,bullet_scene,[true,true,false])

func _on_fire_cooldown_timeout():
	is_shooting = false

problem : it just doesn’t work !

what is weird ?

it was working up until adding collision_mask array to signals and methods(it was based on default collision mask of projectile scenes before ) , is it impossible to pass array via signals ? or i implemented it the wrong way ?

What’s going wrong? Is there an error? Is does it set the wrong values?

it just acts as if there is no signal or method what so ever :expressionless: !

What happens if you remove the default value?

Also what happens if you use just a normal array instead of a typed one?

1 Like

it wasn’t spawning any bullet , after removing array type from target function but still , the collision were all wacky and it wasn’t because of default value of functions arguments , it was for the fact that collision layers start from 1 not 0 like arrays , so i changed new_projectile.set_collision_mask_value(i , collision_array[i])
to new_projectile.set_collision_mask_value(i+1 , collision_array[i])

and here is final spawn_projectile() which works the way i intended :

func spawn_projectile(spawn_position:Vector2,projectile_rotation:float,projectile_scene:PackedScene,collision_array : Array = [true,true,true]):
	var new_projectile = projectile_scene.instantiate()
	new_projectile.position = spawn_position
	new_projectile.rotation = projectile_rotation
	for i in range(collision_array.size()) :
		print(i , collision_array[i])
		new_projectile.set_collision_mask_value(i+1 , collision_array[i])
	add_child(new_projectile)

btw thank you for sticking and helping me to find the solution ! :3

1 Like