Transitioning into wrong rooms

Zapzel-24

Member
Lately I am having some issues with implementing a Room transition Mechanic and it is working fine, it is able to transition from room to room, however when it involves multiple rooms it goes completely haywire.
The problem began like this: I have created a swamp level and a small wooden cabin, when you go to the cabin door you transition to the cabin room. When move further down to the end of the room you transition to another portion of the swamp, instead the player transitions to the cabin even though the creation codes tells the instance to specifically transition to the other room. I tried many other methods such as variable definitions or even global variables but issue is still remains the same.

Instance Creation code:
GML:
//Target Room Coordinates
target_r = rm_SwapOne

//X & Y Coordinates
target_x = 135;
target_y = 150;
Step Event:
GML:
//Transition
if place_meeting(x,y,obj_player) && keyboard_check_pressed(ord("Z")){
    if !instance_exists(obj_fade)
    {
        //Create Fade
        instance_create_layer(x, y,"Instances",obj_fade);
    }
}
//Transfer Coordinates
if instance_exists(obj_fade) && (obj_fade.state = 1)
{
    room_goto(target_r);
    obj_player.x = (target_x);
    obj_player.y = (target_y);
}
Collision to Player:
GML:
//Change Room
if (keyboard_check_pressed(ord("Z")) && !instance_exists(obj_fade)){
    //Target Room
    var _targetRoom = room;
   
    if (_targetRoom == room) _targetRoom = target_r; obj_player.x = target_x; obj_player.y = target_y;
   
    //Fade
    scr_roomTransition(_targetRoom , 30, c_black);
}
Room Transition Scripts:
GML:
///@description
/// @arg room
/// @arg duration
/// @arg color

//Args
var _room = argument0;
var _dur = argument1;
var _color = argument2;

//Create
var _inst = instance_create_depth(0, 0, 0, obj_fade);

//Set Properties
with (_inst){
    targetRoom = _room;
    duration = _dur;
    color = _color;
}
 

chamaeleon

Member
GML:
    var _targetRoom = room;
 
    if (_targetRoom == room) _targetRoom = target_r; obj_player.x = target_x; obj_player.y = target_y;
Two issues here. First you set to the local variable _targetRoom to the current room, then you check if _targetRoom is equal to the current room. Since it is, the if statement is always true and its true value code block will always execute without exception.

Second, your code seem to indicate that you think that maybe all the code on the same line will be part of the if statement code block. It will not. Without braces, only _targetRoom = target_r will be covered. The setting of the x and y coordinates will always happen regardless of what changes you make to the if statement condition, until you add braces to encompass all lines you wish the if statement to make conditional.
 

Zapzel-24

Member
Ok, I found out that my fade script was the culprit since it checks the target room twice. that explains why it keep placing my characters in different spots with coordinates of other doors.
I have deleted it and now it runs perfectly. PS Thanks for the help.
 
Top