MatejSloboda / Dijkstra_map_for_Godot

MIT License
77 stars 13 forks source link

Referencing DijkstraMap from C# #122

Closed SimonasLetukas closed 1 year ago

SimonasLetukas commented 1 year ago

Hi,

Has anyone tried to reference DijkstraMap from C# and could help me figure out what I'm missing? Using Godot 3.5 btw

Working GDScript: onready var pathfinding: DijkstraMap = DijkstraMap.new()

"Cannot resolve symbol" in C#: private DijkstraMap _pathfinding = new DijkstraMap();

I see no AutoLoads or Plugins added for the project where GDScript version works. I found this suggestion online, but I'd like to have static types, if possible.

Any help or ideas would be very appreciated! Thanks :)

SimonasLetukas commented 1 year ago

Also found this. Seems that there is no way to get the actual types going. :(

SimonasLetukas commented 1 year ago

Leaving this code here in case it would be useful for anyone in the future.

public class DijkstraMap : Node
{
    private Object _dijkstraMap; 

    public DijkstraMap()
    {
        var dijkstraMapScript = GD.Load("res://addons/dijkstra-map/Dijkstra_map_library/nativescript.gdns") as NativeScript;
        _dijkstraMap = dijkstraMapScript?.New() as Object;
        if (_dijkstraMap is null) throw new ArgumentNullException($"{nameof(_dijkstraMap)} cannot be null.");
    }

    public Godot.Collections.Dictionary<Vector2, int> AddSquareGrid(Rect2 bounds, int terrain, float orthCost,
        float diagCost)
    {
        var dictionary = _dijkstraMap.Call("add_square_grid", bounds, terrain, orthCost, diagCost) 
            as Godot.Collections.Dictionary;
        return new Godot.Collections.Dictionary<Vector2, int>(dictionary);
    }

    public void SetTerrainForPoint(int pointId, int terrain)
    {
        _dijkstraMap.Call("set_terrain_for_point", pointId, terrain);
    }

    // ...
}

// Usage
public class Pathfinding : Node
{
    private readonly DijkstraMap _pathfinding = new DijkstraMap();
    // ...
}

Still need to test it thoroughly though, I'll update this later in case of any changes.

SimonasLetukas commented 1 year ago

Updated code above with working DijkstraMap C# interface setup.