Jump to content


Photo

How "exit" functions in code invoked by "event_inherited()"

gm:studio

  • This topic is locked This topic is locked
4 replies to this topic

#1 pieterator

pieterator

    GMC Member

  • GMC Member
  • 49 posts
  • Version:GM:Studio

Posted 14 February 2016 - 06:37 AM

After some intense head scratching, I think I've figured out why my game was giving me some VERY weird errors.

//Parent Object - Create Event
values = ds_list_create();
values[| 0] = 1;
values[| 1] = 2;
//Parent Object - Step Event
if (x == 0){
    ds_list_destroy(values);
    instance_destroy();
    exit;
}
//Child Object - Step Event
event_inherited();

var val = values[| 0];

When the code is run, you get a "data structure with index does not exists" error (when x == 0).

 

Reason:

"event_inherited()" acts like a script, thus the "exit" only exists the parent's code block and not the child's code block as well, causing the error.

 

The reason for this post is just to confirm whether my reasoning is sound.

 

What the child's code should look like is:

//Child Object - Step Event
var val = values[| 0];

event_inherited();

Just as a side note, if I do it like this:

//Child Object - Step Event
event_inherited();

if (ds_exists(values),ds_type_list)){
    var val = values[| 0];
}

I get intermittent errors of data structures not existing, even though I test whether they exist or not. Also my game uses insane custom data structures of lists containing maps containing lists etc.


  • 0

#2 GameGoblin

GameGoblin

    GMC Member

  • GMC Member
  • 4325 posts
  • Version:GM:Studio

Posted 14 February 2016 - 09:06 AM

That's not the reason. And event_inherited() does exit the code block of the child object. You're getting an error because the following happens:

  • During the first step of x == 0 the data structure is destroyed successfully.
  • But during the second step when x == 0 is still true, the data structure does not exist to get destroyed. Since it does not exist, GameMaker throws an error.

You should always make sure that a data structure exists when you attempt to destroy it in a Step event.

if (x == 0)
{
   if (ds_exists(values, ds_type_list))
   {
      // Destroy the list
   }
}
Is the Create event being inherited properly anyway?

GameGoblin
  • 0

#3 pieterator

pieterator

    GMC Member

  • GMC Member
  • 49 posts
  • Version:GM:Studio

Posted 14 February 2016 - 09:50 AM

Only an instance of the Child Object is created.

So if (x == 0) occurs, the Parent code destroys the object, so there shouldn't be a second step.

 

Also I did another test:

//Parent - Step Event
if (x == 0){
    exit;
}
//Child - Step Event
event_inherited();

var val = 10/x;

This give a division by zero error, which should not happen if the exit in the Parent's code block also causes the Child's code block to exit.


  • 0

#4 xDGameStudios

xDGameStudios

    GMC Member

  • GMC Member
  • 71 posts

Posted 08 March 2016 - 10:34 AM

Hmmm intriguing!! Anyone figured why this is happening? and if so... event_inherited() is not working the way it should (I think).


  • 0

#5 GameGoblin

GameGoblin

    GMC Member

  • GMC Member
  • 4325 posts
  • Version:GM:Studio

Posted 09 March 2016 - 12:54 AM

Hmmm intriguing!! Anyone figured why this is happening? and if so... event_inherited() is not working the way it should (I think).


The OP was correct. It was a misunderstanding on my end.

That happens because exit returns from the current execution context, and event_inherited() creates a new context and executes the inherited event from that. It works basically the same as a script as the OP had stated in the first place.
  • 0





Also tagged with one or more of these keywords: gm:studio