Hi, I'm coming over from Unity, just trying to get my feet on the ground with C# in Godot 3.2. I've created a simple script for my player character:

using Godot;

public class FirstPersonPlayer : KinematicBody {

    [Export] private Camera myCam;

}

Now, in Unity, the inspector would show you a field in the script variables which allows you to assign a camera. At first glance, Godot seems to do the same:

However, once I click on the dropdown...

... I get a rundown of ALL the stuff Godot knows. Not just cameras. Okay, that was irritating, but at the very bottom it says "load", which allows me to select a file. Except that the camera which is in my scene is not in its own file. It's just a sub-node.

"Okay", I thought, "next try. Let's just take the camera node from the scene tree and draaaag it over into the slot"... Except that I can't do that either: the inspector immediately switches to the camera, and I can't seem to lock it.

So basically: Is getNode(path) really the only way to get a reference to an object in the scene?

In Unity, even using getComponent() (which fetches a script on the same game object) is generally frowned upon, and iterating over the children is discouraged even more because it tends to make game code very brittle and creates implicit dependencies, whereas editor-exposed fields are at least explicit.

Am I missing something? Or is godot philosophy simply different here?

Okay so I did some research on the matter. There's this discussion on the topic: - https://github.com/godotengine/godot/issues/7821

... which lead me to the concept of NodePaths. Which is kind of what I'm looking for.

Except that:

  • Node paths are untyped. They can point to any node. There doesn't seem to be a way to restrict it, e.g. to only camera type nodes. Generics would be highly useful here. Any comments on that?
  • Drag-and-drop from the scene tree seems to be impossible due to the fact that the inspector switches immediately. Can I work around this somehow?

@Alan47 said: Node paths are untyped. They can point to any node. There doesn't seem to be a way to restrict it, e.g. to only camera type nodes. Generics would be highly useful here. Any comments on that?

I know that in the C++ code, you can limit an exported NodePath using a string, where the string contains the node(s) you want to include in the following format: "NodeTypeHere, AnotherNodeTypeHere".

So that may work with the exported NodePath variables in Godot using GDScript or C#, but I have not tested. If it does work, then something like this would be the syntax I would expect (GDScript, untested):

export (NodePath, "Camera, MeshInstance") var camera_or_mesh_path = null;

Drag-and-drop from the scene tree seems to be impossible due to the fact that the inspector switches immediately. Can I work around this somehow?

I'm not sure on this one, sorry!

2 years later