I have an auto load script set up on Node2D, which is the root node, 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<GameManager>("/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

game error

By default GetNode returns a Node. While your GameManager inherits from Node2D, the code in your Player doesn't know that the Node is really a GameManager so it won't let you access currentDir. You can tell it what kind of object you are expecting like this:

var gameManager = GetNode<GameManager>("/root/GameManager");

That specifies GetNode should return a GameManager instead of just the Node base class of it.

Edit: oops, not putting the code in a code block stripped out the angle brackets part, which was the whole point.

9 months later