Jump to content


Photo

File Text functions


  • Please log in to reply
12 replies to this topic

#1 sonosublime

sonosublime

    GMC Member

  • GMC Member
  • 280 posts
  • Version:GM8

Posted 19 April 2012 - 12:43 PM

Hi all,

I am writing a custom save feature, which involves using INIs for saving/loading global variables, and text files for saving/loading arrays.

I am having trouble with loading from a text file. When I try to save/load the value for an array, I have the following code:

//when saving:
lira_array=file_text_open_write('arrays/lira_array')
    file_text_write_real(lira_array, global.lira[1])
file_text_close(lira_array)

//when loading:
lira_array=file_text_open_read('arrays/lira_array')
    global.lira[1]=file_text_read_real(lira_array)
file_text_close(lira_array)

The saving appears to be working fine, but whenever I try to load the value for global.lira[1], the value does not change to what is saved in the TXT file.

Any help would be very much appreciated.

Edited by sonosublime, 19 April 2012 - 12:44 PM.

  • 0

#2 icuurd12b42

icuurd12b42

    Self Formed Sentient

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

Posted 19 April 2012 - 06:53 PM

Is this GM8 by any chance?

Write a new line at the end of each real
  • 0

#3 sonosublime

sonosublime

    GMC Member

  • GMC Member
  • 280 posts
  • Version:GM8

Posted 20 April 2012 - 04:36 AM

No, this is GM7.
What exactly do you mean by "Write a new line at the end of each real"?
  • 0

#4 icuurd12b42

icuurd12b42

    Self Formed Sentient

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

Posted 20 April 2012 - 04:48 AM

file_text_writeln();


but GM7 has no bug related to the lack of missing end of line. Is does however have a disastrous bug for reading reals which if you have that happening and you disabled the error reporting... People using a specific local setting in windows would get a "error reading real" message. open the text file. is there a comma (,) where a decimal point (.) should be in the number?
  • 0

#5 sonosublime

sonosublime

    GMC Member

  • GMC Member
  • 280 posts
  • Version:GM8

Posted 20 April 2012 - 05:20 AM

Ok, I changed the code to:

//when saving:
lira_array=file_text_open_write('arrays/lira_array')
    file_text_write_real(lira_array, global.lira[1])
    file_text_writeln()
    file_text_write_real(lira_array, global.lira[2])
    file_text_writeln()
file_text_close(lira_array)

//where global.lira[1]=2 and global.lira[2]=0

Note, this is just a test, I do plan to save/load the entire array 'global.lira' at once.

But anyway, no there are no commas when I open up the TXT file, it looks like this:

2.0000
0.0000
  • 0

#6 icuurd12b42

icuurd12b42

    Self Formed Sentient

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

Posted 20 April 2012 - 08:19 AM

GM7 gan read real in a single line
without the nl the file couls look like this
2.0000 0.0000

and it would be fine, most efficient.
GM8 broke this.

Anyway, no (,) in the number (2,0000 0,0000)... and it still don't work? try adding an extension to your file name

arrays/lira_array.dat
  • 0

#7 sonosublime

sonosublime

    GMC Member

  • GMC Member
  • 280 posts
  • Version:GM8

Posted 20 April 2012 - 11:11 AM

There are no commas, they are always decimal points.

I tried a couple of different extensions (DAT, TXT), but neither of them worked. Whenever I tried to load the text file, the values do not change.

//load arrays:

//lira array:
lira_array=file_text_open_read('arrays/lira_array.txt')
    global.lira[1]=file_text_read_real(lira_array)
file_text_close(lira_array)

Where 'lira_array.txt' looks like this:

2.0000
0.0000
  • 0

#8 icuurd12b42

icuurd12b42

    Self Formed Sentient

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

Posted 20 April 2012 - 06:15 PM

and you are sure the code to load is actually getting processed.

Is in inside an if statement.

If(something)
{
load?
}


use show_message to make sure the program gets to the load code.

global.lira[1]=file_text_read_real(lira_array)
show_message(string(global.lira[1]))
  • 0

#9 sonosublime

sonosublime

    GMC Member

  • GMC Member
  • 280 posts
  • Version:GM8

Posted 21 April 2012 - 04:22 AM

Yes, I am sure it is loading, because I also load values from an INI file along side it.
Here are the exact codes for my object:

Create:
global.room_load=0 //global variables are to be loaded with an INI file

global.lira[5]=0 //arrays are to be loaded with TXT files

Begin Step:
if keyboard_check_pressed(ord('S')) //to save game
{
ini_open('save.ini')
    ini_write_real('oic', 'glo_sho_swo', global.short_sword);
ini_close()

//lira array:
lira_array=file_text_open_write('arrays/lira_array.txt')
    file_text_write_real(lira_array, global.lira[1])
    file_text_write_real(lira_array, global.lira[2])
file_text_close(lira_array)

show_message("Game saved")
}

if keyboard_check_pressed(global.keyboard_l) //to load game
{
    if file_exists('save.ini') //check if INI file exists
    {
        if file_exists('arrays/lira_array.txt') //check if TXT file exists
            {alarm[0]=1}
        else
            {show_message('Save File has been corrupted. Load cannot be completed')}
    }
    else
        {show_message("Save File does not exist")}
}

Alarm[0]
//to actually load the values:

//load from INI:
ini_open('save.ini')
    global.short_sword=ini_read_real('oic', 'glo_sho_swo', 0)
ini_close()

//load arrays:

//lira array:
lira_array=file_text_open_read('arrays/lira_array.txt')
    global.lira=file_text_read_real(lira_array) //to load the entire array
file_text_close(lira_array)

show_message("Game loaded")

Loading/saving from the INI files work fine.

