![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | atiaxi |
I have a C# class that looks like this:
public partial class Constants : Node
{
public const int None = 0;
public const int X = 1;
public const int O = 2;
}
(It’s actually a full tic-tac-toe class, this is just a minimal example)
I’d like to be able to access those constants from GDScript, but it really doesn’t seem to want me to. I’ve added this to the tree as a script attached to a plain Node named Constants
.
Things that I have tried that do not work:
$Constants.X
→ Invalid get index ‘X’ (on base: ‘Node (Constants.cs)’).load("res://game_logic/Constants.cs").X
→ Invalid get index ‘X’ (on base: ‘CSharpScript’).load("res://game_logic/Constants.cs").new().X
→ Invalid get index ‘X’ (on base: ‘Node (Constants.cs)’).- Suggested by AI:
$Constants.call("X")
→ Invalid call. Nonexistent function ‘X (via call)’ in base ‘Node (Constants.cs)’. $Constants.get("X")
→ null$Constants.get_script().get_script_constant_map()["X"]
→ Invalid get index ‘X’ (on base: ‘Dictionary’). (the dictionary is, in fact, empty)- Doing any of the above but changing
const
in Constants.cs tostatic
→ The same errors as before
The only solution I’ve found to this is to give up altogether and only attempt to access instance variables. However, this only solves the GDScript ↔ C# interoperability. Other C# scripts which were referring to e.g. Constants.None
can now no longer do so since they’re instance-side. My workaround so far is to just define both and call e.g. $Constants.gdX
:
public partial class Constants : Node
{
public const int None = 0;
public const int X = 1;
public const int O = 2;
public int gdNone = None;
public int gdX = X;
public int gdO = O;
}
I’ve noticed that gdscript seems to have only grudging support for static
type access in general, so is this just an instance of language mismatch? Is this even possible? Is there a workaround that satisfies both sides of the language divide?
I think your class Constants
could be static and not a node or just enum
Moreus | 2023-03-20 22:15
As mentioned, there’s more to the class than just the constants (e.g. the full game has a StartGame
, a TakeTurn
, etc) this was a minimal example to reproduce the problem.
If I change the example to be static it’s perfectly accessible from the C# side, but I can no longer attach it as a script on the gdscript side (because it needs to also be partial
and/or has to inherit from Node
)
atiaxi | 2023-03-20 22:21