-1

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.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • @πάντα ῥεῖ that answers an underlying issue with the code but does not answer how to pass X*[][] to a function where it is not a copy – NathanOliver Oct 14 '15 at 16:41
  • Thanks for the information, however as i was unaware at the time of posting this that it was the pointers going out of scope causing the problems, the information given on this other question just made me more confused. – Yuri Suzumiya Oct 14 '15 at 17:51
  • @YuriSuzumiya *Don't* update questions with "Solved" or to contain an answer. That is what answers are for (and if no answer is complete, you are free to write your own). – crashmstr Oct 14 '15 at 17:53

1 Answers1

0
JumpScareRoom JSRoom1;

is a local object and when you go out of the function scope, it is invalidated. Instead, you can use:

JumpScareRoom *pJSRoom1 = new JumpScareRoom();
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.");
...
fatihk
  • 7,789
  • 1
  • 26
  • 48