Get the name of a resource

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

Do you know of a better method to get “my_resource”?

static func get_name(resource: Resource):
	
    var resource_name = resource.resource_path # "res://path/my_resource.tres"
    resource_name = resource_name.split("/")  # ["res:", "path", "my_resource.tres"]
	resource_name = Array(resource_name)
	resource_name = resource_name.back() # "my_resource.tres"
	resource_name = resource_name.trim_suffix(".tres") # "my_resource"
	
    return resource_name
:bust_in_silhouette: Reply From: timothybrentwood
static func get_name(resource: Resource):
    return resource.resource_path.get_file().trim_suffix('.tres')

As a general rule of thumb, if you have something that works in a non-performance critical section of you code, you shouldn’t necessarily worry about optimizing it.

Nice solution. Thanks.

luislodosm | 2021-04-30 16:36