How to get global variables to work in C#

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mystical-TEDDY_

Guys, I don’t know why but for the “remote” and “local” tabs aren’t showing up at all. I have an auto load script set up on Node2D, but I can’t get anything to work while trying to reference a global variable.

public class GameManager : Node2D // [ Global Variable ]
public int currentDir = 1;

public class Player : KinematicBody2D // [ Player Node ]
var gameManager = GetNode(“/root/GameManager”);
gameManager.currentDir = 1;

There’s more code for the player but it’s not relevant
I also get this error message for trying to use the global variable -

Invalid token ‘=’ in class, record, struct, or interface member declaration

:bust_in_silhouette: Reply From: juppi

If you just need a script to store variables in (integers, strings, booleans) and nothing Godot related, then a static class with static properties is what you need.

using System;

namespace MyFunnyGodotGame
    public static class GameManager
        public static int CurrentDir { get; set; } = 1;

You can now just access that property from everywhere like that:

GameManager.CurrentDir = 2;