Save/Load Code
Displaying 1-4 of 4 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Goldengamer111

I would like to allow the player to save the game, and have expanded on the normal "flags" method - splitting it into different groups, called "Regions" (also used for add-ons). However, I would also like the game to save based upon the player's name (can have many save files, as long as the main character in each has a unique name). Does FileOpen create a file if none is present, or does it only work with files already present. If it is the latter, is there any function that would allow me to create a file (and maybe delete it as well)? Also, are there any errors in my code (sorry about the size). GiveChoice is an as-yet-unfinished function that will eventually allow the player to answer a multiple-choice question.
int CheckSaveGame(string SaveFile)
{
string SaveList = ListFilePattern(SaveFile + ".sav");
if (strcmp(SaveList, "") != 0)
{
if (!GiveChoice("There is already a save file with the name '" + SaveFile + "'. Would you like to "
+ "overwrite it?", "Yes", "No")) return 0;
else return 1;
}
else return 1;
}

void SaveGame()
{
int SaveFile = FileOpen(PlayerName + ".sav", FILE_WRITE);
if (!CheckSaveGame(PlayerName)) return;
int i, f;
string s;
f = 0;
for (i = 0; Region[i].Used == 1; i ++)
{
s += Region[i].Name + " ";
f ++;
}
FileWriteString(SaveFile, s);
FileWriteQuad(SaveFile, f);
for (i = 0; Region[i].Used == 1; i ++)
{
FileWriteString(SaveFile, Region[i].Name);
FileWriteString(SaveFile, Region[i].PathName);
FileWriteQuad(SaveFile, Region[i].FlagsUsed);
for (f == 0; f < Region[i].FlagsUsed; f ++)
{
FileWriteQuad(SaveFile, Region[i].Flags[f]);
}
}
FileClose(SaveFile);
}

void LoadGame(string SaveName)
{
string SaveList = ListFilePattern(SaveName + ".sav");
if (strcmp(SaveList, "") == 0)
{
GiveChoice("", "There is currently no save file with the name '" + SaveName + "'. Would you like to "
+ "create one?", "Yes", "No");
return;
}
int SaveFile = FileOpen(PlayerName + ".sav", FILE_READ);
string s, t;
int i, r, f;
for (i = 0; Region[i].Used == 1; i ++)
{
s += Region[i].Name + " ";
}
t = FileReadString(SaveFile);
if (strcmp(left(s, len(t)), t) != 0)
{
Log(chr('\n') + "There has been a problem loading a save file. This will normally happen when you rearrange "
+ "the regions already present in the list in 'RegionList.vc', and then try to run a game saved before"
+ " that change. To make this save file usable again, place the names in the order of the first list "
+ " below. The second is present for comparisens, but should be the order in which the list is "
+ "written.");
Log("The region names in the order that the save file '" + SaveName + "' expects them: " + t);
Log("The region names in the order that the list in 'RegionList.vc' places them :" + s);
GiveChoice("There has been a problem loading this saved game. See 'verge.log' in the folder in which you "
+ "installed this game for more details." + chr('\n') + "Would you like to create a new game?");
return;
}
r = FileReadQuad(SaveFile);
for (i = 0; i < r; i ++)
{
Region[i].Name = FileReadString(SaveFile);
Region[i].PathName = FileReadString(SaveFile);
Region[i].FlagsUsed = FileReadQuad(SaveFile);
for (f = 0; f < Region[i].FlagsUsed; f ++)
{
Region[i].Flags[f] = FileReadQuad(SaveFile);
}
}
FileClose(SaveFile);
}


Finally, I still have a few questions from my previous, apparently dropped, thread (I have solved the problem with the bridge, but the other questions remain).

EDIT: I realized that I had forgotten to link the file in which I created these to the rest of the game. The code should no longer give any errors.

Posted on 2009-06-26 17:34:47 (last edited on 2009-06-26 18:19:26)

pthaloearth

indeed, fileOpen creates a new file when one does not exists.

Posted on 2009-06-27 11:07:50

Goldengamer111

Thanks for clarifying that! I would have responded earlier, but I have just finished a cross-country move with my family, and just got a connection to the internet yesterday.

Posted on 2009-07-10 18:41:21

Goldengamer111

Thanks for clarifying that! I would have responded earlier, but I have just finished a cross-country move with my family, and just got a connection to the internet yesterday.

Posted on 2009-07-10 18:41:22


Displaying 1-4 of 4 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.