0 votes

I'm trying to get debug-console working in my c# project (https://github.com/QuentinCaffeino/godot-console)

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!

asked Jun 2 in Engine by Ditoga (24 points)
edited Jun 2 by Ditoga

1 Answer

+1 vote
Best answer

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
answered Jun 2 by omggomb (655 points)
selected Jun 2 by Ditoga

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);
}

}

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.