• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Saving/loading? GM[8.0]

Le_Beholder

Member
Anyone have some tips on how I could setup a save/loading system? I know it involves several functions to write my desired variables down into ini files.. but as far as implementation goes..
Step events? For loops?
Is it something like:
If select = true
{
case "Save":
ini_write_file(variable?)
ini_write_real(variable?)
break;
}

I've never attempted a save system before.. been trying to Google around for the ancient GM8.0 articles of more, but alas they are buried.
I only need to save some global variables, stuff like a boss(s) beIng defeated, heart containers gathered(how many and from where.)
What button that unlocks a secret door was pushed, said door being unlocked, the position of the block that pushed the button, what tools the player obtained on his perilous quest, and lastly, game configs like keyboard preferences, music and sound levels..

Ok that's quite a laundry list... But it should be easier since the world I'm building is only in one room, I think.
Any ideas? Links to old posts or videos? Would greatly appreciate any help!

Edit: actually I don't have a game configs section made yet, or half of an idea how to do that.. so just help with the other stuff, it's all just a long bridge to get over.
 
S

spoonsinbunnies

Guest
Ill just throw you my save and load scripts I used for my nueral network bots, if you can make everything you want to save into an array this should work fine
First a Save code, keep in mind this is very simple it dosent overwrite old saves, although I'm sure it wouldn't be hard to add using some simple lookup logic,
Code:
save=get_string("Name Save","Save0")//get name to save as
ini_open("Saves.ini")
var w=""
{
    for (var a=0; a<setup a++;)//loop through the array
    {
        w=w+string;(weight[a])//add next part of the array
        w=w+" "; // add space
    }
    ini_write_string("File", save, w);//save to ini file
    ini_close();
}
Now the load code this you will need to alter because mine just readds the variables back to the array, yours you will then have to use the array to reset the variables, but you know the order so it shouldn't be hard,
Code:
// this is a  modified decoder so cant do as much commenting
save=get_string("Name of Save","Save0")//get the name of what your looking for
ini_open("Saves.ini")
var at = 0;
var my_str = ini_read_string("File", save,"save not found")//this looks through the Files section and returns save not found if its not found
var sub_str = "";
var Quote = false;
if my_str!="save not found"
for(var i = 1; i < string_length(my_str)+1; i++)//this goes through the whole line saved on the Files section
{
   var next_char = string_char_at(my_str,i);//spaces are the breaks between vairables, the original file used commas to make something negative it appears as well
   if(next_char == ",")
   {
        Quote = !Quote;
   }
   else if (next_char != " " || Quote) // if not a break
   {
       sub_str = sub_str + next_char;//add it to our string to get the whole variable
   }
   else
   {
       //ADD TO ARRAY
       if(sub_str!="") global.new.weight[at++] = real(sub_str);//if not blank add the variabes into a new array
       sub_str = "";//blank the string to grab the next piece of data
   }
}
ini_close()
This should give you a big chunk of what your looking for, as for overwriting data look up the gamemaker ini functions basically you would just want in the save to delete any that share a name before you write the current file.[/code]
 

Le_Beholder

