# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #1 2009-06-19 20:04:32

flexaplex
Member
Registered: 2008-12-11
Posts: 72

### creation/destroy events not executing after set to move rooms

Brief Description:           Creation/destroy events not executing after set to move rooms
GM Versions:                  GM5, GM6, GM7
References:                    [1] [2]
Example File:                  instance_create bug after room move.gmk
Bug Confirmed:               Yes
Hardware Dependent:    No

Main Info:

This topic came up yesterday and was a nightmare to debug. As known when you tell GM to move/restart rooms or end/restart the game GM marks the function to be done but first executes the entire rest of the code in the event before before carrying out the move. However there is a bug that any instance's created or destroyed after a room change is set to occur will not have it's creation event or destroy event executed.

For example, if you do this:

room_restart();
instance_create(x,y,object0);

An instance of object0 is created and functions exactly the same as normal before the room_restart kicks in, except the bug is causing it's creation event not to be executed. The same bug comes in with destroying or changing instances.

You may argue this is technically not good coding practice so it isn't really a bug, however I believe gml is specifically designed to work after the function calls so I personally think it should be counted.

Demonstration:

This file tests creating, destroying and changing instances after a room_restart is called. You can change the code to move rooms or end the game and you will see the same bug occur:

instance_create bug after room move.gmk

After running the file keep pressing any key to run through the separate tests. Code has been put in the creation/destroy events of the instances to display a message in the dialogue when the event is executed. You can see this message occurs when an instance is created before the room_restart is set but afterwards the messages are not displayed which means the events are not being executed. You can also clearly see that the instances are being created/destroyed normally at each point.

Workarounds:

To avoid this either move the room change code to occur after the instances are created/destroyed or execute the creation and destroy events for instances from a script manually.

If this behaviour is not actually desired and you want the code to exit after a room change function is called then use the key word exit to end the currently read code. If the room change function is called from within a sub-script and thus it's not possible to exit all the currently read code then return a value from the script or set a variable to mark that a room change has been set and then check against this in the currently read code to exit. This is not a very nice way to solve the problem as you will have to continuously check after every script call whether the room change has been marked, however there is really no other alternative method except to just not use sub-scripts altogether.

Last edited by flexaplex (2009-06-21 20:38:57)

Offline

## #2 2009-06-19 20:12:09

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

### Re: creation/destroy events not executing after set to move rooms

I finally found mine that is somewhat related...

http://gmc.yoyogames.com/index.php?showtopic=420152&hl=

Last edited by icuurd12b422 (2009-06-19 20:12:55)

Offline

## #3 2009-06-19 20:23:09

flexaplex
Member
Registered: 2008-12-11
Posts: 72

### Re: creation/destroy events not executing after set to move rooms

I think it is a different issue causing the bug. But it is another example of bug that can cause the creation event to be skipped.

In any case I will be making a separate topic for that bug and the similar one with deactivation I found, I think they might as well be put in the same topic.

Last edited by flexaplex (2009-06-22 19:49:51)

Offline

## #4 2009-06-20 05:32:24

paul23
Member
Registered: 2007-10-17
Posts: 110

### Re: creation/destroy events not executing after set to move rooms

Ah, this also explained the other bug I had...

(game_end not working if there was a game_restart after that event).

Last edited by paul23 (2009-06-20 05:55:17)

Offline

## #5 2009-06-20 09:13:19

flexaplex
Member
Registered: 2008-12-11
Posts: 72

### Re: creation/destroy events not executing after set to move rooms

paul23 wrote:

Ah, this also explained the other bug I had...

(game_end not working if there was a game_restart after that event).

That is not really a bug, it's just how GM handles things. When calling several room/game change functions in a single code execution only the last function is executed and the previous functions are unmarked to be executed. This includes room changes being made after a game end/restart also, the game end/restart is unmarked still and the room change occurs instead.

As for the bug I mentioned I think I have realised a likely cause. When moving rooms normally obviously you want don't want the destroy events for objects to be executed when the instances are removed before changing rooms. So to get around this Mark probably turned off the destroy event to occur when a room change is marked to happen. Although the creation events do not need to be turned off likely the destroy and creation events are interlinked which is why they are both removed (perhaps this is because he simply calls the same function that is used in changing the instance_change perf). Obviously Mark didn't realise the fact that instances could still be destroyed/created after a room change is marked to occur.

Offline

