[Tutorial+Example] Saving and Loading done EASY!

Learn to use DS Game Maker by Example.

[Tutorial+Example] Saving and Loading done EASY!

Postby Foxi4 » April 27th, 2011, 8:44 pm

First of all, sorry for being so absent lately - I was *really* busy.

I've successfuly prepared a very simple Loading and Saving method for DSGM5, since many people requested one.

It *WILL NOT* work on No$GBA or any other emulator unless you DLDI-Patch the file to a compatible flash cart type, for example the AK2i (at least it SHOULD work when you do so), otherwise the FAT calls will be read as invalid. This procedure is unnecessary for using the file on a real DS, since most carts DLDI-Patch automatically. I tested it with an Acekard 2i and it works just dandy.

In the attached example, pressing up adds to the Value, pressing down substracts from it, pressing A saves the value, pressing B loads it. The .nds creates a save_file.sav on the flashcart in its parent directory upon the first save.

Enjoy.

EDIT: Added the source file with no custom actions
EDIT2: After changing the code into actions I get a truckload of errors that I don't feel like dealing with, so I'll just explain what's what here - it's not that complicated anyways.

Saving and Loading done EASY! - a quick tutorial

Every single game that aspires to be a bit more than a mini-game needs a saving/loading system. Be it for High Scores or the current game state.

Getting one to work is easy for some and hard for the others. This tutorial will show you how to do it the easiest way possible using FAT.

Let's start off by turning FAT lib on. There are 2 ways to do that.

1. You can turn on FAT via Game Settings if you want it initialized throughout the entire game:

Image

2. You can manually initialize FAT by using this code in the Execute Code Action:
Code: Select all
fatInitDefault();

This will initialize FAT with the default buffer size - more than enough to get this thing rolling.

Let's move on to creating something we can save.

For that, it's best to use a structure, so that you can save multiple variables at one time. You can save just 1 variable if you want too using the same method.

In any case, create a Structure Type using the Global Structures menu.

Image

In this menu, you'll be able to create a Structure that'll carry all the variables you'll be using in your game.

Image

When ready, create a Variable of this type to handle the data.

You'll do so using the Global Variables menu.

Image

IMPORTANT NOTICE!

Individual variables placed in the structure have to be called using this format: "StructureName.ItemName", in this case it's "save_data.Item1".

Once this is settled, it's time for the FUN part.

This code will allow you to save. It has all the necessary comments in it to understand what's going on:

Code: Select all
//Create a temporary Var in the FAT system to control saving
FILE* save_file
//Open the file you are saving to or create it if it is not present
save_file=fopen("save_file.sav", "wb");
//Write data into the file
fwrite(&save_data, 1, sizeof(save_data), save_file);
//Close the file
fclose(save_file);


This code allows loading from the saved file if it is present.

Code: Select all
//Create a temporary Var in the FAT filesystem to control the loading
FILE* save_file
//Open the file you're loading from
save_file=fopen("save_file.sav", "rb");
//Read the data within the file, save it into the Struct
fread(&save_data, 1, sizeof(save_data), save_file);
//Close the file
fclose(save_file);


You can put either in the Execute Code action . The comments in the code are unnecessary and can be deleted if they bug you. ;)

Now you're good to go and your app is able to save/load all values from and to a given struct!

If anything needs clarification, feel free to post!

~Foxi4
Attachments
SavingLoading.nds
(308.06 KiB) Downloaded 429 times
SavingLoading.dsgm
(1.35 KiB) Downloaded 525 times
Last edited by Foxi4 on July 17th, 2011, 5:39 am, edited 26 times in total.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1221
Joined: December 31st, 2010, 3:25 am

Re: Saving and Loading done EASY!

Postby dereklarue » April 27th, 2011, 9:11 pm

Nice, The .nds file Works great with my R4. Id perfer using non actions if you release the .DSGM. Maybe make one with and without actions?
www.dereklarue.net
Location location location
dereklarue
 
Posts: 32
Joined: April 7th, 2011, 3:42 pm

Re: Saving and Loading done EASY!

Postby Foxi4 » April 27th, 2011, 10:14 pm

Errata. This MAY be used on an emulator as well, as long as you DLDI-Patch the ROM image to a compatible type, for example AK2i or the R4.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1221
Joined: December 31st, 2010, 3:25 am

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby astroshark » April 30th, 2011, 2:44 am

Thank you so much just what I was looking for!
astroshark
 
Posts: 5
Joined: April 19th, 2011, 7:42 pm

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Nobody » May 22nd, 2011, 11:45 pm

