0 votes

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
Godot version 3.3
in Engine by (227 points)

1 Answer

+2 votes
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.

by (3,389 points)

Nice solution. Thanks.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.