Jump to content


Photo

Load and Save 'mechanism'


  • Please log in to reply
16 replies to this topic

#1 siriuszwart

siriuszwart

    GMC Member

  • New Member
  • 13 posts

Posted 29 April 2012 - 08:39 PM

Heya guys,

Im fairly new to the whole coding thing of game maker, but aside from that i am making a program to keep
financial records mostly using global.values, with a save and a load button ofc.. Why do i use game maker
for this? Mostly to train my insight with coding, and for the fun of it. But a problem i ran into; when i finally
got the save and load functions working, i noticed that the loading and saving of files doesn't change the
global.values at all. It doesn't save them, it doesn't load them. And on top of that the loading isn't working
at all anymore. -_-''

The code i use for the save button;
jaartal = get_string("Voer hier het jaartal in","Bijv: 2012");
savegame = jaartal+".sav";
game_save(savegame);

The code for the load button;
jaartal = get_string("Voer hier het jaartal in","Bijv: 2012");
savegame = jaartal+".sav";
game_load(savegame);

/*I also tried it with some variety:*/
jaartall = get_string("Voer hier het jaartal in","Bijv: 2012");
loadgame = jaartall+".sav";
game_load(loadgame);

Ps: When i betatest i create the executable and place it in a folder to properly test it, so it has nothing to
do with the temp folders.

If there is any more additional coding required from me to solve this please ask for it, i will answer as soon
as possible.
Thanks in advance!

Siriuszwart
  • 0

#2 Weird Dragon

Weird Dragon

    GMC Member

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

Posted 29 April 2012 - 09:16 PM

What your code does:
You attempt to get a file name with the get_string function, but if that file name includes a colon, then the colon won't be accepted as part of the file name.

So if you type
jaartal = "Bijv: 2012"

and you try to use that as the file name, the file name will turn out this way: "Bijv"

Anything after the colon will be missing.
Adding an extension won't change it

savegame = jaartal+".sav";
would then be
savegame = "Bijv: 2012.sav";

but the file name will still be turn out to be "Bijv" omitting everything after the colon.

***
And I think that it is not just the filename that suffers but the whole file (game) might not be properly saved because of that colon thus the global variables are not saved.

Edited by Weird Dragon, 29 April 2012 - 09:25 PM.

  • 0

#3 siriuszwart

siriuszwart

    GMC Member

  • New Member
  • 13 posts

Posted 30 April 2012 - 09:29 AM

What your code does:
You attempt to get a file name with the get_string function, but if that file name includes a colon, then the colon won't be accepted as part of the file name.

So if you type
jaartal = "Bijv: 2012"

and you try to use that as the file name, the file name will turn out this way: "Bijv"

Anything after the colon will be missing.
Adding an extension won't change it


I'm sorry i wasn't clear about that: 'Bijv: "2012"' means: 'For example "2012"', i have been saving the files as '2012', '2013' and '2014' to test them (without quote marks).
  • 0

#4 Weird Dragon

Weird Dragon

    GMC Member

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

Posted 30 April 2012 - 10:52 AM

I'm sorry i wasn't clear about that: 'Bijv: "2012"' means: 'For example "2012"', i have been saving the files as '2012', '2013' and '2014' to test them (without quote marks).


Well, the code you posted shows something else.
My former reply was based on the code you posted. It won't work with that code.
  • 0

#5 siriuszwart

siriuszwart

    GMC Member

  • New Member
  • 13 posts

Posted 30 April 2012 - 10:58 AM


I'm sorry i wasn't clear about that: 'Bijv: "2012"' means: 'For example "2012"', i have been saving the files as '2012', '2013' and '2014' to test them (without quote marks).


Well, the code you posted shows something else.
My former reply was based on the code you posted. It won't work with that code.


I changed the code to
jaartal = get_string("Voer hier het jaartal in","");
savegame = jaartal+".sav";
game_save(savegame);

jaartal = get_string("Voer hier het jaartal in","");
savegame = jaartal+".sav";
game_load(savegame);

And it still doesn't work, also ...
jaartall = get_string("Voer hier het jaartal in","");
loadgame = jaartall+".sav";
game_load(loadgame)
... doesn't work at all.

The entire code makes the game/program crash when attempting to load a saved file.
  • 0

#6 Weird Dragon

Weird Dragon

    GMC Member

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

Posted 30 April 2012 - 11:05 AM

I need to go to work now, so I will be back later on, but I will say one thing for now and that is that I suggest you use the get_save_filename and the get_open_filename functions in stead of the get_string function for this purpose.

Quotes from the help file:

get_save_filename(filter,fname) Asks for a filename to save with the given filter. If the user presses Cancel an empy string is returned.

get_open_filename(filter,fname) Asks the player for a filename to open with the given filter. The filter has the form 'name1|mask1|name2|mask2|...'. A mask contains the different options with a semicolon between them. * means any string.


Look at those in the help file. I will be back.
  • 0

#7 Desert Dog

Desert Dog

    GMC Member

  • Global Moderators
  • 6409 posts
  • Version:Unknown

