Jump to content


Photo

Problem with save-mechanism


  • This topic is locked This topic is locked
12 replies to this topic

#1 Plastic

Plastic

    GMC Member

  • New Member
  • 28 posts
  • Version:GM7

Posted 23 February 2012 - 03:27 PM

Hi,
I got an error when I try to use my own save-mechanism. I'm using the build in file functions to save a pattern of dots (purely for testing purposes).
I'm using this code for saving:
{
  var fname, file, nn;
  fname = get_save_filename("files (*.txt)|*.txt","");
  if (fname == "") exit;
  file = file_text_open_write(fname); /* opens a file */
  nn = instance_number(obj_dot); /* number of dots */
  file_text_write_real(file,nn); /* write it to the file */
  with (obj_dot)
  {
    file_text_write_real(file,x); /* writes x-position of dot */
    file_text_write_real(file,y); /* writes y-position of dot */
  }
  file_text_close(file); /* closes file */
}
and I use this code for loading a file:
{
  var fname, file, nn, xx, yy;
  fname = get_open_filename("files (*.txt)|*.txt","");
  if ((fname == "") || !file_exists(fname)) exit; /* if the name is empty or doesn't exist, game maker will crash. Hence this error check */
  with (obj_dot) instance_destroy(); /* remove old dots */
  file = file_text_open_read(fname);
  nn = file_text_read_real(file); /* reads how many dots are saved */
  for (i=0; i<nn; i+=1)
  {
    /* create new dots */
    xx = file_text_read_real(file); 
    yy = file_text_read_real(file);
    instance_create(xx,yy,obj_dot);
  }
  file_text_close(file); /* closes file */
}
but when I use the loadscript, it gives an error saying: error reading real. when I check the file, all positions are there,
so my question is: why do I get this error?

Edited by Plastic, 24 February 2012 - 06:32 PM.

  • 0

#2 stainedofmind

stainedofmind

    GMC Member

  • GMC Member
  • 368 posts
  • Version:GM8

Posted 23 February 2012 - 04:38 PM

It looks like to me the problem is that you're not calling file_text_writeln() after saving a value to file. As I recall, if you don't do this, when you read the info back, it will assume everything to wrote to the file was one line/entry. Also, you have to do the same thing when reading the info back into the game, save you have to use file_text_readln(). So what you'll want is thus:

var fname, file, nn;
  fname = get_save_filename("files (*.txt)|*.txt","");
  if (fname == "") exit;
  file = file_text_open_write(fname); /* opens a file */
  nn = instance_number(obj_dot); /* number of dots */
  file_text_write_real(file,nn); /* write it to the file */
  file_text_writeln();
  with (obj_dot)
  {
    file_text_write_real(file,x); /* writes x-position of dot */
    file_text_writeln();
    file_text_write_real(file,y); /* writes y-position of dot */
    file_text_writeln();
  }

... And then the same for your load script, save for file_text_readln(). Hope this helps!
  • 0

#3 Plastic

Plastic

    GMC Member

  • New Member
  • 28 posts
  • Version:GM7

Posted 23 February 2012 - 06:08 PM

It looks like to me the problem is that you're not calling file_text_writeln() after saving a value to file. As I recall, if you don't do this, when you read the info back, it will assume everything to wrote to the file was one line/entry. Also, you have to do the same thing when reading the info back into the game, save you have to use file_text_readln(). So what you'll want is thus:

var fname, file, nn;
  fname = get_save_filename("files (*.txt)|*.txt","");
  if (fname == "") exit;
  file = file_text_open_write(fname); /* opens a file */
  nn = instance_number(obj_dot); /* number of dots */
  file_text_write_real(file,nn); /* write it to the file */
  file_text_writeln();
  with (obj_dot)
  {
    file_text_write_real(file,x); /* writes x-position of dot */
    file_text_writeln();
    file_text_write_real(file,y); /* writes y-position of dot */
    file_text_writeln();
  }

... And then the same for your load script, save for file_text_readln(). Hope this helps!

Thanks! I'll try that

Edit: Nope, it gives the message: error reading real

Edited by Plastic, 23 February 2012 - 06:11 PM.

  • 0

#4 paul23

paul23

    GMC Member

  • Global Moderators
  • 3387 posts
  • Version:GM8

Posted 23 February 2012 - 06:13 PM

You've got to go in more detail about this:

can you confirm the writing is done as expected - open the file manually to verify -. WHEN is the error reporting (reading the first real, or only at a later stadium)..

Basically what I also see is that you forget to type "file_text_readln()" after reading each real.
  • 0

#5 Plastic

Plastic

    GMC Member

  • New Member
  • 28 posts
  • Version:GM7

Posted 23 February 2012 - 06:18 PM

You've got to go in more detail about this:

can you confirm the writing is done as expected - open the file manually to verify -. WHEN is the error reporting (reading the first real, or only at a later stadium)..

Basically what I also see is that you forget to type "file_text_readln()" after reading each real.

I checked the file and it gives the correct number of dots and all the x and y positions are in the file,
and I already added the file_text_writeln(file) and file_text_readln(file).

Edit: I think I've found what courses the bug: each value in the file has a comma as a seperator.
How do I save a real with a dot as a seperator?

Edited by Plastic, 23 February 2012 - 06:34 PM.

  • 0

#6 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 23 February 2012 - 09:55 PM

Show use the file text you got.

Tell use what version of GM. there are a few problems with write/real real from version to version

gm6-7 write z , instead of a . for 10.5 while the real expects a .
gm8 you cannot wire reals in a single line like you could in 6 and 7
0.0 1.3 1 12.5
  • 0

#7 Plastic

Plastic

    GMC Member

  • New Member
  • 28 posts
  • Version:GM7

Posted 24 February 2012 - 01:56 PM

