GML Global Variables not working

N

NicNamSam

Guest
Hello. I decided that, instead of a bunch of objects accessing the variables of my player object, I would use global variables. This is my first time using them. Unfortunately they do not work like they are supposed to. I have tried setting the variables multiple ways: In an "initialization" dummy object placed in the room that sets the variables first then spawns my player object and other objects after; and I also tried putting it in the creation code for the room itself. I have made sure that the objects calling the variables are created after the variables are already set, but Game Maker throws the error no matter what.

The first global variable that triggers the error is called "g_Weapon" to store what weapon my character is holding. I put "g_" to denote that it's a global variable.

The Game Maker help states "In general you should have a single script or object that declares all your global variables at the very start of the game (for example, in the Room Start Event), so that they are initialised and ready for any instance to use, and this also gives you a handy place to go back and reference should you need to check a variable name." and "Once declared in this way that variable is now considered global and requires no "global." prefix".

I have made sure no other variables are using the names of the global variables. I have put the initializations as early as I possibly can. Here is the error it throws:

Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object obj_player:

Variable obj_player.g_Weapon(100115, -2147483648) not set before reading it.
 at gml_Object_obj_player_StepNormalEvent_1 (line 817) - if (g_Weapon == g_Swords || g_Weapon == g_Hook) isWpnHold = true;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_player_StepNormalEvent_1 (line 817)
It almost looks to me like it's trying to treat g_Weapon as an instance variable when it says "obj_player.g_Weapon", because that's usually how it looks when you access the instance variables of one object from another (objectName.VariableName). If I put "global." before a specific use of "g_weapon", the error just moves to the next use of g_weapon. If I put "global." before every single global object, it finally runs, but that doesn't seem very efficient, and the help explicitly states this shouldn't be necessary.

Can anyone with experience help me with this?
 

Bluetail7

Member
global variables do not depend of instances.

obj_player.g_Weapon is a local variable.

so if you set the g_Weapon in another instance it won't work
if you do obj_player.g_Weapon = "SET" from another instance, it might crash the game from either: the instance that won't exist or the obj_player trying to read it at the same time.

Note:
just if you need it.
Declaring variable
global.variablename = 1

Reading variable
global.variablename
 
N

NicNamSam

Guest
obj_player.g_Weapon is a local variable.
Reading variable
global.variablename
So, in order to access/read the "global.g_Weapon" variable, I always have to use "global." before it? Why does the documentation say I don't?
 

CloseRange

Member
We declare the "food" variable by first writing "global" and then a "." to tell GameMaker that this variable is now global scope. We will need to use this method from now on any time we are required to access or to change this variable in any way.
that was taken directly from the docs. Where does it say you don't?
global is like any other object in game maker, and if you ever want to acess an object's variables, you need to put the name of the object before the variable name, like obj_player.x, however in this case the name of the object is global
 
N

NicNamSam

Guest
Ok, I found my mistake. In the documentation, it includes an alternate way using "globalvar". Then, the following sentence is in reference to that method only. It says "Once declared in this way that variable is now considered global and requires no "global." prefix". I thought, since "globalvar" obviously doesn't contain "global.", it was referring to the "global." method, as a sort of concluding paragraph. I did a dumb.
 

mimusic

Member
edit: looks like you just figured it out lol

----

Once declared in this way that variable is now considered global and requires no "global." prefix

You misunderstood, and confused this statement to apply to all global variables. This statement directly comes after the explanation of "globalvar", not the "global." prefix. They both declare a variable on a global scope, but only "globalvar" allows you to later refer to the variable without any prefix.

It's recommended that users no longer use globalvar for global scoping, since it could become deprecated in any future version, but theyve been saying this since 1.4 and it's still around in 2. My only guess for the deprecation is that globalvar allows a player to address the variable before global scoping, and accidentally make it an instance variable instead.
 
Top