Issue accessing C# property in GDScript

Godot Version

4.5.1

Question

How do I get the return value of a property defined in C# using a script written in GDScript?

I want to get a Dictionary from C# and print out its key and values, but this is causing an error claiming it cannot find the .Players property in PlayerManager.cs node.

I read over the documentation on Cross-language scripting however it didnt cover this use case. I would prefer to avoid signals for now if possible - as this is mostly a debugging feature.

The error I get in the print_player_list.gd script is:

Invalid call. Nonexistent function 'Players' in base 'Node (PlayerManager.cs)'.

Scene structure:

┖╴GameVars
┃   ┖╴ GameSession
┃        ┖╴PlayerManager (PlayerManager.cs)
┖╴Players
┃   ┖╴Vehicle1 (Vehicle.cs)
┃   ┖╴Vehicle2 etc. 
┖╴PrintPlayerList (print_player_list.gd)

PlayerManager.cs

public partial class PlayerManager : Node
{
...
    Dictionary<long, Vehicle> _Players; // Key: MultiplayerID
    public Dictionary<long, Vehicle> Players { get => _Players; }
...
}

Vehicle.cs

public partial class Vehicle : VehicleBody3D
{
...
    string _PlayerName = "unset";
    public string PlayerName { get => _PlayerName; }
...
}

print_player_list.gd

extends Node

@export var print_players_button : Button

func _ready() -> void:
    print_players_button.pressed.connect(on_print_players_button_pressed)

func on_print_players_button_pressed() → void:
	var players = (get_node("/root/GameVars/GameSession/PlayerManager")).Players

	for key in players:
		print(key)
		print(players[key].PlayerName)

Thanks for your help in advance!

I solved it. The problem was that I was using C# inbuilt collections, not Godot’s - and C#’s collections do not derive from GodotObject(per the docs).

To fix, replace:

using System.Collections.Generic;

with:

using Godot.Collections;

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.