Show use the file text you got.

Tell use what version of GM. there are a few problems with write/real real from version to version

gm6-7 write z , instead of a . for 10.5 while the real expects a .
gm8 you cannot wire reals in a single line like you could in 6 and 7
0.0 1.3 1 12.5

Here are the numbers I've found in the file:
22,0000
184,0000
176,0000
184,0000
176,0000
184,0000
176,0000
184,0000
176,0000
184,0000
176,0000
184,0000
176,0000
184,0000
176,0000
184,0000
176,0000
192,0000
176,0000
192,0000
176,0000
192,0000
176,0000
192,0000
176,0000
192,0000
176,0000
192,0000
176,0000
192,0000
176,0000
192,0000
176,0000
192,0000
176,0000
192,0000
176,0000
192,0000
176,0000
200,0000
176,0000
200,0000
176,0000
200,0000
176,0000
I'm using :GM7:
  • 0

#8 stainedofmind

stainedofmind

    GMC Member

  • GMC Member
  • 368 posts
  • Version:GM8

Posted 24 February 2012 - 04:16 PM

That's very odd it would save the numbers like that. The reason it keeps throwing the error is because when it sees the "," it thinks that it's a string, not a number. There is a work around you can use, which I'll post, but I would look into why it's replacing your decimal points with commas. The work around is thus:

var fname, file, nn, xx, yy;
  fname = get_open_filename("files (*.txt)|*.txt","");
  if ((fname == "") || !file_exists(fname)) exit; /* if the name is empty or doesn't exist, game maker will crash. Hence this error check */
  with (obj_dot) instance_destroy(); /* remove old dots */
  file = file_text_open_read(fname);
  nn = real(string_replace(file_text_read_string(file), ",", ".")); /* reads how many dots are saved */
  file_text_readln();
  for (i=0; i<nn; i+=1)
  {
    /* create new dots */
    xx = real(string_replace(file_text_read_string(file), ",", ".")); 
    file_text_readln();
    yy = real(string_replace(file_text_read_real(file), ",", "."));
    file_text_readln();
    instance_create(xx,yy,obj_dot);
  }
  file_text_close(file); /* closes file */

This should work, but again, look into why it is saving it like it is.

Edited by stainedofmind, 24 February 2012 - 04:17 PM.

  • 0

#9 Plastic

Plastic

    GMC Member

  • New Member
  • 28 posts
  • Version:GM7

Posted 24 February 2012 - 04:24 PM

That's very odd it would save the numbers like that. The reason it keeps throwing the error is because when it sees the "," it thinks that it's a string, not a number. There is a work around you can use, which I'll post, but I would look into why it's replacing your decimal points with commas. The work around is thus:

var fname, file, nn, xx, yy;
  fname = get_open_filename("files (*.txt)|*.txt","");
  if ((fname == "") || !file_exists(fname)) exit; /* if the name is empty or doesn't exist, game maker will crash. Hence this error check */
  with (obj_dot) instance_destroy(); /* remove old dots */
  file = file_text_open_read(fname);
  nn = real(string_replace(file_text_read_string(file), ",", ".")); /* reads how many dots are saved */
  file_text_readln();
  for (i=0; i<nn; i+=1)
  {
    /* create new dots */
    xx = real(string_replace(file_text_read_string(file), ",", ".")); 
    file_text_readln();
    yy = real(string_replace(file_text_read_real(file), ",", "."));
    file_text_readln();
    instance_create(xx,yy,obj_dot);
  }
  file_text_close(file); /* closes file */

This should work, but again, look into why it is saving it like it is.

Thanks, I'll the code you've provided
Edit: it doesn't work! I tried the code, but it still gives the error message

Edited by Plastic, 24 February 2012 - 04:28 PM.

  • 0

#10 stainedofmind

stainedofmind

    GMC Member

  • GMC Member
  • 368 posts
  • Version:GM8

Posted 24 February 2012 - 05:26 PM

Oops! I forgot to change the yy = ... to say file_text_read_string(). Fix that and try the code again. My mistake!
  • 0

#11 Plastic

Plastic

    GMC Member

  • New Member
  • 28 posts
  • Version:GM7

Posted 24 February 2012 - 06:20 PM

Oops! I forgot to change the yy = ... to say file_text_read_string(). Fix that and try the code again. My mistake!

I already noticed that mistake, but when Game Maker wants to read the first real, it gives the error message
Edit: never mind, I solved it.
I changed the wrong script.

Edited by Plastic, 24 February 2012 - 06:30 PM.

  • 0

#12 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 24 February 2012 - 06:51 PM

All you needed was to use an alternative to file_text_write_real which is the one writing a , instead of . in the file

it's on the forum somewhere.
  • 0

#13 kburkhart84

kburkhart84

    GMC Member

  • GMC Member
  • 1659 posts
  • Version:GM:Studio

Posted 26 February 2012 - 07:36 PM

I'm glad this is solved...but I have a suggestion that I like to use. It may break between versions of GM, and if you add/remove things, it can break, but if everything stays the same, it does not.

You could use a data structure to store things. Example, a ds_grid could be used like a 2d array, where each row contains a "dot" and the first column is the 'x' and the second column is the 'y' and you could add more columns for more things if you need. Then, you the ds_grid_read and ds_grid_write to write/read the whole structure into a single string. Then you can save that string into a text file, either encrypted or not. This comes in handy immensely because you don't have to mess with separating the values, or anything of the sorts, because that is all part of the structures read/write functions. Also, instead of trying to use text to store reals(when you may be better off using a binary file for that) you can store an actual "string", which can easily be encrypted as well. Also, if you have more than one of this type of thing, or other things that need to go into the save file, say a players x/y or something similar, you can easily store all of this into a single file, using separate lines of the text file.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users