0 votes

I'm making a Chess engine, but the engine is having some performance issues so I decided to transfer functions with heavy calculations from GDScript to C#. What keeps me from making proper progress is an error in C#.

Here are simplified versions of the scripts. foo.gd and Bar.cs are both AutoLoad scripts with the node name of Bar.cs as 'Bar'.

foo.gd

extends Node

func _ready():
    var inp = [[1, 2], [3, 4]]
    print(Bar.Test(inp))

Bar.cs

using Godot;
using System;

public class Bar : Node
{
    public override void _Ready(){
    }

    public static int Test(int[,] inp){
        return inp[1, 1];
    }
}

Whenever I index 2D Array in C# and the Array originated from GDScript, Godot throws the following error:

Int32 Bar.Test(System.Int32[,] ): System.NullReferenceException: Object reference not set to an instance of an object

The error refers to line 10 of Bar.cs. When I try to input 1D Array (i.e. [1, 2, 3, 4]) from foo.gd and tweak the C# function for indexing 1D Array, it worked, but Chess boards are 2D. I also tried calling the function from Bar.cs itself and input C#-type 2D Array, and it also worked, but I need to call from foo.gd.

What fixes this bug?

Godot version v3.4.4.stable.mono.official [419e713a2]
in Engine by (41 points)

1 Answer

+1 vote
Best answer

In short, you have to use the Godot.Collections.Array:

public static int Test(Array<Array<int>> inp)
{
    return inp[1][1];
}
by (1,075 points)
selected by

Thank you for suggesting, but a new error arises from this:

The non-generic type 'Array' cannot be used with type arguments

By replacing Array with Godot.Collections.Array, the code finally works! Thank you so much.

Here is the function

public static int Test(Godot.Collections.Array<Godot.Collections.Array<int>> inp){
    return inp[1][1];
}
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 Frequently asked questions and 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 [email protected] with your username.