+1 vote

I am creating a game with chunk based generation like in Minecraft (but in 2D). I decided to change that part of the code to C# so I could get some performance improvements and use Threads in C# so I can make the process feel a bit smoother in-game.
I'm experienced with C#, but never used it with Godot.
There's a part in my GDScript code where I store all my Player instances so I can get some info about their current chunk later (I use class_name Player).

Problem is: How can I do that in C#? Can you reference a .gd "class" type the same way you can do it inside GDScript? I'm using Visual Code.

in Engine by (29 points)

1 Answer

+3 votes

To interoperate with GDScript from C#, you have to use Godot's generic functions from Godot.Object (a bit like reflection) because there are no actual C# functions and variables you can use.

So to call a function, it would be like this:

Node node = GetNode("Node/With/Your/Script/On/It")
node.Call("function_name", argument1, argument2);

Getting and setting variables:

Vector3 v = (Vector3)node.Get("variable_name");

node.Set("variable_name", value);
by (27,603 points)
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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.