just wondering, what will happen if the game tries to reads a variable that has not yet been saved to the file? I'm guessing it will just ignore it then and do nothing, but i may be wrong.
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Foxi4 » May 22nd, 2011, 11:53 pm

I actually never checked that, but I'm assuming that it will ignore input, since the file won't be present.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1221
Joined: December 31st, 2010, 3:25 am

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Nobody » May 23rd, 2011, 12:08 am

ok, was just wondering encase i wanted to make updates to my games with new things to save. Thought it would be a good thing to think about. Encase it doesn't thought, i have thought about a way to get around this with an in game option.
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Nobody » May 23rd, 2011, 1:00 am

I have a question when it comes to saving the file. when it says fwrite, fread, and fclose; when you say save_file, is that the actual name of the file, or is it just a representative of a save file?
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Foxi4 » May 23rd, 2011, 10:41 pm

It's the actual file being opened/closed/edited via fat.

Code: Select all
save_file=fopen("save_file.sav", "rb or wb");


rb = read = fat opens the file "save_file.sav" and assigns the values to "save_file".
wb = write = fat opens the file "save_file.sav" and saves the values stored in "save_file" in binary within it.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1221
Joined: December 31st, 2010, 3:25 am

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Nobody » May 24th, 2011, 1:27 am

Okay, but i'm doing something wrong in the process. The above works fine, but when i try to adapt it into my own sav file i have problems. I'm trying to get it to save on the default sav file created by my R4 card when it doesn't automatically find a save data file for a game.

Here's my code:
Code: Select all
//Declare a Var in the FAT filesystem to control the loading
FILE* The Living Dead
//Open the file you're loading from
The Living Dead=fopen("The Living Dead.SAV", "rb");
//Read the data within the file, save it into the Struct
fread(&savefile, 4, sizeof(savefile), The Living Dead);
//Close the file
fclose(The Living Dead);

Here's the error
Code: Select all
C:\DSGMTempTheLivingDead7577531>make
Build process start for project "DSGMTempTheLivingDead7577531"...
main.c
In file included from c:/DSGMTempTheLivingDead7577531/source/main.c:6:0:
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h: In function 'recordtrackerC
reate_Event':
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3417:15: error: expected '='
, ',', ';', 'asm' or '__attribute__' before 'Living'
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3417:15: error: 'Living' und
eclared (first use in this function)
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3417:15: note: each undeclar
ed identifier is reported only once for each function it appears in
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3417:22: error: expected ';'
 before 'Dead'
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3419:5: error: 'The' undecla
red (first use in this function)
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3419:9: error: expected ';'
before 'Living'
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3421:47: error: expected ')'
 before 'Living'
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3423:16: error: expected ')'
 before 'Living'
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h: In function 'saverCreate_Ev
ent':
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3441:13: error: expected '='
, ',', ';', 'asm' or '__attribute__' before 'Living'
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3441:13: error: 'Living' und
eclared (first use in this function)
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3441:20: error: expected ';'
 before 'Dead'
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3443:3: error: 'The' undecla
red (first use in this function)
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3443:7: error: expected ';'
before 'Living'
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3445:46: error: expected ')'
 before 'Living'
c:/DSGMTempTheLivingDead7577531/include/GameWorks.h:3447:14: error: expected ')'
 before 'Living'
make[1]: *** [main.o] Error 1
make: *** [build] Error 2

C:\DSGMTempTheLivingDead7577531>pause
Press any key to continue . . .
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Foxi4 » May 24th, 2011, 1:32 am

You cannot use spaces in Variable names and most likely in filenames either.

It's "TheLivingDead.sav" and "TheLivingDead". If you add a space, the compiler thinks you're "done" with the name input and carries on looking for a function or a value.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1221
Joined: December 31st, 2010, 3:25 am

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Nobody » May 24th, 2011, 10:42 pm

