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!
int
is 2147483648, so the fact that one of your indices is coming up -2147483648 suggests you might have an integer overflow error. \$\endgroup\$