Retrieve name of a PackedScene

Godot Version

v4.2.1.stable.official [b09f793f5]

Question

I have a bunch of enemies walking around the game scene, they all have a
@export var fruit : PackedScene
associated. For debugging purposes, I’d like to see which enemy has which fruit so I’m trying to assign the name of the Fruit to a label’s text every enemy is tagged with. But I’m always getting an empty string? I tried various things but the closest I got to what I’m looking for is to use
text = fruit.get_path()
which gives me the correct path but I don’t like this because it’s too long (I just need “Apple”, not the whole “res://Items/Edibles/… .tscn”)

If there’s no direct way I’ll just process the path name that I can obtain with get_path(), but is there a direct way of getting the name of the PackedScene?

They have a resource_name property that you can set in the editor, but they only work for some resources and not for others, I don’t really know tbh…

If it’s only for debug and you don’t mind a hacky solution this should strip down the path and only return the name of the scene:

var path = fruit.get_path()
text = path.right(-path.rfind("/") - 1).left(-5)

I would .instantiate() the scene and get the instance, use .name on the instance, and that’s all (no .add_child() obviously.

func get_scene_name():
  var instance = scene.instatiate().
  var name = instance.name
  return name

Yes I’ll just strip the name from the path for now. Thanks!

Wouldn’t instantiating the scene just for the sake of getting the name of a PackedScene be kind of wasteful?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.