Member
Ill just throw you my save and load scripts I used for my nueral network bots, if you can make everything you want to save into an array this should work fine
First a Save code, keep in mind this is very simple it dosent overwrite old saves, although I'm sure it wouldn't be hard to add using some simple lookup logic,
Code:
save=get_string("Name Save","Save0")//get name to save as
ini_open("Saves.ini")
var w=""
{
    for (var a=0; a<setup a++;)//loop through the array
    {
        w=w+string;(weight[a])//add next part of the array
        w=w+" "; // add space
    }
    ini_write_string("File", save, w);//save to ini file
    ini_close();
}
Now the load code this you will need to alter because mine just readds the variables back to the array, yours you will then have to use the array to reset the variables, but you know the order so it shouldn't be hard,
Code:
// this is a  modified decoder so cant do as much commenting
save=get_string("Name of Save","Save0")//get the name of what your looking for
ini_open("Saves.ini")
var at = 0;
var my_str = ini_read_string("File", save,"save not found")//this looks through the Files section and returns save not found if its not found
var sub_str = "";
var Quote = false;
if my_str!="save not found"
for(var i = 1; i < string_length(my_str)+1; i++)//this goes through the whole line saved on the Files section
{
   var next_char = string_char_at(my_str,i);//spaces are the breaks between vairables, the original file used commas to make something negative it appears as well
   if(next_char == ",")
   {
        Quote = !Quote;
   }
   else if (next_char != " " || Quote) // if not a break
   {
       sub_str = sub_str + next_char;//add it to our string to get the whole variable
   }
   else
   {
       //ADD TO ARRAY
       if(sub_str!="") global.new.weight[at++] = real(sub_str);//if not blank add the variabes into a new array
       sub_str = "";//blank the string to grab the next piece of data
   }
}
ini_close()
This should give you a big chunk of what your looking for, as for overwriting data look up the gamemaker ini functions basically you would just want in the save to delete any that share a name before you write the current file.[/code]
That could work.. but be advised I'm using GM8.0, so some of the stuff in there, I can already tell, needs to be converted. Things like "++"and "for(var i=6, i<a, i++)" tends to act iffy or just break in 8.0's code..
Edit:
Ok, after googling around some more, I've found a working retrofit for gamemaker8.0 when it comes to for loops and increment operators;
Code:
var i;
for (i = 0; i <a; i+=1) 
{ 
myScript(); 
}
I'm going to try that and play around with @spoonsinbunnies 's code. I'll edit it back later when/if it works.
Or most likely when I set up my basic menu system and gui so I can save anything...
 
Last edited:

Le_Beholder

Member
Ok, still need some help here.
I've tried @Desert Dog 's example he linked to because it looked simple enough, but I'm having trouble making some variables show up after I load the save.. bombs count remain the same after I've used them all, it's still drawing 3 heart containers after I've collected a fourth one.. idk.. I can't make a basic Zelda RPG without a functioning save system in place :(
 
H

Homunculus

Guest
Post the code you have so far for the save / load mechanism, we can't really help you if you don't provide the necessary details
 

FrostyCat

Redemption Seeker
Ok, still need some help here.
I've tried @Desert Dog 's example he linked to because it looked simple enough, but I'm having trouble making some variables show up after I load the save.. bombs count remain the same after I've used them all, it's still drawing 3 heart containers after I've collected a fourth one.. idk.. I can't make a basic Zelda RPG without a functioning save system in place :(
The key to loading and saving isn't following any specific tutorial, but having an exact idea about what pieces of information need to be saved. If after loading a save file you get residual values from before loading, that means your loading code is failing to retrieve the saved value, or your saving code is failing to save a value that should be saved.

Put down the keyboard for a moment, you won't need it for now. Get out a sheet of paper, and on it list EVERYTHING in your game that should be saved if the player needs to quit right now. The total number of bombs is one, and so is the number of heart containers and the number of filled ones. Don't add or remove a single line of code until you're completely done. Once your list is complete, go back to your saving routine and make sure everything on your list has corresponding code writing to the file. Then go back to your loading routine and make sure everything on your list also has a corresponding line reading from the file there.

One of the most common mistakes with loading and saving is failing to stop controllers from overriding loaded values with defaults. For example, if you unconditionally set the number of bombs to a default value in a controller's Create event, that line has to go somewhere else that a file-load can bypass, for example to the room where you start a new game from scratch.

You must NEVER copy-and-paste saving code that is specific to another project or hope for a universal solution. Loading and saving code is always tailor-made. A tutorial can only go as far as teaching you how to load and save a single value, and perhaps show you an example with some other game's data model. It's your job to repeat the basic procedure for every single value in YOUR game's data model.
 

RangerX

Member
With GM8 you can also go the very lazy route and use these:

game_save();
game_load();

Those functions with save or load a state of your game, basically saving everything except what's volatile like a data structures. Game states are also valid only with game version they been created with but if you don't plan on updating your game after release or have DLC after release, it should not impact you at all.
 
Top