[Solved]Do I need a saved file variable?

W

Wild_West

Guest
I'm finally at the last major leg of my game, I just need to make sure that the player can close the game and reload their saved data.
I did this easily in practice of course just to see the mechanic working, but when I moved back into my real game to implement it, I ran into the issue of , I have a global variable to check for erasing saved data, and loading saved data.

global.has_saved_file;

which is initialized to false of course.

So as you can well imagine this creates a problem whenever the game is restarted since all the rooms have to start again and the has_saved_file variable is re initialized, setting it back to false any time the game starts up again.

So this is my script for all the necessary checks before saving and loading, my question is is there a way I can still use the checks I need before I save but without global.has_saved_file ?

Code:
///menu_script()

//works with menu object only

switch(menu_position)
{
   case 0 : {  
                //Start a new game 
                if( global.has_saved_file == false)
                    {room_goto(map_select_room);  exit;}
               
               //Ask if player will overwrite if there's already a saved file 
                if( global.has_saved_file == true)
                { new_game_check = show_question("This will erase any saved progress, are you sure ?"); }
               
               //Choose not to overwrite save
                if(new_game_check == false){ exit; }else
               
               //Overwrite saved file 
                if(new_game_check == true) and ( global.has_saved_file == true )
                                         {
                                            show_message("Starting New Game");
                                            //empty the collected treasures values
                                            for(empty = 0; empty < array_length_1d(global.collection); empty ++)
                                            { global.collection[empty] = ""; }
                                           
                                            //reset all cleared region stages
                                           
                                            //empty columns
                                             for(emptyColumn = 0; 
                                                 emptyColumn < array_height_2d(global.region_cleared);
                                                 emptyColumn ++){
                                                                  //empty rows
                                                                   for(emptyRow = 0; 
                                                                       emptyRow < array_length_2d(global.region_cleared,emptyColumn);
                                                                       emptyRow ++){global.region_cleared[emptyColumn, emptyRow] = false;}
                                                                }
                                            global.has_saved_file = false;
                                            game_restart();
                                          }
                     
   break;   }
   //--------------------------------------------------------------------------------------------------------------------------------------
   case 1 : {
                   load_check = show_question("Load save file ?");
                if(load_check == false){ exit; }else
               
                if(load_check == true) and (global.has_saved_file == false){ show_message("No Data found.."); }
               
                if(load_check == true) and (global.has_saved_file == true)
                                      { 
                                         ini_read_real("Player_info.ini", "West's_Stats", 0);
                                         
                                         west_stats.fortune   = ini_read_real("West's_Stats", "Fortune", west_stats.fortune  );
                                         west_stats.max_hp    = ini_read_real("West's_Stats", "Hp",      west_stats.max_hp   );
                                         west_stats.max_atk   = ini_read_real("West's_Stats", "Atk",     west_stats.max_atk  );
                                         west_stats.max_grd   = ini_read_real("West's_Stats", "Grd",     west_stats.max_grd  );
                                         west_stats.max_stock = ini_read_real("West's_Stats", "Stock",   west_stats.max_stock);
                                         west_stats.max_lan   = ini_read_real("West's_Stats", "LAN",     west_stats.max_lan  );
                                         west_stats.max_magic = ini_read_real("West's_Stats", "Magic",   west_stats.max_magic);
                                         
                                         ini_close();
                                         
                                         show_message("Game Load Successful");
                                         room_goto(map_select_room);
                                      }
   break;   }
   //--------------------------------------------------------------------------------------------------------------------------------------
   case 2 : {    
                 exit_check = show_question("This will turn the game off,
                                             erasing any unsaved progress,
                                             are you sure ?");
              if(exit_check == false){ exit; }else
             
              if(exit_check == true){ game_end(); }
   break;   }
   //---------------------------------------------------------------------------------------------------------------------------------------
   default :    break;
}
 
A

Azure Spectre

Guest
Rather than initializing global.has_saved_file to false, check if the file exists, and set it based on that. I'm not familiar with .ini files personally, but I'm positive there is a way to do it. Perhaps with the file_exists function?
 
W

Wild_West

Guest
Rather than initializing global.has_saved_file to false, check if the file exists, and set it based on that. I'm not familiar with .ini files personally, but I'm positive there is a way to do it. Perhaps with the file_exists function?
Yeah I figured checking for a file first was the way I just didn't know what the functions were since I only ever did super basic ini file tutorials up until now.
I'll look into it.
 
W

Wild_West

Guest
Rather than initializing global.has_saved_file to false, check if the file exists, and set it based on that. I'm not familiar with .ini files personally, but I'm positive there is a way to do it. Perhaps with the file_exists function?
welp gave file_exists a go, but it only seems to work if I actually save while the game is running and then load it back in after quitting to the main menu.

If I close the game outright and then re run it it can't read the ini file's values.
 
A

Azure Spectre

Guest
Hmm... that's interesting. I'll try and test things out tonight and I'll get back to you if I figure anything out.


EDIT:
@Wild_West
Running into similar problems. Again, my first time with these functions. Working on a solution...
 
Last edited by a moderator:
A

Azure Spectre

Guest
Ok, so I've been playing with .ini files for a while here, and they're actually pretty neat. The cool thing about the ini read functions is that they have a default value that is returned if the file doesn't exist. So I made a has_saved slot for checking this exact thing. In my testing, the information was persistent on multiple runs of the game. Here's the saving and loading code I used:
Code:
//scr_save

ini_open("game_settings.ini");
ini_write_real("Settings","Saved",1);
ini_write_real("Settings","Light",global.lighting);
ini_write_real("Settings","Debug",global.debugon);
ini_write_real("Settings","FullScreen",global.fullscreen);
ini_close();
Code:
//scr_load

ini_open("game_settings.ini");
has_saved=ini_read_real("Settings","Saved",0);
global.lighting = ini_read_real("Settings","Light",1); //ini_read_real(section,key,default)
global.debugon = ini_read_real("Settings","Debug",0);
global.fullscreen = ini_read_real("Settings","FullScreen",1);
ini_close();
show_debug_message('Has Saved: '+string(has_saved));
If you can't get it to work that way, there may be a problem elsewhere and I'll need more information to help.
 
Last edited by a moderator:
W

Wild_West

Guest
Okay editing this the second time today lol
Don't know if you saw the previous ones already but you can ignore them now I figured out what I needed.
Apparently you can't just use dot notation to read or write other objects values to a .ini file, so I tied using a with() statement to save the data from the object in question and it worked.

There is still ONE oddity that doesn't seem to make sense. Even though the save, and load works fine now, I still can't see any of the sections, keys or values when I look at the file where they should be stored.
 
Last edited by a moderator:
A

Azure Spectre

Guest
Sorry, I didn't catch your earlier edits, had a busy day. By dot notation do you mean objectName.variable? That should work if you only have one instance of the object in question. If you have more than one, yeah with() is definitely the solution. :D

It should save in your local app data. The path I find it in is: C:\Users\<Username>\AppData\Local\<GameName>. I'm using Windows 7 at the moment. I'm not sure if other versions are different.

I'm glad you got it to work though! :)
 
W

Wild_West

Guest
Sorry, I didn't catch your earlier edits, had a busy day. By dot notation do you mean objectName.variable? That should work if you only have one instance of the object in question. If you have more than one, yeah with() is definitely the solution. :D

It should save in your local app data. The path I find it in is: C:\Users\<Username>\AppData\Local\<GameName>. I'm using Windows 7 at the moment. I'm not sure if other versions are different.

I'm glad you got it to work though! :)
Oh yeah that is where the files went no wonder I couldn't see them, thanks ^
I included the files in the actual cm : S project resources so I figured that should be where they were going but I guess I don't need to.
 
Top