GameMaker Unknown Variable Error. Don't know What's Causing it

I don't exactly know how to explain this error or what is going on, but I've tried lots of ways to fix this error message that keeps popping up.

It started when I was finishing adding a inventory (that works in a previous game) to a new game I'm working on. As I was finishing the code, I went to do a test run to see if everything was working and I got the following error message. I looked around and found a few typos, but they didn't fix the following error message.

To be clear I know what the "not set before reading it." error message means and there's nothing in the Create section of the "o_game" object pertaining to "oPlayer2" to begin with, which is why I don't know what's causing this error, nor is "oPlayer2" even mentioned in the "o_game" objects code.

So, to see if it was a "oPlayer2" issue, I deleted the "oPlayer2" object and i get the same error message only instead of it saying "oPlayer2" it went to the next object in the list which is "oEnemy1" as seen in the second error message.

The inventory works in a different game I have in development and I followed the code the way it was entered and worked in that game and it works in that game the way it should.

The inventory works by pausing the game and the inventory UI shows. But now it won't even let the game run.

To be clear, "checkInput" is a GML script that fully worked before adding the inventory code.

If anyone knows what might be going on here, any help would be appreciated.

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

Variable oPlayer2.<unknown variable>(100012, -2147483648) not set before reading it.
 at gml_Object_o_game_Step_0 (line 30) - if checkInput.pause_pressed_ {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_o_game_Step_0 (line 30)

Exact Error Message on next run only with "oEnemy1"

############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object o_game:

Variable oEnemy1.<unknown variable>(100012, -2147483648) not set before reading it.
 at gml_Object_o_game_Step_0 (line 30) - if checkInput.pause_pressed_ {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_o_game_Step_0 (line 30)
 

dialgpalkia

Member
Well first of all whatever line of code that is doesn't seem to recognise whatever variable <unknown variable> is, so perhaps there is an incompatibility with something in the rest of your code? For example if you inventory checks, say, for the position of varHealth, the game sees that as oPlayer2.varHealth and when you delete oPlayer2, it uses oEnemy2.varHealth, etc. Check to make sure the input code doesn't re-use variables.
 
If you knew checkInput is a script, why are you using it like an object or instance?
Because it works throughout the rest of the code.

Well first of all whatever line of code that is doesn't seem to recognise whatever variable <unknown variable> is, so perhaps there is an incompatibility with something in the rest of your code? For example if you inventory checks, say, for the position of varHealth, the game sees that as oPlayer2.varHealth and when you delete oPlayer2, it uses oEnemy2.varHealth, etc. Check to make sure the input code doesn't re-use variables.
Thanks, yeah, I'll check for that. The thing is, it doesn't give a "oPlayer1" error. Just "oPlayer2" for some reason.
 

TheouAegis

Member
As FrostyCat said...

Variable oPlayer2.<unknown variable>(100012, -2147483648)
if checkInput.pause_pressed_
So the first line of the error refers to oPlayer2 and the second line, the one that is a direct quote of the code causing the error, doesn't mention oPlayer2 at all. So clearly oPlayer2 is not the problem. What is the problem is, as FC said, you are using a script ID as a reference point for the variable pause_pressed_. You said yourself checkInput is a script; scripts do not hold any values other than their own personal id. So what the error is saying is that script checkInput has the same resource ID as the object oPlayer2. When you deleted oPlayer2, the object oEnemy1 then had the same resource ID as the script checkInput.

So as FrostyCat was hinting, learn how to use scripts. I'm guessing you have var pause_pressed_ inside that script. What you need to do is have that script return pause_pressed_ and either store the result in a variable or check it directly. Without seeing the rest of your code, it's impossible to say what exactly about scripts in Game Maker that you don't understand.
 
But it doesn't work. You just think it does because you never got an actual error message.
That's the way it has been working until, like I said I added the inventory. In my other game that has the inventory working uses the input manager as an Object and not a Script. The game I'm currently working on gets the majority of it's code from a marketplace asset that has the input manager using a Script. But apparently both code interpretations are not compatible the way they are and I need to change the input manager back to an Object.
 
Last edited:

TheouAegis

Member
If your script uses instance variables instead of local variables (i.e., you forgot to put "var" before all the variables in the script), then whatever object has the same resource ID as that script would be referenced by your code. If object 0 called script 0, then object 0 would hold the variables created by script 0, so you wouldn't get errors. If object 1 called script 0, then object 1 would hold all the variables created by that script and you would get errors because object 0 is the one that would be referenced but it wouldn't have any of the variables. Like I said, just because the program doesn't crash and just because you don't get error messages and just because the results appear to be what you were hoping to accomplish doesn't mean the code works. It's like saying you tell your son to clean his bedroom and his girlfriend does it for him without you knowing -- just because his room is clean doesn't mean he did what you told him to do.

If you can, double check that the asset even used proper variable scoping.
 
Thanks for all the answers, but, I'll probably just be redoing the input manager as an Object. That way I won't have to redo the inventory function much.
 
Top