i've already tried taking out the spaces before, but it didn't work. I tried it again a few times and now it seems like it's going to work. I guess i did something wrong before. well now i have a question. Well i don't want the game file to include underscores in the name in order to use the same file. Anyways the file saving worked but it messed up the game. :( I'm sure it was just due to the bad coding i put in just to see if it worked.
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Foxi4 » May 25th, 2011, 12:50 am

Nobody wrote:i've already tried taking out the spaces before, but it didn't work. I tried it again a few times and now it seems like it's going to work. I guess i did something wrong before. well now i have a question. Well i don't want the game file to include underscores in the name in order to use the same file. Anyways the file saving worked but it messed up the game. :( I'm sure it was just due to the bad coding i put in just to see if it worked.


You're probably doing something wrong with the code indeed. I'll post this in the most "condensed" way possible.

1. Create a Struct Type. It can have a whatever name, it doesn't matter - you won't be calling the type.
2. Create a Global of said type. In my example it's "Save_Data" but it doesn't have to be called that way at all!
3. In the saving code, use the Global you've created, but you can name the save file anyway you want. For example, you could be saving a Save_Data structure into "TheLivingDead.sav", the names don't have to match at all. All that matters is that fopen opens the right file at the beginning of the code!

Code: Select all
//Create a temporary Var in the FAT system to control saving
FILE* save_file
//Open the file you are saving to or create it if it is not present
save_file=fopen("The Living Dead.sav", "wb");
//Write data into the file
fwrite(&save_data, 1, sizeof(save_data), save_file);
//Close the file
fclose(save_file);


Note the word *temporary var*. It's there only to store the information from the struct or the file when loading/saving. It's not visible anywhere (in the example, this temp var is called save_file. You can rename it to anything you want, but you have to rename all instances of save_file in the code)! Neither is the name of the struct (in the example it's save_data. If your struct has a different name, input it instead of save_data)!

The only thing the user will be able to "see" is the save file name.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1221
Joined: December 31st, 2010, 3:25 am

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Nobody » May 25th, 2011, 11:51 am

So when your saving, your saving the struct, and not the variable? If that's what you mean, then that might be what caused the glitch.
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Foxi4 » May 25th, 2011, 3:00 pm

Nobody wrote:So when your saving, your saving the struct, and not the variable? If that's what you mean, then that might be what caused the glitch.


You're saving all the variables defined in the struct type.

The glitch happened because you defined a FILE slot that wasn't the same with what you wrote later. You used FILE* The Living Dead, which wouldn't work anyways since it has spaces and then tried to save into save_file which didn't exist.

Code: Select all
//Declare a Var in the FAT filesystem to control the loading
FILE* The Living Dead  <--- Should be "TheLivingDead", for example
//Open the file you're loading from
TheLivingDead=fopen("The Living Dead.SAV", "rb");
//Read the data within the file, save it into the Struct
fread(&savefile, 4, sizeof(savefile), TheLivingDead); <--- The struct name is savefile then?
//Close the file
fclose(TheLivingDead);


That'd work.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1221
Joined: December 31st, 2010, 3:25 am

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Mudbud » May 25th, 2011, 4:12 pm

This is really cool! I am going to use this for mah game now.. Thanks for the tut :mrgreen:
Mudbud
 
Posts: 3
Joined: May 23rd, 2011, 3:24 am

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Nobody » May 25th, 2011, 8:04 pm

I knew the "The Living Dead" wouldn't work. So in my last post i used "The_Living_Dead.SAV" as the file name, an only saved the variable, and not the structure. I'll try again later today
Feel free to ask me for help. I'm on the forum a lot.

Recent Projects:
Retro Ducks: viewtopic.php?f=13&t=3533 (latest)
The Living Dead 2: viewtopic.php?f=13&t=3483
Crafters DS (Minecraft 2D Clone!) viewtopic.php?f=13&t=3082
User avatar
Nobody
 
Posts: 2323
Joined: February 14th, 2011, 12:08 am
Location: United States of America

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Foxi4 » May 26th, 2011, 12:20 am

Nobody wrote:I knew the "The Living Dead" wouldn't work. So in my last post i used "The_Living_Dead.SAV" as the file name, an only saved the variable, and not the structure. I'll try again later today


It saves the entire struct, all of the items, I'm 100% certain. Perhaps you're calling the items wrong?
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1221
Joined: December 31st, 2010, 3:25 am

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby shoyru_master_11 » June 29th, 2011, 11:11 am

Thanks, if this does not corrupt my demo's saving in games that i make, this will come in handy and make use of the "missingfeat" in DSGM
shoyru_master_11
 
Posts: 17
Joined: January 13th, 2011, 8:09 am

Re: [Tutorial+Example] Saving and Loading done EASY!

Postby Foxi4 » June 29th, 2011, 2:09 pm

YoshiInAVoid wrote:You can also use:
if (save_file) {
}
To check if it exists or not.


I assumed that you kinda need to start a new game anyways, so it will create the save file/profile regardless, hence I ignored it. But yes, you are correct.

Thanks, if this does not corrupt my demo's saving in games that i make, this will come in handy and make use of the "missingfeat" in DSGM


It's not really a "missing feat", seeing that it's clearly "do-able" in DSGM. It's just not built-in as a function. :P
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1221
Joined: December 31st, 2010, 3:25 am

Next

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests

cron