0
\$\begingroup\$

I am currently working on some map generation for my GMS2 game, and I have run into an issue that I absolutely cannot figure out. (I am very new to GMS2, so bear with me).

I am working on map generation, in which there are 9 rooms, and a piece of land for each room. Each piece of land has the exact same code, along with 2 to 4 paths connecting each room to the next. This is all handled by a persistent object pathManager that I have created. I want to make it so that certain paths connect to certain rooms in a grid-like format, but I have run into an issue.

Here is the code for creation of each room:

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

//Create paths
/*
We must create the paths before the land, because upon creation, the
land does some things with the paths. So we will move the paths to their
proper spots later on down 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 land
var land = oLand1;
instance_create_layer(960, 540, "Land", land);

//Create 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 = bottom;
}
if(instance_exists(oPathBR)){
oPathBR.x = right;
oPathBR.y = bottom;
}

Here is the code for the pathManager:

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

//This finds 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 room is bottom middle
        instance_deactivate_object(oPathBR);
        BR_active = false;
    }

//We see here that we must create the paths in the room FIRST,
//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 issue, because they are currently empty objects.

Now, my issue seems to have something to do with uninitialized variables in the land coding, but I absolutely cannot figure it out. When I run the game, I get the following error:


#

FATAL ERROR in action number 1 of Create Event for object oLand1:

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

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

stack frame is gml_Object_oLand1_Create_0 (line 70) called from - gml_Room_room0_Create (line 17) - instance_create_layer(960, 540, "Land", land);


I've looked at these lines many times, but I cannot see what I'm doing wrong. Can someone tell me? Thank you for your help!

\$\endgroup\$
  • 1
    \$\begingroup\$ The max value of a signed 32 bit int is 2147483648, so the fact that one of your indices is coming up -2147483648 suggests you might have an integer overflow error. \$\endgroup\$ – Pikalek Mar 28 at 14:24
  • \$\begingroup\$ Wow, I wasn't even looking for that as an issue haha! Do you have any idea what could be causing it though? \$\endgroup\$ – Little Boy Blue Mar 28 at 17:11
  • 1
    \$\begingroup\$ I don't notice anything in the code shown that looks suspect. While not the only possible cause, typically this sort of thing happens when you have a loop that doesn't terminate as expected - the loop variable increments past the positive values causing an overflow which blows something up. Try adding an if statement in the loop containing the line that crashes & check to see if it goes past the expected limit. \$\endgroup\$ – Pikalek Mar 29 at 2:54
0
\$\begingroup\$

I haven't located the axact issue in my code; however, I believe I have found the overarching issue of what's going on. I found it here: https://www.reddit.com/r/gamemaker/comments/4jj8vn/referencing_the_object_that_called_instance_create/. I created a solution in which, instead, I create the land objects, and then have the land spawn in the paths. This looks as follows (for the land object coding):

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

//This finds 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
        TL_active = false;
        BL_active = false;
    }
    else if(roomIndex == (sqrt(numRooms) - 1)){//If room is top right corner
        TL_active = false;
        TR_active = false;
    }
    else if(roomIndex == (numRooms - sqrt(numRooms))){//If room is bottom left corner
        BL_active = false;
        BR_active = false;
    }
    else if(roomIndex == (numRooms - 1)){//If room is bottom right corner
        TR_active = false;
        BR_active = false;
    }
    else if(roomIndex > 0 && roomIndex < sqrt(numRooms)){//If room is top middle
        TL_active = false;
    }
    else if(roomIndex % sqrt(numRooms) == 0){//If room is left middle
        BL_active = false;
    }
    else if((roomIndex + 1) % sqrt(numRooms) == 0){//If room is right middle
        TR_active = false;
    }
    else if(roomIndex > (numRooms - sqrt(numRooms)) && roomIndex < numRooms){//If room is bottom middle
        BR_active = false;
    }

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

if(TL_active == true){
    instance_create_layer(left, top, "Path", oPathTL);
    oPathTL.roomConnection = pathManager.roomLibrary[roomIndex - sqrt(numRooms)];
}
if(TR_active == true){
    instance_create_layer(right, top, "Path", oPathTR);
    oPathTR.roomConnection = pathManager.roomLibrary[roomIndex + 1];
}
if(BL_active == true){
    instance_create_layer(left, bottom, "Path", oPathBL);
    oPathBL.roomConnection = pathManager.roomLibrary[roomIndex - 1];
}
if(BR_active == true){
    instance_create_layer(right, bottom, "Path", oPathBR);
    oPathBR.roomConnection = pathManager.roomLibrary[roomIndex + sqrt(numRooms)];
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.