I have tiles that each four descriptions for leaving the room in that direction, for example if you walked north from x y, show that description. I am trying to load the descriptions from files in an organized manner. I will be adding try/catch blocks for opening the files at a later point. Is this solution here too cumbersome or is there a better way to go about it?
How much information is in the files? If it's not very much, you could include them directly in the code and you wouldn't have to worry about them being missing or not opening for some reason. Though I guess you might want to be able to change the files without recompiling?
If you don't want to or can't put the files directly in the code, this will work better:
#include "gameworld.h"
#include <fstream>
#include <string>
void gameworld::gameworld ( )
{
setDescription ( "North" );
setDescription ( "East" );
setDescription ( "South" );
setDescription ( "West" );
}
// I made it a bool so you can check if the file opened.
bool gameworld::setDescription ( const std::string & direction )
{
std::ifstream fin ( "DirectionDescriptions/" + direction + ".txt" );
if ( fin ) // Is the file open?
{
std::string input;
unsigned x = 0, y = 0;
while ( getline ( fin, input ) )
{ // I don't know what this function takes, so you probably have a better way to do it.
world[x][y].set_description ( direction, input );
if ( y < boundary ) ++y;
else
{
++x;
y = 0;
}
}
returntrue;
}
returnfalse; // File could not be opened.
}
bool gameworld::setDescription ( const std::string & direction )
{
const std::string file_name = "DirectionDescriptions/" + direction + ".txt";
std::ifstream fin ( file_name );
if ( fin ) // Is the file open?
{
std::string input;
unsigned x = 0, y = 0;
while ( getline ( fin, input ) )
{ // I don't know what this function takes, so you probably have a better way to do it.
world[x][y].set_description ( direction, input );
if ( y < boundary ) ++y;
else
{
++x;
y = 0;
}
}
returntrue;
}
returnfalse; // File could not be opened.
}