c ++ – Uninitialized Variables with Object Disabling in Game Maker Studio 2

I am currently working on a generation of maps for my GMS2 game, and I am facing a problem that I absolutely can not understand. (I am very new on GMS2, ​​so support me).

I work on card generation, in which there are 9 rooms and one plot for each room. Each parcel has exactly the same code, with 2 to 4 paths linking each room to the next. All this is handled by a persistent object PathManager that I created. I want to make sure that some paths connect to certain rooms in a grid-like format, but I have encountered a problem.

Here is the code for the creation of each piece:

// create pathManager
instance_create_layer (960, 540, "Character", pathManager);

// Create paths
/ *
We must create the paths before the earth, because during the creation, the
the earth does things with the ways. So we will move the paths to their
the appropriate places later in the code.
* /
instance_create_layer (960, 540, "Path", oPathTL);
instance_create_layer (960, 540, "Path", oPathTR);
instance_create_layer (960, 540, "Path", oPathBL);
instance_create_layer (960, 540, "Path", oPathBR);

// Create a terrain
var land = oLand1;
instance_create_layer (960, 540, "Earth", earth);

// Create a character
instance_create_layer (960, 540, "Character", oCharacter);

// move paths
var top = land.y - land.sprite_height / 4;
var bottom = land.y + land.sprite_height / 4;
var left = land.x - land.sprite_width / 4;
var right = land.x + land.sprite_width / 4;

if (instance_exists (oPathTL)) {
oPathTL.x = left;
oPathTL.y = top;
}
if (instance_exists (oPathTR)) {
oPathTR.x = right;
oPathTR.y = top;
}
if (instance_exists (oPathBL)) {
oPathBL.x = left;
oPathBL.y = low;
}
if (instance_exists (oPathBR)) {
oPathBR.x = right;
oPathBR.y = low;
}

Here is the code for the PathManager:

TL_active = true;
TR_active = true;
BL_active = true;
BR_active = true;

// This is what index our room is on the map
roomIndex = 0;
numRooms = pathManager.numRooms;

for (i = 0; i < numRooms; i++){
    if(pathManager.roomLibrary[i] == room){
        roomIndex = i;
        break;
    }
}

//This sets up the path connections of the room
    if(roomIndex == 0){//If room is top left corner
        instance_deactivate_object(oPathTL);
        TL_active = false;
        instance_deactivate_object(oPathBL);
        BL_active = false;
    }
    else if(roomIndex == (sqrt(numRooms) - 1)){//If room is top right corner
        instance_deactivate_object(oPathTL);
        TL_active = false;
        instance_deactivate_object(oPathTR);
        TR_active = false;
    }
    else if(roomIndex == (numRooms - sqrt(numRooms))){//If room is bottom left corner
        instance_deactivate_object(oPathBL);
        BL_active = false;
        instance_deactivate_object(oPathBR);
        BR_active = false;
    }
    else if(roomIndex == (numRooms - 1)){//If room is bottom right corner
        instance_deactivate_object(oPathTR);
        TR_active = false;
        instance_deactivate_object(oPathBR);
        BR_active = false;
    }
    else if(roomIndex > 0 && roomIndex < sqrt(numRooms)){//If room is top middle
        instance_deactivate_object(oPathTL);
        TL_active = false;
    }
    else if(roomIndex % sqrt(numRooms) == 0){//If room is left middle
        instance_deactivate_object(oPathBL);
        BL_active = false;
    }
    else if((roomIndex + 1) % sqrt(numRooms) == 0){//If room is right middle
        instance_deactivate_object(oPathTR);
        TR_active = false;
    }
    else if(roomIndex > (numRooms - sqrt (numRooms)) && roomIndex <numRooms) {// If the room is down in the middle
instance_deactivate_object (oPathBR);
BR_active = false;
}

// We see here that we have to create the paths in the FIRST room,
// this way, we can manipulate where the paths connect, directly from here.
if (TL_active == true) {
with (oPathTL) {
roomConnection = pathManager.roomLibrary[roomIndex - sqrt(numRooms)];
}
}
if (TR_active == true) {
with (oPathTR) {
roomConnection = pathManager.roomLibrary[roomIndex + 1];
}
}
if (BL_active == true) {
with (oPathBL) {
roomConnection = pathManager.roomLibrary[roomIndex - 1];
}
}
if (BR_active == true) {
with (oPathBR) {
roomConnection = pathManager.roomLibrary[roomIndex + sqrt(numRooms)];
}
}

Finally, the paths are named oPathTL, oPathTR, oPathBL and oPathBR. Their code has nothing to do with the problem because they are currently empty objects.

Now, my problem seems to have something to do with the uninitialized variables in the field coding, but I absolutely can not understand it. When I run the game, I get the following error:


#

FATAL ERROR in
action number 1
to create an event
for the object oLand1:

Variable oPathTR.roomIndex (100018, -2147483648) not defined before reading.
at gml_Object_oLand1_Create_0 (line 70) – roomConnection = pathManager.roomLibrary[roomIndex + 1];

### ##########################################

stack frame is
gml_Object_oLand1_Create_0 (line 70)
called since – gml_Room_room0_Create (line 17) – instance_create_layer (960, 540, "Earth", earth);


I have looked at these lines many times, but I do not see what I'm doing wrong. Someone can tell me? Thank you for your help!