Access to gdscript addon from c# (autoload)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ditoga

I’m trying to get debug-console working in my c# project (GitHub - quentincaffeino/godot-console: In-game console for Godot 3.)

creating new commands using a gdscript works fine, but I need to create these in c#, the problem is that I can’t access to the global variable ‘Console’ from C#

func _ready():
    # 1. argument is command name
    # 2. arg. is target (target could be a funcref)
    # 3. arg. is target name (name is not required if it is the same as first arg or target is a funcref)
    Console.addCommand('sayHello', self, 'printHello')\
      	.setDescription('Prints "Hello %name%!"')\
      	.addArgument('name', TYPE_STRING)\
      	.register()

but in c# says: The type or namespace name ‘Console’ does not exist in the namespace ‘Godot’ (are you missing an assembly reference?)

Any Ideas?
Thanks!

:bust_in_silhouette: Reply From: omggomb

Disclaimer: I usually don’t use C# in Godot.

Autoloads get attached to an invisible node with the same name under the scene tree root. So you could get it using var console = GetNode("/root/Console").

You’ll then need to use console.call() as described in the docs. I think you might be able to chain them like you do in gdscript: console.call("addCommand", ...).call("setDescription", ...).call("addArgument", ...).call("register");

EDIT:
Seems like you can’t just chain call together, you’ll need to do some typecasting in between e.g.

var temp = console.call("addCommand", ...) as Godot.Object;
temp = temp.call("setDescription", ...) as Godot.Object;
temp = temp.call("addArgument", ...) as Godot.Object;
// etc

Thank you! it worked:

here is an example:

using Godot;

using System;

public class ConsoleCommands : Node2D
{

Node console;
PlayerController player;

public override void _Ready()
{
    console = GetNode ("/root/Console");
    player =  GetNode <PlayerController>("/root/World/Player");

    var command = console.Call ("addCommand", "test", this, "test") as Godot.Object;
        command.Call ("setDescription", "This is a test command");
        command.Call ("addArgument", "show", Godot.Variant.Type.String);       
        command.Call ("register");

    command = console.Call ("addCommand", "player_showState", this, "showPlayerState") as Godot.Object;
        command.Call ("setDescription", "Show/hide player FSM State label.");
        command.Call ("addArgument", "arg", Godot.Variant.Type.Bool);      
        command.Call ("register"); 

}

public void showPlayerState (bool show=true)
{
    player.setStateLabel (show);
}

public void test (string arg)
{
    console.Call ("writeLine", "Hola, esto es un test, arg: " + arg);
}

}

Ditoga | 2020-06-02 14:40