How to get the index of a 3d Physics layer?

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

I need to build a bit flag to pass to PhysicsDirectSpaceState.intersect_ray(). To do this, I need the index of the 3d physics layer I’ve named ‘Player’ in Project Settings/Layer Names. Rather than hardcoding this flag in my script, is there a way for me to query the API to find out what flag index I gave to the layer “Player”?

Are you referring to the collision layer?

Ertain | 2020-11-10 18:57

Yes. The value you enter in the Project Settings/Layer Names/3d Physics spreadsheet.

kitfox | 2020-11-10 22:51

:bust_in_silhouette: Reply From: AndyCampbell

I think you need something like this:

func layer_index_from_name(target_name):
	for i in range(0, 19, 1):
		var layer_name = ProjectSettings.get_setting("layer_names/3d_physics/layer_" + str(i+1))
		if layer_name == target_name:
			return int(pow(2,i))
	return null



func _ready():
	var look_for = "test"
	var res = layer_index_from_name(look_for)
	if res: 
		print(res)
	else:
		print("Not found")