2
\$\begingroup\$

I am using Godot Engine 3.0.6 Mono for Windows x64. I am prototyping the majority of my project in C# before I rewrite the game in C++ for release. I installed this plugin mostly to help with testing and debugging value-sensitive functionality. To make this possible however, I will need to be able to make references to my C# classes from GDScript (or vice versa) for binding commands. I could not find any instructions on how to do this in the official documentation. Can this be done currently in either direction?

Note: If the references are made from C# to GDScript classes, I need to be able to cast the Nodes respectively to make function calls.

\$\endgroup\$
2
\$\begingroup\$

I figured out how this works both ways after a couple of days.

It is not immediately obvious and the GDScript interpreter will yell at you, but as long as your C# objects are included in the .csproj file and the class itself extends Object, you can call any method in the C# script.

The C# API adds a nice way of calling GDScript instance functions, which is are the Godot.Object methods Call(), CallDeferred(), and Callv().

E.G. given:

SampleGD.gd

func from_csharp(content):
    print(content)

Then in C# you would obtain a reference to this object in the scene or use ResourceLoader to load a new instance. After such, you could do:

SampleCS.cs

var sampleGD = FindNode("SampleGD");
sampleGD.Call("from_csharp", "Hello from C#!");
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.