## #6 2009-06-20 16:40:42

paul23
Member
Registered: 2007-10-17
Posts: 110

### Re: creation/destroy events not executing after set to move rooms

Hmm that still bothers me (I've always wondered why destroy events weren't executed when a (non-pesistant)  room was "ended/restarted").. I've always thought as the "creation" event being the "constructor", and the "deconstructor".. And those 2 methods are always called in OO when you're freeing the instances.. (And GM does free an instance/forget it's variables when changing a room). - So I'm sorry, but that "obviously" is startles me a bit.. (I know it is the way it is, but why..)

Also I fail to see any link between creation/destroy event.. (Maybe because I'm too much focussed on trying to give GM OO capabilities).

Also your work-around is very rough/not working:
-If you would use sub-functions that actually call this function "exit" would only stop the current script (same as return 0). So you still have to watch out that no code is put below a function that might use room_goto(). - But I guess that's asking the users of our scripts to "check" if a room_goto() has happened is the best way to go..
(I'm building my own exception routine-script, where an exception shouldn't always result in a "continue" or "abort", but might also be "retry" or "restart". - But since this "bug" prevents a script from ending mid-event, I don't see any good exception handling routine that doesn't require the programmer to constantly check for exceptions.

Last edited by paul23 (2009-06-20 16:47:15)

Offline

## #7 2009-06-20 17:43:20

flexaplex
Member
Registered: 2008-12-11
Posts: 72

### Re: creation/destroy events not executing after set to move rooms

paul23 wrote:

Hmm that still bothers me (I've always wondered why destroy events weren't executed when a (non-pesistant)  room was "ended/restarted").. I've always thought as the "creation" event being the "constructor", and the "deconstructor".. And those 2 methods are always called in OO when you're freeing the instances.. (And GM does free an instance/forget it's variables when changing a room). - So I'm sorry, but that "obviously" is startles me a bit.. (I know it is the way it is, but why..)

An example of where you would not want destroy events to be called when changing rooms would be in a gem game, where you check in the destroy event of a gem whether there is none left or something. You would clearly not want to do things like this when changing rooms manually. There is always the room end event which you can use instead for instances if needed.

Also I fail to see any link between creation/destroy event.. (Maybe because I'm too much focussed on trying to give GM OO capabilities).

I am guessing there exists a routine that turns off both creation and destroy event off for instances together. And I'm guessing they are just done together for ease as I it would be used in both instance_deactivation/activation and instance_change where both are turned off together so Mark then just used the same routine with the room changing. However I could very well be wrong with my guesses, it's just the most logical explanation I can come up with as I don't see any reason why Mark would want to turn off the creation events.

Also your work-around is very rough/not working:
-If you would use sub-functions that actually call this function "exit" would only stop the current script (same as return 0). So you still have to watch out that no code is put below a function that might use room_goto(). - But I guess that's asking the users of our scripts to "check" if a room_goto() has happened is the best way to go..
(I'm building my own exception routine-script, where an exception shouldn't always result in a "continue" or "abort", but might also be "retry" or "restart". - But since this "bug" prevents a script from ending mid-event, I don't see any good exception handling routine that doesn't require the programmer to constantly check for exceptions.

I did say at the end of my post that if you have set the room change in a sub-function you will need to return a value from the script or set a variable to check against. I agree though that the work-around is rough and not very nice at all, the problem is rather annoying.

Note though that this issue comes up normally when using a sub-function, in the very example file I wrote for this I came across the problem. I wanted to check for Esc being pressed within a script so I could exit the main loop however it was not possible without checking it constantly in the main loop so I wound up just leaving it out. The problem does not occur solely with this "bug" but in general and I cannot think of any nice way to solve it.

I also agree that the way gml works by not changing rooms instantly is not good. However it is not a bug because it was clearly designed to work that way for whatever reason. It's often annoying for me when I want to use a game_end(); statement in debugging but I'm presuming there is good reason why it is set up the way it is.

edit:
I have updated my first post to also mention that the workaround is not very nice.

Last edited by flexaplex (2009-06-21 11:42:04)

Offline

## #8 2009-06-20 21:56:40

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

### Re: creation/destroy events not executing after set to move rooms

destruct not called on room end, think persistent objects... Thiink of effects when an instance is destroyed which many people do (bad idea to put effect in the destruct though)

It's pretty standard to me; I set the room end of my parent object to call the destruct...

Offline