Accessing c# static integers in a GDscript

Godot Version

V4.2.2.stable.mono.official[15073afe3]

Question

Hi,

TLDR: How do I access Static Integers written in a C# class from a GDScript?

The long version….

I am working on an Arduino based game for my kids utilizing the c# Serial library. I have some inputs(potentiometers, buttons, slide pots) from a custom Arduino controller that I would like to interface with my Godot game.

The structure is laid out like this in Godot.

I have an Arduino class(Arduino.cs) that connects, via Serial port, to the Arduino Mega. After establishing the port, baud rate and opening the pipe everything works great within that class.

Arduino.cs

using Godot;
using System;
using System.IO.Ports;

Establish variables
	byte[] data = new byte[8]; // This is the array that is temporarily storing the bytes coming in through Serial

_onReady(){
		textBlue = GetNode<RichTextLabel>("text_blue");
		textRed = GetNode<RichTextLabel>("text_red");
		textGreen = GetNode<RichTextLabel>("text_green");
		textYellow = GetNode<RichTextLabel>("text_yellow");
		textK1 = GetNode<RichTextLabel>("text_knob1");
		textK2 = GetNode<RichTextLabel>("text_knob2");
		textK3 = GetNode<RichTextLabel>("text_knob3");
		textK4 = GetNode<RichTextLabel>("text_knob4");
		
    try
		{
		serialPort = new SerialPort();
		serialPort.PortName = "/dev/cu.usbmodem2101";
		serialPort.BaudRate = 31250;
		serialPort.Open();
		}
		catch(Exception e)
		{
			GD.Print("No Connection at this moment. Please restart.");
		}
}

_onPhysicsProcess(double delta) // I don’t want to use “Process()”. 60fps works great.
	{
        if serial connection(){
	store all the bytes into variables and then display them in the text labels.
	}
}

Phase 2
I am currently feeding the stream of bytes coming in to Arduino.cs to a second utility class(myGlobals.cs) that stores said bytes into Static Integers.

myGlobals.cs CODE

    public static class myGlobals
{
    public static int bB {get; set; } = 0;  // blue button
	public static int gG {get; set; } = 0; // green button
	public static int rR {get; set; } = 0;  // red button
	public static int yY {get; set; } = 0; //yellow button
    public static int k1 {get; set; } = 0; //Potentiometer 1
	public static int k2 {get; set; } = 0; //Potentiometer 2
	public static int k3 {get; set; } = 0; //Potentiometer 3
	public static int k4 {get; set; } = 0; //Potentiometer 4

}

New Arduino.cs code

		myGlobals.gG = data[0];
		myGlobals.yY = data[1];
		myGlobals.rR = data[2];
		myGlobals.bB = data[3];
		myGlobals.k1 = data[4];
		myGlobals.k2 = data[5];
		myGlobals.k3 = data[6];
		myGlobals.k4 = data[7];

The reason for this is that I want to eventually create various levels that can each get the static variables to manipulate things in that level. Now at this point everything works well. The level that I am currently using to interface with the Arduino is also monitoring the inputs(Using text labels on the stage) so the round trip from the two C# classes works great.

Here is where I am getting hung up…

I want to use GDScript to build out all the levels and handle the rest of the game. I have a stage with a virtual replica of the controller(I made out of control,Box containers and texture nodes) using GDscript(PanelUI.gd) to manipulate the various components. But I can’t for the life of me figure out how to access the static variables from the “myGlobals.cs” file in that GDscript?

PanelUI.gd code


extends Control

//Establish variables
var blueButton
var greenButton
var redButton
var yellowButton

func _onReady():


//check if state has changed and update. 
Func _PhysicsProcess(_delta):
	blueButton = myGlobals.bB
	greenButton = myGlobals.gG
	redButton = myGlobals.rR
	yellowButton = myGlobals.yY
	
	if blueButton > 0:
		change the texture to blue.
	else:
		change the texture back to black.

…and so on

Any help would be greatly appreciated.

you cannot directly access static c# variables from gdscript.

if you need to store values in c# as static variables and you need to access those values from gdscript, use fields to access their value.

using Godot;
using System;

[GlobalClass]
public partial class MyCsharp:Node {
	public static int M_number1 = 1; // static int

	// use field to return value of static int
	public int NUMBER1 {
		get => M_number1;
	}

	public int NUMBER10 = 10;
	public int NUMBER100 = 100; // or just access the variable
}

extends Node

@onready var m_constants : MyCsharp = $"/root/Test/Constants"

func _ready() -> void:
	print(m_constants.NUMBER1)
	print(m_constants.NUMBER10)
	print(m_constants.NUMBER100)

Autoloads/Singletons are what you are looking for.

2 Likes

Maybe use Signals too?

1 Like

Thanks, Man! This did the trick.

2 Likes

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