Saving to a TXT file appears to be working fine.
However, I cannot get the loading from the TXT file to work.

Edited by sonosublime, 21 April 2012 - 04:24 AM.

  • 0

#10 sonosublime

sonosublime

    GMC Member

  • GMC Member
  • 280 posts
  • Version:GM8

Posted 23 April 2012 - 05:51 PM

post deleted

Edited by sonosublime, 25 April 2012 - 03:57 AM.

  • 0

#11 sonosublime

sonosublime

    GMC Member

  • GMC Member
  • 280 posts
  • Version:GM8

Posted 25 April 2012 - 03:57 AM

bump
  • 0

#12 ParodyKnaveBob

ParodyKnaveBob

    theUndiscovered

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

Posted 25 April 2012 - 05:02 AM

Yes, I am sure it is loading, because I also load values from an INI file along side it.
Here are the exact codes for my object:

Create:

global.room_load=0 //global variables are to be loaded with an INI file

global.lira[5]=0 //arrays are to be loaded with TXT files

Begin Step:
if keyboard_check_pressed(ord('S')) //to save game
{
ini_open('save.ini')
    ini_write_real('oic', 'glo_sho_swo', global.short_sword);
ini_close()

//lira array:
lira_array=file_text_open_write('arrays/lira_array.txt')
    file_text_write_real(lira_array, global.lira[1])
    file_text_write_real(lira_array, global.lira[2])
file_text_close(lira_array)

show_message("Game saved")
}

if keyboard_check_pressed(global.keyboard_l) //to load game
{
    if file_exists('save.ini') //check if INI file exists
    {
        if file_exists('arrays/lira_array.txt') //check if TXT file exists
            {alarm[0]=1}
        else
            {show_message('Save File has been corrupted. Load cannot be completed')}
    }
    else
        {show_message("Save File does not exist")}
}

Alarm[0]
//to actually load the values:

//load from INI:
ini_open('save.ini')
    global.short_sword=ini_read_real('oic', 'glo_sho_swo', 0)
ini_close()

//load arrays:

//lira array:
lira_array=file_text_open_read('arrays/lira_array.txt')
    global.lira=file_text_read_real(lira_array) //to load the entire array
file_text_close(lira_array)

show_message("Game loaded")

Loading/saving from the INI files work fine.

Saving to a TXT file appears to be working fine.
However, I cannot get the loading from the TXT file to work.


Howdy, sonosublime,

"to load the entire array"? Methinks you expect GML to work like PHP. $;^ ] You can't pass entire arrays around like that. Effectively, GML translated your...

//lira array:
lira_array=file_text_open_read('arrays/lira_array.txt')
    global.lira=file_text_read_real(lira_array) //to load the entire array
file_text_close(lira_array)
...into...

//lira array:
lira_array=file_text_open_read('arrays/lira_array.txt');
    global.lira[0]=file_text_read_real(lira_array); //load the FIRST REAL value into global.lira[0] -- the first index of the global.lira array
file_text_close(lira_array);
See the problem?

In GML, every variable you make is actually an array, and it just defaults to index 0 when you leave that off.

I hope this helps, ~nodnod~
  • 0

#13 sonosublime

sonosublime

    GMC Member

  • GMC Member
  • 280 posts
  • Version:GM8

Posted 30 April 2012 - 04:17 AM


Yes, I am sure it is loading, because I also load values from an INI file along side it.
Here are the exact codes for my object:

Create:

global.room_load=0 //global variables are to be loaded with an INI file

global.lira[5]=0 //arrays are to be loaded with TXT files

Begin Step:
if keyboard_check_pressed(ord('S')) //to save game
{
ini_open('save.ini')
    ini_write_real('oic', 'glo_sho_swo', global.short_sword);
ini_close()

//lira array:
lira_array=file_text_open_write('arrays/lira_array.txt')
    file_text_write_real(lira_array, global.lira[1])
    file_text_write_real(lira_array, global.lira[2])
file_text_close(lira_array)

show_message("Game saved")
}

if keyboard_check_pressed(global.keyboard_l) //to load game
{
    if file_exists('save.ini') //check if INI file exists
    {
        if file_exists('arrays/lira_array.txt') //check if TXT file exists
            {alarm[0]=1}
        else
            {show_message('Save File has been corrupted. Load cannot be completed')}
    }
    else
        {show_message("Save File does not exist")}
}

Alarm[0]
//to actually load the values:

//load from INI:
ini_open('save.ini')
    global.short_sword=ini_read_real('oic', 'glo_sho_swo', 0)
ini_close()

//load arrays:

//lira array:
lira_array=file_text_open_read('arrays/lira_array.txt')
    global.lira=file_text_read_real(lira_array) //to load the entire array
file_text_close(lira_array)

show_message("Game loaded")

Loading/saving from the INI files work fine.

Saving to a TXT file appears to be working fine.
However, I cannot get the loading from the TXT file to work.


Howdy, sonosublime,

"to load the entire array"? Methinks you expect GML to work like PHP. $;^ ] You can't pass entire arrays around like that. Effectively, GML translated your...

//lira array:
lira_array=file_text_open_read('arrays/lira_array.txt')
    global.lira=file_text_read_real(lira_array) //to load the entire array
file_text_close(lira_array)
...into...

//lira array:
lira_array=file_text_open_read('arrays/lira_array.txt');
    global.lira[0]=file_text_read_real(lira_array); //load the FIRST REAL value into global.lira[0] -- the first index of the global.lira array
file_text_close(lira_array);
See the problem?

In GML, every variable you make is actually an array, and it just defaults to index 0 when you leave that off.

I hope this helps, ~nodnod~


Ah ha, I see what you mean. Thanks for pointing that out.
I've finally managed to get the arrays working now. Thanks everyone for the help.
  • 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users