Doors to next level not working

conman06

Member
So, I'm working on a platformer. Each level has a door that leads to another room, and sometimes there are locked doors. The problem I'm confused about is why the door won't take me to the next room after the door has been unlocked. There is the object for a locked door (oDoorLocked), the key (oKey), and a regular door. I have a variable for oDoor (target_room) which is set in each oDoor instance, and a global variable for the oDoor which is +1 everytime a key has been collected by the player. The code for oDoorLocked is (Event is Key Press Up):
GML:
if place_meeting(x,y,oPlayer) && global.Key = 1{
image_speed = 0;
with(instance_create_layer(x,y,"Walls",oDoor)){
image_xscale = other.image_xscale;
image_yscale = other.image_yscale;
image_index = 0;
visible = true;
target_room = L1s2};
instance_destroy()};
else sprite_index = sDoorLocked;
It basically tells the new oDoor instance to look the same as the locked door, and tells the game that target_room = L1s2 (the next room in the level.) In case you need it, the rooms are here: https://gyazo.com/79cc55951ec6ed8b61dec0732fefa0a7
In the transition object (oTransition), the code for switching between rooms is in the Animation End event:
Code:
room_goto(oDoor.target_room);
That is activated when the script in oDoor creates an oTransition object in oDoor's Animation End event:
Code:
with(instance_create_layer(x,y,"Transition",oTransition)){
image_xscale = 32;
image_yscale = 32;
image_index = 1;
visible = true;
}
I know that the regular door works. I've tested that out before. I've been trying to fix this for a few days, so I'd be very grateful if someone could help me fix my mistake. If oyu need more info on my game, please feel free to ask. Thanks :)
 
Is the door just not working, or are you getting an error message? Also, is it possible in your game to have more than one key at a time? Because if you can, global.Key will be greater than one, and the code block won't trigger. I also don't see anywhere in the code you've posted where you subtract 1 from that variable once the door is unlocked, which could lead to the same problem if you're adding 1 to the variable when you pick up a key but not subtracting 1 from it when you use it. Either way, I would change global.Key = 1 to either global.Key >= 1 or global.Key > 0.
 

conman06

Member
Is the door just not working, or are you getting an error message? Also, is it possible in your game to have more than one key at a time? Because if you can, global.Key will be greater than one, and the code block won't trigger. I also don't see anywhere in the code you've posted where you subtract 1 from that variable once the door is unlocked, which could lead to the same problem if you're adding 1 to the variable when you pick up a key but not subtracting 1 from it when you use it. Either way, I would change global.Key = 1 to either global.Key >= 1 or global.Key > 0.
Ok, thanks for that. By the way, it doesn't crash. It takes me to the lobby instead of L1s2.
 

Nidoking

Member
How many oDoor are there? Exactly one, or more than one? This includes the one you've created in the oDoorLocked event you posted.
 
This is a good point. If there's more than one instance of oDoor in the room, your room_goto code may not be accessing the target_room variable for the instance you want it to. You'd need to use the instance id instead (which is good practice anyway).
 

conman06

Member
How many oDoor are there? Exactly one, or more than one? This includes the one you've created in the oDoorLocked event you posted.
There are two doors in the room (Level one) one goes to the room before (lobby) and the pther one goes to the next part of the level (L1s2).
 

conman06

Member
This is a good point. If there's more than one instance of oDoor in the room, your room_goto code may not be accessing the target_room variable for the instance you want it to. You'd need to use the instance id instead (which is good practice anyway).
Now that you say this, that does make a lot of sense! Hopefully this works, thanks for explaining it to me.
 
Top