Jump to content


Photo
- - - - -

Custom Save/load System


  • Please log in to reply
11 replies to this topic

#1 Zeddy

Zeddy

    Totally Radical Dude

  • GMC Member
  • 1797 posts
  • Version:GM8

Posted 11 March 2009 - 10:53 PM

  • Title: Custom Saving and Loading
  • Description: The purpose of these scripts is to have a proper way to save and load your game. Sure, the default GM savesystem is decent enough, but it's pretty rare to need to save EVERYTHING there is, so I wrote this thing.
  • GM Version: I used GM7
  • Registered: No, at least I don't think so
  • File Type: zip
  • File Size: 18,6kB
  • File Link: http://www.box.net/shared/2me1ylcf83
Additional Info
I couldn't find any other topics on how to do this so I had to figure it out on my own. At first I did it way too complicated with a switch-statment in a loop checking for every single variable I'd use, but at some point I stumbled over variable_global_set and it blew my mind how simple it was to do the saving and loading with only 20 lines of code and a seperate text-file.

If you credit me for this, then that's cool. If you don't , then I'm fine with that too. I have better things to do than chase after people who had the nerve to use something I put up on the Internet.

Here's the loading script, without commenting for the sake of being neat: (There's commenting in the downloadable file)

var file, value, item;

if file_exists(argument0)
{
file = file_text_open_read(argument0);

while (!file_text_eof(file))
{
item = file_text_read_string(file);
value = real(string_digits(item));
if (item != "")
{variable_global_set(string_letters(item), value);}
file_text_readln(file);
}
file_text_close(file);
return(true);
}
else
{return(false);}


And here's the saving script:
Not that you'll NEED the default-file.

var file, item, value;

if (file_exists("default.sav"))
{
varfile = file_text_open_read("default.sav");
file = file_text_open_write(argument0);

while (!file_text_eof(varfile))
{
item = string_letters((file_text_read_string(varfile)));
value = variable_global_get(item);

if (item != "" && variable_global_exists(item))
{file_text_write_string(file, string(item) + " = " +string(round(value)));}
file_text_writeln(file);
file_text_readln(varfile);
}

file_text_close(varfile);
file_text_close(file);

return(true);
}
else
{ return(false);}


  • 0

#2 JAk HAk

JAk HAk

    sepius fidelis

  • New Member
  • 713 posts
  • Version:GM:HTML5

Posted 12 March 2009 - 09:41 PM

Oh cool. So all of the variables that need saving will be in default file. And then it sets the values of those variables into the new file. Nice work. Neat and simple.

It might be better though if the list of variables was in an array, because if the person accidentally deletes the default file, then he can never save :D. Also, encryption would be nice (one of the handier features of default GM saves).
  • 0

#3 James Barnaby

James Barnaby

    GMC Member

  • New Member
  • 52 posts

Posted 12 March 2009 - 10:38 PM

Even though it's still small, other than encryption, you might be able to just compress it, and in turn make it harder to modify. If someone is desperate enough, they'll just decompile the game (Even though it breaks the EULA of GM - which I disagree with, but you can't disagree with the fact that people do it). In some cases, people want to leave the save files editable though.

Either way, the default.sav could be made internally, avoiding possible deletion, editing, etc.

Still nice and simple, and right to the point though.

Edited by James Barnaby, 12 March 2009 - 10:39 PM.

  • 0

#4 Zeddy

Zeddy

    Totally Radical Dude

  • GMC Member
  • 1797 posts
  • Version:GM8

Posted 15 March 2009 - 10:39 PM

^Basically what he said.

You can just include the default.file with Global Game Settings->Include and I really don't see the point encrypting the save files. If the player would like to cheat and edit their savefile, why shouldn't they be allowed to?

I'm sure you could work an encryption process in there though.
  • 0

#5 wai0001

wai0001

    GMC Member

  • New Member
  • 162 posts

Posted 07 April 2009 - 08:36 AM

Thats great. Ive been looking for this for hours. <_<
  • 0

#6 A saurus1

A saurus1

    GMC Member

  • Sandbox Moderators
  • 1608 posts
  • Version:GM8.1

Posted 07 April 2009 - 09:25 AM

Also, encryption would be nice (one of the handier features of default GM saves).


I believe GM does not encrypt, but simply saves the file as a binary file.
  • 0

#7 Rydiasa

Rydiasa

    GMC Member

  • New Member
  • 1 posts

Posted 10 April 2009 - 09:41 PM

Waiiit can this be used to save things like health and scores too? :)
  • 0

#8 farqumic76

farqumic76

    GMC Member

  • New Member
  • 19 posts

Posted 26 May 2009 - 04:54 PM

this works great on gm7 but i use gm6.1 (tried it on my friends computer, he has gm7), I copied everything into the respective places but it only comes up with "loading failed, WTF!" anyone got any ides? could it be arguments. i dont know how to set those. I really want to use this in my game!
  • 0

#9 Yambam

Yambam

    GMC Member

  • GMC Member
  • 378 posts
  • Version:GM8

Posted 03 June 2009 - 06:41 PM

you can do the same thing with
Scripts
Scripts cannot be changed during the execution of the game. The scripts are part of the game logic. Changing scripts would lead to self-rewriting code which very easily leads to errors. Also there are other ways to achieve this. If you really need to execute a piece of code that is not known at design time (e.g. from a file) you can use the following functions _linenums:0'>ScriptsScripts cannot be changed during the execution of the game. The scripts are part of the game logic. Changing scripts would lead to self-rewriting code which very easily leads to errors. Also there are other ways to achieve this. If you really need to execute a piece of code that is not known at design time (e.g. from a file) you can use the following functions: execute_string(str,arg0,arg1,...) Execute the piece of code in the string str with the indicated arguments.execute_file(fname,arg0,arg1,...) Execute the piece of code in the file with the indicated arguments.Sometimes you want to store a script index in a variable and execute it. For this you can use the following function script_execute(scr,arg0,arg1,...) Execute the script with index scr with the given arguments.

  • 0

#10 Zechikin

Zechikin

    GMC Member

  • New Member
  • 3 posts

Posted 14 December 2011 - 10:42 PM

Thank you! This is exactly what I was looking for! I get it now...
  • 0

#11 TemplarX2

TemplarX2

    GMC Member

  • GMC Member
  • 93 posts

Posted 07 August 2012 - 01:58 PM

Using ini files is better? What your example is missing is how to save and load enemies and their position. I load them as below after going to the room.

ini_open("settings.ini")
i=0
ii=ini_read_real( "icount", "i",0)
while(i<ii+1)
{
instance_create(ini_read_real( "x", string(other.i),0),ini_read_real( "y", string(other.i),0),ini_read_real( "object_index", string(i),0))


i+=1

}

Edited by TemplarX2, 07 August 2012 - 02:00 PM.

  • 0

#12 Sirosky

Sirosky

    GMC Member

  • GMC Member
  • 1332 posts
  • Version:GM8

Posted 08 August 2012 - 08:53 PM

Using ini files is better?


Doubt it, INI files are substantially slower.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users