Variable Set on Create Somehow Not Set When First Referenced Elsewhere

tylorlilley

Member
Hi, I am having a strange error. What's more, it's not an error I have ever run into on my mac, but when I run the same exact game code on my windows machine it is fairly frequent. Not sure if that's a red herring or not..

Anyway, in my parent object's create event (obj_spider), I have simply:

GML:
initial_x = x;
initial_y = y;
And in that same object's room start event, I then have:

GML:
x = initial_x;
y = initial_y;
The error being thrown is coming from a child of this parent object (obj_spider). But I have checked and triple checked and that object is calling event_inherited() as the first line in its own Create AND its own Room Start events. The error being thrown is saying that the initial_x variable is not set before reading it, but since create events happen before room_start events, how can this be the case? What's more, is I have other children of this parent object and they never seem to throw this error, even though they would also be inheriting things in the same way. What could be going on here?

Error being Thrown:
GML:
___________________________________________
############################################################################################
ERROR in
action number 1
of Other Event: Room Start
for object obj_enemy:

Variable obj_spider.initial_x(100483, -2147483648) not set before reading it.
at gml_Object_obj_enemy_Other_4
############################################################################################
gml_Object_obj_enemy_Other_4 (line -1)
gml_Object_obj_spider_Other_4 (line -1)
Thanks in advance for any help you can provide.
 
Last edited:

samspade

Member
Have you tried stepping through it in the debugger and watching the variables get set? As side note, objects have a built in xstart and ystart that might do the same thing you're attempting.
 

tylorlilley

Member
Have you tried stepping through it in the debugger and watching the variables get set? As side note, objects have a built in xstart and ystart that might do the same thing you're attempting.
... I did not know about xstart and ystart. Thank you for that! I will try using those instead to see if that fixes things, but it still leaves me concerned that the version I had going was not working.

I have not stepped through it in the debugger to watch the variables get set - I do not know how to do that easily. The problem is that it is inconsistent; it does not happen at the start of every room. Only every so often when entering a room for the first time and that room contains obj_spider objects this happens, but it does not happen every time. I have not gotten it to happen when trying to recreate it myself, only repeatedly when watching someone else playtest my game.
 

tylorlilley

Member
Hmm, after switching to the xstart and ystart variables I have not had this exact issue happen again, but I have had other issues along the same lines. Also, I tried just moving the variable declaration out of the parent object and into the child object before switching to xstart and ystart and I DID still have that issue happen, so I think the inheritance is not the issue here. In fact, here is another example with no inheritance at all:

On create, this obj_bush object simply sets occupied = false;

And it reads from it in its step event. Which should only ever happen after it's create event has already run.

But I just got this error during a testing session (90% of the time in this testing session, this object appeared on screen and had no issues whatsoever).

GML:
___________________________________________
############################################################################################
ERROR in
action number 1
of  Step Event0
for object obj_bush:

Variable obj_bush.occupied(100250, -2147483648) not set before reading it.
at gml_Object_obj_bush_Step_0 (line 5) -           (!instance_place(x,y,global.player) && !instance_place(x,y,obj_enemy) && occupied) ||
############################################################################################
gml_Object_obj_bush_Step_0 (line 5)
It's almost as if the creation code is just sometimes not being run for some objects or something. Again, I do not have these errors when running this same project on my mac machine, so I am wondering if this is a bug?
 

tylorlilley

Member
Sure, if you think that will help:

Create Event (obj_bush):
GML:
event_inherited();

depth = -20;
occupied = false;

Step Event (obj_bush):

GML:
if (process_this_frame()) {
    event_inherited();

    if ((instance_place(x,y,global.player) || instance_place(x,y,obj_enemy)) && !occupied) ||
       (!instance_place(x,y,global.player) && !instance_place(x,y,obj_enemy) && occupied) ||
       (instance_place(x,y,obj_enemy) && get_random_chance_out_of(16) && instance_place(x,y,obj_enemy).lethal) {
        rustle_bush();
    }
}
 

samspade

Member
I'd recommend using the debugger and stepping through, it's the only way to know if something like this is a bug or something that's been over looked.

Normally, I would say it isn't a bug, and that is almost certainly the case, but over the weekend I had a very similar occurrence where a variable declared in a create event and used in the step event caused a crash if there was more than one of those instances in the room. When I ran it through the debugger I could verify that the variable was being created and existed in the object until that step event at which point the object lost all of its variables - it just so happened that that was the first variable referenced in the step event. I never figured out what happened. I cleaned the cache several times, rebooted GM, deleted all the objects in the room, changed the variable name and then at some point it just stopped crashing.

It could very well have been my code too and I just never figured out what I was doing, but using the debugger was what gave me at least some confidence that it might not be my code and to try other non-code related solutions.
 

Roldy

Member
An interesting part of the first error you posted is the call stack:

gml_Object_obj_enemy_Other_4 (line -1)
gml_Object_obj_spider_Other_4 (line -1)
It doesn't know the line number of the script being executed.

Like wise the error doesn't report the line number of the offending script"

Variable obj_spider.initial_x(100483, -2147483648) not set before reading it.
at gml_Object_obj_enemy_Other_4 (typically the line number would be reported here)
For obj_spider::room_start event where do you have event_inherited() being called?