Posted 30 April 2012 - 11:53 AM

From the 'Smart Codes' topic:

CONTRIBUTED BY daman123125: NOTE* Edited by Desert Dog
Better Game Save...
Simple, easy to use Save and Loading system, which can have as many save files as your computer can handle.
Saving code

file = get_save_filename("Game Files|*.sav;*","");
if string_length(file) >0  // if the user presses cancel, then it will return nothing.
{
game_save(file);           // otherwise, save the game!
}



Loading code
file = get_open_filename("Game Files|*.sav;*","");
game_load(file);


  • 0

#8 siriuszwart

siriuszwart

    GMC Member

  • New Member
  • 13 posts

Posted 30 April 2012 - 12:16 PM

I need to go to work now, so I will be back later on, but I will say one thing for now and that is that I suggest you use the get_save_filename and the get_open_filename functions in stead of the get_string function for this purpose.
Look at those in the help file. I will be back.


Too bad, is not working for me, the codes that i tried:
jaartal = get_save_filename(".sav","");
game_save(jaartal);
break
jaartal = get_save_filename(".sav","");
savegame = jaartal+".sav";
game_save(jaartal);
break
And ofc without break.

As for the codes that i tried (also with the load function), the program does make a save file, but crashes afterwards (same result with attempting to load).
  • 0

#9 siriuszwart

siriuszwart

    GMC Member

  • New Member
  • 13 posts

Posted 30 April 2012 - 12:21 PM

From the 'Smart Codes' topic:


CONTRIBUTED BY daman123125: NOTE* Edited by Desert Dog

file = get_save_filename("Game Files|*.sav;*","");
if string_length(file) >0  // if the user presses cancel, then it will return nothing.
{
game_save(file);           // otherwise, save the game!
}
file = get_open_filename("Game Files|*.sav;*","");
game_load(file);


Still crashed when i try to save or load :S
  • 0

#10 Desert Dog

Desert Dog

    GMC Member

  • Global Moderators
  • 6409 posts
  • Version:Unknown

Posted 30 April 2012 - 12:26 PM

Can you test that code in a new, seperate project? And tell me if you can get it work there?

It's odd. What sort of level of data are you saving? you may need to write a custom save..
  • 0

#11 siriuszwart

siriuszwart

    GMC Member

  • New Member
  • 13 posts

Posted 30 April 2012 - 12:49 PM

Can you test that code in a new, seperate project? And tell me if you can get it work there?

It's odd. What sort of level of data are you saving? you may need to write a custom save..


Ahead of ya. Indeed the coding is working in a separate (empty) project, now i am carefully building up the new empty project to my ideas for the one i already made, carefully testing. Will update later!
So thanks for the code, for now it seems to work!
  • 0

#12 Desert Dog

Desert Dog

    GMC Member

  • Global Moderators
  • 6409 posts
  • Version:Unknown

Posted 30 April 2012 - 01:41 PM

if you're using data structures, then those built-in save/load won't do. (although it still shouldn't crash the game..!)
  • 0

#13 siriuszwart

siriuszwart

    GMC Member

  • New Member
  • 13 posts

Posted 30 April 2012 - 02:10 PM

if you're using data structures, then those built-in save/load won't do. (although it still shouldn't crash the game..!)


I was just using global. variables and some normal ones (helloworld = string...).
Anyhow with my second version of the same program all saving is working!! So thank you guys for helping!
  • 0

#14 Desert Dog

Desert Dog

    GMC Member

  • Global Moderators
  • 6409 posts
  • Version:Unknown

Posted 30 April 2012 - 02:16 PM

Odd indeed! Glad it's working. :D
  • 0

#15 Weird Dragon

Weird Dragon

    GMC Member

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

Posted 30 April 2012 - 06:45 PM

I promised that I would be back. Nice to see that the problem is solved.
  • 0

#16 siriuszwart

siriuszwart

    GMC Member

  • New Member
  • 13 posts

Posted 01 May 2012 - 09:03 AM

I promised that I would be back. Nice to see that the problem is solved.


Odd indeed! Glad it's working. :D


Indeed, so thanks for the help! But i did find out what the cause was yesterday eve.

I had an 'START' object with a code that made all the global.variables that i use, and next destroys itself (object_delete()). That was what caused the crash. In my second try to make the program when i tried to load or save it worked fine. But when i did that or went back to the main page that contains the START object the game would still crash. So i went on a search to what the cause might be. My solution was changing the normal STEP event to a GAME START event and remove the object_delete() function. Works like a charm now!

Greetz!
  • 0

#17 Desert Dog

Desert Dog

    GMC Member

  • Global Moderators
  • 6409 posts
  • Version:Unknown

Posted 01 May 2012 - 09:06 AM

don't use object_delete(), use instance_destroy();

object_delete actually removes the object resources..!

edit:thanks for letting us know what the problem was, btw. Not many people do, but I really was curious why it was crashing. Glad to resolve that! :biggrin:

Edited by Desert Dog, 01 May 2012 - 09:07 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users