Cross-Language and Autoload

Godot Version

Godot 4.2.1

Question

Hello Guys, i’m new of godot and i’m trying to implement some cross-language code. Here is my problem, i’m working in C# but i have one addon in gdscript, i just need to have this gdscript as a Singleton. Now if i instantiate it in the autoload:


and try to call the node:

public partial class GameManager : Node
{
	//AddManager
	AddManager AddM; 

	public override void _Ready()
	{	
		//AddManager
		AddM= GetNode<AddManager>("/root/AddManager");
	}
}

it returns this error: "CS0246: The type or namespace name ‘AddManager’ was not found. You’re probably missing a using directive or assembly reference. "

If i try to instantiate it from my C# script and set it as a Singleton (from script) is just disposed the moment i switch scene:

public partial class GameManager : Node
{
	//AddManager
	GodotObject AddM; 

	public override void _Ready()
	{	
		//AddManager
		
		GDScript MyGDScript = GD.Load<GDScript>("res://add_manager.tscn");
		AddM = (GodotObject)MyGDScript.New();
		//Added to singletons
		GetTree().Root.AddChild((Node)AddM);
		AddM.Call("_ready");
	}
}

What shoud i do?
It is possible to call that singleton from my C# code?
It is possible to make my node a Singleton in code?
I hope I explained myself as best as I could. Thanks in advance to anyone who tries to give me a hand

I was recently working on a C#/GDScript project as well and encountered this issue too. I ended up doing this on the C# side:

	var sceneManager = GetNode("/root/SceneManager");
	sceneManager.Call("pop_scene");

I was easily able to reference types defined in C# on the GDScript side and It seems like you should be able to reference Types from C# → GDScript, but I didn’t find an answer for that and ended up dealing with the situation using the 'Call(‘methodName)’ functionality.

I also ended up changing one of my types that I had defined in GDScript over to C# to make the C# side more convenient/possible. So if someone else knows a better way, please let us know.

1 Like

Thank you man.
Good work around. :wink:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.