Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: TMuh on Sat 13/07/2013 16:21:35

Title: File.ReadInt and WriteInt problems
Post by: TMuh on Sat 13/07/2013 16:21:35
Ok, I have another problem when im trying to save tiledata to a file.

Code (AGS) Select
if (keycode == eKeyF5) {
File *output = File.Open("tiles.dat",eFileWrite);
if (output == null) Display("Error opening file.");
else {
  int tx = 0; int ty = 0;
  while (ty < 15) {
    while (tx < 20) {
      output.WriteInt(tiles[ty].x[tx]);
      tx++;
    }
  ty++;
  }
output.Close();
}
}


if (keycode == eKeyF6) {
File *input = File.Open("tiles.dat",eFileRead);
if (input == null) Display("Error opening file.");
else {
  int tx = 0; int ty = 0;
  while (ty < 15) {
    while (tx < 20) {
      tiles[ty].x[tx] = input.ReadInt();
      tx++;
    }
  ty++;
  }
input.Close();
}
}



It should work that pressing f5 saves tiles[].x[] to tiles.dat and f6 loads the stored data back to tiles[].x[]. But from some reason when I load the stored data, only first row of the data is returned (that means tiles[0].x[from 0 to 20]). Values in tiles[1].x[], tiles[2].x[]... are 0. Code is short and simple so the problem should be easy to fiqure out but i just dont see it.

tiles[].x[] is like this:

Code (AGS) Select
struct TileDataSt {
int x[20]; 
};

TileDataSt tiles[15];
Title: Re: File.ReadInt and WriteInt problems
Post by: Snarky on Sat 13/07/2013 16:44:49
Please use the [code=AGS] tag to format your code. I've edited your post for you.
Title: Re: File.ReadInt and WriteInt problems
Post by: RickJ on Sat 13/07/2013 16:58:38
I don't see it either.  Just put in a couple of Display commands like this ...

Code (ags) Select

if (keycode == eKeyF5) {
File *output = File.Open("tiles.dat",eFileWrite);
if (output == null) Display("Error opening file.");
else {
  int tx = 0; int ty = 0;
  while (ty < 15) {
    while (tx < 20) {
      output.WriteInt(tiles[ty].x[tx]);
      tx++;
      // Try this to see what your code is actually doing
      Display("tiles[%d].x[%d]=%d",ty,tx,tiles[ty].x[tx]);
    }
  ty++;
  }
output.Close();
}
}


Title: Re: File.ReadInt and WriteInt problems
Post by: TMuh on Sat 13/07/2013 17:15:29
Thanks, that helped. I simply forgot to reset "tx" value after each "ty" cycle :)
Title: Re: File.ReadInt and WriteInt problems
Post by: monkey0506 on Sun 14/07/2013 09:21:34
Quote from: RickJ on Sat 13/07/2013 16:58:38Just put in a couple of Display commands

In lieu of a more robust debugger, this is often one of the most useful debugging techniques AGS offers. Ensuring that the code is called, printing variable values, and various other tasks are greatly simplified thanks to this command and usage.