Need help in accessing C# variables or functions through gdscript

Godot Version

4.3

Question

i have the following scene:
-scene (Node)
----PointsCalculator (with attached the PointsCalculator.cs script) (Node)
----PointsDrawer (with attached the PointsDrawer.gd script) (Node2D)

i am trying to generate a few (x,y) points in the C# script and use the gdscript to draw them

PointsDrawer.gd script:

extends Node2D

var points_calculator

func _ready():

points_calculator = get_node(“/root/scene/PointsCalculator”)

if points_calculator:
print(“Successfully connected to PointsCalculator”) #i get this
if points_calculator.has_method(“GetPoints”):
print(“GetPoints method exists!”)
else:
print(“GetPoints method is missing!”) #i get this
else:
print(“Failed to connect to PointsCalculator”)

print(“1”)

queue_redraw()

func _draw():
var points = points_calculator.GetPoints() #fails here with “Invalid call. Nonexistent function ‘GetPoints’ in base ‘Node (PointsCalculator.cs)’.”

for point in points:
draw_circle(point, 5, Color(1, 0, 0)) # Red color circles with radius 5

func _process(_delta):

queue_redraw()

PointsCalculator.cs script:

using Godot;
using System;
using System.Collections.Generic;

public partial class PointsCalculator : Node
{
private List Points = new List();

// Example function to calculate points (you can customize this).
public void CalculatePoints()
{
    GD.Print("3!");
    Points.Clear();
    for (int i = 0; i < 10; i++)
    {
        float x = i * 20;
        float y = (float)(50 + 30 * Mathf.Sin(i));
        Points.Add(new Vector2(x, y));
        GD.Print(i," -> ", x," - ", y);
    }
    GD.Print("4#");
}

// Public method to get the points.
public List<Vector2> GetPoints()
{
    GD.Print("GetPoints method called");
    return Points;
}

public override void _Ready()
{
    // Calculate points when the node is ready.
    CalculatePoints();
    GD.Print("2!");
    for (int i = 0; i < 10; i++)
    {
        GD.Print(i, " -> ", Points[i]);
    }
}

}

the various sanity check prints are actually called and the gdscript connects the PointsCalculator, but cant find the GetPoints Method. Even if i replace it with Points as public, the gdscript still can’t find the variable Points.

Can someone help?
thanks is advance

You can try with signals

1 Like

So, that’s the only way?
isn’t it weird that it can’t be done in a more simple way?

The documentation page above does provide a way to access anything from a C# script with GDScript, and vice versa, here’s the specific point in the docs:

You can even call methods with no issues at all.

i have read that, but in my case i can’t understand why my listings dont work

PS converting the PointsDrawer to C#, it works. But that defeats the purpose of the mixing language scripts that Godot boasts about

PS2. if i try to return a single 1D array of numbers (public Array GetPoints()), everything works well between gdscript and C#. The moment i change the type of the function to list of Vector2

(public List <Vector2> GetPoints())

the gdscript cant find it and get the error mentioned above.
Any useful insights or ideas?

You can use Metadata. But signals give you more controlling

GDScript requires types to be a Variant type which is specific to the engine. You will need to return a Godot.Collections.Array<Vector2> rather than a System.Collections.Generic.List<Vector2>. If I am not mistaken you can pass any IEnumerable type into the Godot Array contructor to create a new one containing the data from the IEnumerable, so you could return new Godot.Collections.Array<Vector2>(list_var);