I am making a small text based game. my code works if i have it in the main function, but when trying to put it into it's own function it fills my strings with garbage data which eventually causes the program to crash when it tries to read the data.
So here is my question, how should i be passing my 2D array of Room pointers into my function so i can get the same result that i would if i just kept the code in my main function.
The Following is the code currently in my function which will work fine when not in a function.
void InitialiseLevel(int CurrentLevel, Room *Level[2][2]){
if (CurrentLevel == 1){
JumpScareRoom JSRoom1;
Room *room1 = &JSRoom1;
JSRoom1.SetExits("se");
JSRoom1.SetDescriptions("I am a jumpscare room.", "I am a boring room 1");
JSRoom1.SetSouthMessages("There is a door to the SOUTH", "You exit through the SOUTH door.");
JSRoom1.SetEastMessages("There is a door to the EAST", "You exit through the EAST door.");
Room room2;
room2.SetExits("ws");
room2.SetDescriptions("I am a cool room 2", "I am a boring room 2");
Room room3;
room3.SetExits("ne");
room3.SetDescriptions("I am a cool room 3", "I am a boring room 3");
Room room4;
room4.SetExits("wn");
room4.SetDescriptions("I am a cool room 4", "I am a boring room 4");
Level[0][0] = room1;
Level[0][1] = &room2;
Level[1][0] = &room3;
Level[1][1] = &room4;
}
}
my main initializes the array as
Room *Rooms[2][2] = {};
it then calls the function using
InitialiseLevel(1, Rooms); // set up level 1.
I have tried many different methods such as using
void InitialiseLevel(int CurrentLevel, Room **Level[2][2])
and
void InitialiseLevel(int CurrentLevel, Room *(&Level)[2][2])
however i seem to be missing something important.