Game Development Community

File paths in game engine

by Pedro Vicente · in Torque 2D Beginner · 12/14/2013 (11:59 pm) · 5 replies

What is the recommended way to define a path in the C++ engine code, for reading a file?

I don't need exposure to script.

My file setup, using Windows paths as example, is:

"M:/torque2d_dev/project_game/main.cs"

where

"M:/torque2d_dev"

is the root of the game engine

So, this is a bit different than the usual

"M:/torque2d_dev/main.cs"

The file I want to read is located at,

"M:/torque2d_dev/project_game/modules/games/1/files"

This is parallel to the usual scripts and assets

"M:/torque2d_dev/project_game/modules/games/1/scripts"
"M:/torque2d_dev/project_game/modules/games/1/assets"

I have the following code

void read_word_file()
{
  std::ifstream ifs;
  char filename[256];

  const StringTableEntry exePath = Platform::getMainDotCsDir();
  const StringTableEntry cwd = Platform::getCurrentDirectory();

  dSprintf(filename, sizeof(filename), "%s%s", exePath, "/modules/games/1/files/word.txt");

  ifs.open (filename, std::ifstream::in);

#if defined (DEBUG_GAME_SEQUENCE)
  Con::printf("read_word_file (exePath=%s)", exePath); 
  Con::printf("read_word_file (cwd=%s)", cwd); 

  if (ifs.is_open())
  {
    Con::printf("read_word_file ifs.is_open(filename=%s)", filename); 
  }
  else
  {
    Con::printf("read_word_file Cannot open(filename=%s)", filename); 
  }
#endif

In Windows, all works fine

read_word_file (exePath=M:/torque2d_dev/project_game)
read_word_file (cwd=M:/torque2d_dev/project_game)
read_word_file ifs.is_open(filename=M:/torque2d_dev/project_game/modules/games/1/files/word.txt)


but in Android I get


12-14 23:42:35.239: I/Torque2D(15551): [2013/12/14 23:42:35] read_word_file (exePath=)
12-14 23:42:35.239: I/Torque2D(15551): [2013/12/14 23:42:35] read_word_file (cwd=/)
12-14 23:42:35.239: I/Torque2D(15551): [2013/12/14 23:42:35] read_word_file Cannot open(filename=/modules/games/1/files/word.txt)


The call is made at the end of

bool DefaultGame::mainInitialize(int argc, const char **argv)


I tried other options, like exposure to script

const char* file_directory = Con::getVariable( "$GUI::file_directory" ); 
dSprintf(filename, sizeof(filename), "%s/%s", file_directory, "word.txt");

where in script, this is defined at the "create" function of the module named "games"

function games::create( %this )
{
  $Gui::file_directory = expandPath( "./files" );

Thanks



#1
12/15/2013 (7:46 am)
Don't do this:

/modules/games/1/files

Use the expando system:

expandPath("^games/files/");

#2
12/15/2013 (9:24 am)
Thanks, I tried

void read_word_file()
{
  char file_name[1024];
  char path_buffer[1024];
  
  const char* file_directory = "^games/files";
  Con::expandPath( path_buffer, sizeof(path_buffer), file_directory );
  dSprintf(file_name, sizeof(file_name), "%s/%s", path_buffer, "word.lst");

But I still get this path in Android

12-15 09:13:40.589: I/Torque2D(18640): [2013/12/15 09:13:40] read_word_file Cannot open(file_name=/modules/games/1/files/word.lst)

In Windows, I get

read_word_file ifs.is_open(file_name=M:/torque2d_dev/project_game/modules/games/1/files/word.lst)


The file is located at

M:\torque2d_dev\project_game\modules\games\files\word.lst

Note here that there is also a "modules" folder at

M:\torque2d_dev\modules

where all the toys are located, but I am using instead

M:\torque2d_dev\project_game\modules


#3
12/16/2013 (5:35 am)
Sorry, I missed a part. You must use expandPath(...). I updated my first reply with the right code.
#4
12/16/2013 (7:07 am)
Expandos are identified using the ^ symbol. You typically set the variable to a path, then use it later. Whenever a module is registered, an expando is automatically created for it. The variable will always be the ^ followed by the module ID. You should always use expandPath(...) to get a fully qualified file path.
#5
12/16/2013 (10:40 am)
I posted the solution in the android thread. Files in android are read from a zip file, so you cannot use STL file IO functions for it, you have to use the built in android functions for reading the file contents from the zip that I provided.