How to specify exported NodePath type?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ruckus T-Boom

Is there a way to specify the type of a NodePath?

Say I have export(NodePath) var target, is there a way I can specify target should be a KinematicBody, Target.gd, etc.?

:bust_in_silhouette: Reply From: eons

No, there is no way to filter by class.

If you need a restriction to avoid mistakes you can make a setter that checks, prints an error and ignore nodes of the incorrect type.

:bust_in_silhouette: Reply From: Multirious

from https://forum.godotengine.org/123057 (Mine)

You can do this by using _get_property_list()

func _get_property_list() -> Array:
    return [
        {
            name="test",
            type=TYPE_NODE_PATH,
            usage=PROPERTY_USAGE_DEFAULT,
            hint=35,
            hint_string="Node"
        }
    ]

At hint=35 is not in the docs; I found it from here

I have no idea how to do it for export key-word, though. Trying

export(NodePath, 'Node') var test: NodePath

return an error
Parse Error: Type "NodePath" can't take hints.
Advance export docs

Yes! This is the correct approach. However, you constant is wrong. It should be

PROPERTY_HINT_NODE_PATH_VALID_TYPES = 37:

The full code is then:

func _get_property_list() -> Array:
    return [
        {
            "name": "sprite",
            "type": TYPE_NODE_PATH,
            "usage": PROPERTY_USAGE_DEFAULT,
            "hint": PROPERTY_HINT_NODE_PATH_VALID_TYPES,
            "hint_string": "Sprite"
        }
    ]

Juxxec | 2023-04-06 10:18