Like @samspade suggested, try to nail this down in the debugger. Also try to clean your project.
 

tylorlilley

Member
Thanks for the help and suggestions so far! I really appreciate it. Because it's so intermittent I feel like it will be difficult to step through in the debugger, but I will give it a try sometime this week when I have the time to set aside for doing that.

For the obj_spider code, I am calling event_inherited() first thing. I'll post the entire room start and create events for that object since it seemed like people thought that might help before:

Room Start for obj_spider:
GML:
event_inherited();

lethal = get_random_chance_out_of(2);
if global.controller.entered_from_stairs { lethal = false; }

WAITING = 0;
SCREECHING = 1;
ATTACKING = 2;
state = WAITING;
dir = -1;
Create for obj_spider:

GML:
event_inherited();

killable_by_sword = true;
consume_block = false
consumed_by_block = true;
lethal = get_random_chance_out_of(2);
death_sound = snd_crunch;

WAITING = 0;
SCREECHING = 1;
ATTACKING = 2;
state = WAITING;
dir = -1;
Also, I am not sure what you mean by "try to clean your project". Can you help me understand what that is?

EDIT: Ah, maybe you just mean to run it with a clean build to clear out the cache? I can try that.
 
Last edited:

TailBit

Member
Could you post the 2 events they are inheriting too?

"Clean your project" just click the broom icon, it just flushes the temporary data so that the game have to reconstruct it the next time you run a build (sometimes needed when messing with resources, give it a click when odd stuff happens)
 

tylorlilley

Member
I sure can - I posted them in the first message but they've changed slightly since I learned about xstart and ystart as built in variables.

To be fully transparent in posting this, the obj_spider inherits from obj_enemy, which itself inherits from a more generic obj_death object. My previous post has the obj_spider code exactly, here is the same thing for the other objects:

Enemy create event:
GML:
event_inherited();
lethal = true;
Enemy Room Start event:
GML:
x = xstart;
y = ystart;
Death Object Create event:
GML:
corporeal = true;
stopped_by_special_rosary = false;
killable_by_sword = false;
consume_block = false;
consumed_by_block = false;
death_sound = snd_lose;
lethal = true;
 

chamaeleon

Member
If it was my code, I'd add some show_debug_message() at the beginning of each relevant event code showing what event for what object was executed at some point in time and make sure the order these messages comes up in is the expected order, and that all of them does show up.
 

tylorlilley

Member
After cleaning the project, I am still not getting the xstart or ystart errors. However I AM getting the bush error. I tried adding some show_debug_message() lines and I noticed that whenever the game crashed due to the bush error, I was seeing the following (or something similar to it) in the Output panel:


GML:
Attempting to add instance 105298 multiple times to a layer
Attempting to add instance 105298 multiple times to a layer
Attempting to add instance 105304 multiple times to a layer
Attempting to add instance 105306 multiple times to a layer
Attempting to add instance 105308 multiple times to a layer
Does anybody know what these means or where this could be coming from? Cursory googling of this error message is not turning up any results.
 

Nidoking

Member
Looks like you're either trying to create instances in a loop that's failing to exit, or trying to put something into a layer in a loop that isn't properly setting different instances each time.
 

samspade

Member
After cleaning the project, I am still not getting the xstart or ystart errors. However I AM getting the bush error. I tried adding some show_debug_message() lines and I noticed that whenever the game crashed due to the bush error, I was seeing the following (or something similar to it) in the Output panel:


GML:
Attempting to add instance 105298 multiple times to a layer
Attempting to add instance 105298 multiple times to a layer
Attempting to add instance 105304 multiple times to a layer
Attempting to add instance 105306 multiple times to a layer
Attempting to add instance 105308 multiple times to a layer
Does anybody know what these means or where this could be coming from? Cursory googling of this error message is not turning up any results.
Are you adding it by code or in the room editor? If by code, make sure the layer exists. If in the room editor, then this was actually the second weird error that I got over the weekend. An object I placed in the room editor (not through code) wouldn't spawn in. The other five of them would, and if I added a seventh it would, but the sixth wouldn't. The same error message showed up for me (different number of course). I literally just kept adding and deleting instances until they all showed up. I never figured out why it started or stopped. For me these two errors happened at different times to different objects though. Not much help I know, but you might not be crazy (or maybe we both were/are).
 

tylorlilley

Member
Are you adding it by code or in the room editor? If by code, make sure the layer exists. If in the room editor, then this was actually the second weird error that I got over the weekend. An object I placed in the room editor (not through code) wouldn't spawn in. The other five of them would, and if I added a seventh it would, but the sixth wouldn't. The same error message showed up for me (different number of course). I literally just kept adding and deleting instances until they all showed up. I never figured out why it started or stopped. For me these two errors happened at different times to different objects though. Not much help I know, but you might not be crazy (or maybe we both were/are).
Interesting - these objects are indeed created in the room editor. I will try doing what you suggest (Just deleting and re-adding these objects) and seeing if that fixes anything. Either way thanks for the post commiserating with me on these strange issues - it's very nice to have a stranger on the internet go out of their way to let me feel like I am in the same boat as them! :)
 
Top