Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | 9BitStrider |
C:\Users\nineb\Documents\GitHub\Retro-Game-Wrapper-Sharp\scripts\disclaimer.cs(9,18): The name 'GlobalVars' does not exist in the current context
I get the above error when trying to pull a value from global_vars.cs in my project. I have the script set to autoload in the project settings with the name GlobalVars.
using Godot;
using System;
public class disclaimer : Node
{
public override void _Ready()
{
GD.Print(GlobalVars.scaleFactor);
}
}
The above code SHOULD spit out the scale factor value saved in global_vars.cs, but it errors out no matter what I do. I’m very new to using C#. Any help would be appreciated.
For clarification, here’s the script being used in global_vars.cs.
using Godot;
using System;
using Dictionary = Godot.Collections.Dictionary;
using Array = Godot.Collections.Array;
public class globalvars : Node
{
//Set the maximum supported resolution and game window resolution respectively.
public Vector2 appRes = new Vector2(3840, 2160);
public Vector2 gameRes = new Vector2(256, 240);
//Set the display type of the scaling to be done.
//0: Integer Scaling - 10:9
//1: Integer Scaling - 4:3
public int scaleType = 0;
public float scaleFactor;
//Add additional variables here.
public override void _Ready()
{
//Get the viewport node.
var viewPort = GetViewport();
//Connect the signal and run the initial resize script.
viewPort.Connect("size_changed", this, "resizeApp");
resizeApp();
}
public void resizeApp()
{
//Grab the viewport node.
var viewPort = GetViewport();
//Get current desktop size and initialize the scale factor.
var newRes = OS.GetScreenSize();
//Scale the screen.
if(newRes.x < appRes.x)
{
scaleFactor = newRes.x / appRes.x;
newRes = new Vector2(appRes.x * scaleFactor, appRes.y * scaleFactor);
}
if(newRes.y < appRes.y)
{
scaleFactor = newRes.y / appRes.y;
newRes = new Vector2(appRes.x * scaleFactor, appRes.y * scaleFactor);
}
OS.SetWindowSize(newRes);
OS.CenterWindow();
viewPort.SetSizeOverride(true, newRes);
}
}
The above works just fine when the app starts. I do get an error, but the game runs nonetheless:
E 0:00:00.707 start: Script does not inherit a Node: res://scripts/globals/global_vars.cs
9BitStrider | 2021-07-06 14:48
And now my app isn’t loading global_vars at all. What am I doing wrong?
EDIT: Deleting the script and retyping them now works. Is Mono really this broken?
9BitStrider | 2021-07-06 14:57
First snippet tries to access GlobalVars
, but the class shown is named globalvars
.
Is GlobalVars
a Godot singleton? As of right now, C# can’t use Godot singletons just by name reference alone like you can in GDScript; you have to do something like this:
var GlobalVars = GetNode<GlobalVars>("/root/GlobalVars")
GD.Print(GlobalVars.scaleFactor)
I got a similar error to the “does not inherit a Node” thing when adding a new singleton written in CS; closing back to the project list and re-opening fixed it. Not sure what the deal is there.
EDIT: Also, the class name and the name of the CS file must match, as noted in the C# basics page on the docs, otherwise Godot can’t find the class! So if your class is named GlobalVars
, it should be in a file named GlobalVars.cs
, not global_vars.cs
Void | 2021-07-10 21:32