Having trouble with getting a script off of a node.
The following is my current code. I have a PlayerController script attached to the example node, which is named "Player". Yet I can find no way to access the script itself, to get any variables, public methods, etc.
I am going to use this to run a method defined by an Interface.

[Tool]
public partial class GizmoDrawer : Node
{
	[Export] Node example;

	public override void _Process(double delta)
	{
		GD.Print(example.Name);
		GD.Print(example.GetScript().As<CSharpScript>().ResourcePath);
		GD.Print(example is PlayerController);
	}
}

Console Output

Player
res://Scripts/PlayerController.cs
False

The console output indicates that the script is definitely attached, but I can't find anyway to access the instance of it through code.

    epicsninja Your code doesn't cast example to PlayerController. Since it's declared as Node you'll only be able to access the Node class functionality. Either declare example as PlayerController or cast it explicitly in code.

      xyz Casting directly does not work. In this case, example is a Rigidbody3D with a PlayerController script attached. Attempting to cast directly causes a System.InvalidCastException: Unable to cast object of type 'Godot.RigidBody3D' to type 'PlayerController'.

      • xyz replied to this.

        xyz Surprisingly, it does not. I genuinely expected it would.
        While it does highlight the "Player" node as a valid option, trying to set it in-editor returns the same error, System.InvalidCastException: Unable to cast object of type 'Godot.RigidBody3D' to type 'PlayerController'.

        epicsninja So, it seems my issue may be due to one class being marked as [Tool] and the other not.
        My intent here was to build a debug tool that would run a debug method within a game script while the editor was running. Unless someone can confirm otherwise, I believe this may be something the engine simply does not support.

          spaceyjase I am attempting to replicate Unity's OnDrawGizmos() method. My plan for this was to create a [tool] script, which when childed to a node would check if that node had a script attached, and if it implemented my GizmosInterface. If so, it would cast to that, then run my DrawGizmos() method.

          Unfortunately, as far as I can tell, classes not marked as [tool] aren't properly loaded into the editor. Even if I create an [Export] for a specific class I know uses the interface, the [tool] will only get the base type of the node (e.g, Rigidbody3D), not the script attached.
          This is not the case if I mark the class I want with [tool] as well, but then that defeats the purpose of what I am trying to do.

            epicsninja Yea, sounds like you should make it a editor plugin rather than a tool script. You'll get a deeper access to the editor that way. Mind you can still use gdscript or C#, plugins are not limited to C++.

            epicsninja Further testing leads me to believe I am half correct. I have created the following custom node, and childed it to my Player Node, which is a Rigidbody3D with a PlayerController script attached.

            [Tool]
            public partial class GizmoDrawer : Node
            {
                Node parent;
            
                public override void _EnterTree()
                {
                    base._EnterTree();
            
                    parent = GetParent();
                }
            
                public override void _Process(double delta)
                {
                    base._Process(delta);
            
                    DebugDraw2D.SetText("Parent is Null: " + (parent == null));
                    DebugDraw2D.SetText("Parent Script Path:" + parent.GetScript().As<CSharpScript>().ResourcePath);
                    DebugDraw2D.SetText("Has method: " + ((PlayerController)parent).HasMethod(PlayerController.MethodName.TestString));
                    DebugDraw2D.SetText("Result:" + ((PlayerController)parent).Call(PlayerController.MethodName.TestString).ToString());
                }
            }

            While in the editor, this throws an error and outputs System.InvalidCastException: Unable to cast object of type 'Godot.RigidBody3D' to type 'PlayerController'. to the console. However, if the script is marked as [Tool] or when run in-build, it works fine, and as intended.

            I can't tell if this is a bug, or intentional behaviour, but it seems like in-editor I am only allowed to access the base node type, while in-game I am able to access the script attached to the node.