Save/Load Function
Displaying 1-7 of 7 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
M_Dragon

Hi all, it's me again. I'm trying to implement a save/load function that allows the player to save the game, mainly their current map, x and y coordinates, their direction, and all the flags they have activated. This is what I have so far, however, the game crashes when LoadGame() is called upon, yet no error message appears.
In the system.vc:

int posx = entity.x[player];
int posy = entity.y[player];
int posface = entity.face[player];


In savegame.vc:

#define FILE_READ 1
#define FILE_WRITE 2
#define FILE_WRITE_APPEND 3

void SaveGame()
{
	int savefile = FileOpen("save.dat", 2);

	FileWriteQuad(savefile, posx);
	FileWriteQuad(savefile, posy);
	FileWriteQuad(savefile, posface);
	FileWriteString(savefile, curmap.path);

	FileClose(savefile);
}

void LoadGame()
{
	int savefile = FileOpen("save.dat", 1);

	posx = FileReadQuad(savefile);
	posy = FileReadQuad(savefile);
	posface = FileReadQuad(savefile);
	string savemap = FileReadString(savefile);

	FileClose(savefile);

	Map(savemap);
}

Posted on 2015-12-13 01:44:30

Kildorf

Hmmm... there's nothing obviously wrong with your code there. Can you try putting in some logging messages so you can see how far you get in LoadGame() before it crashes?

Posted on 2015-12-20 20:09:55

M_Dragon

I logged messages after each function and it crashes when it tries Map(savemap). However, I think it is because savegame doesn't always record the map file. When I open save.dat, it sometimes has a mapfile, and sometimes doesn't; but I don't understand why.

Also, there is no error message that pops up or is in v3.txt

Posted on 2016-01-06 02:17:05 (last edited on 2016-01-16 19:31:41)

M_Dragon

If this means anything, I was going through v3.txt, and in it was this:

vc/#included/savegame.vc(2) : warning: 'FILE_READ' : macro redefinition
vc/#included/savegame.vc(3) : warning: 'FILE_WRITE' : macro redefinition
vc/#included/savegame.vc(4) : warning: 'FILE_WRITE_APPEND' : macro redefinition

Posted on 2016-01-16 19:30:41

M_Dragon

Here is the final Savegame() / LoadGame() function, if anyone wants to use it. It saves your (x,y) position, the map you were on, and the value of all flags. *NOTE* The number of flags you have is represented by number in the declaration of the while loop.

void SaveGame()
{
	int savefile = FileOpen("save.pcx", FILE_WRITE);

	string x = str(entity.x[player]);
	string y = str(entity.y[player]);

	FileWriteLn(savefile, curmap.path);
	FileWriteLn(savefile, x);
	FileWriteLn(savefile, y);
	int i = 0;
	while(i <= 14)
	{
	FileWriteLn(savefile, str(flag));
	i++;
	}
	

	FileClose(savefile);
}

void LoadGame()
{
	int savefile = FileOpen("save.pcx", FILE_READ);

	FileSeekPos(savefile, 0, SEEK_CUR);
	savemap = FileReadLn(savefile);
	int posx = val(FileReadLn(savefile));
	int posy = val(FileReadLn(savefile));
	int i = 0;
	while(i <= 14)
	{
	flag= val(FileReadLn(savefile));
	i++;
	}
	FileClose(savefile);
	
	mapswitch(savemap, abs((posx/16)), abs((posy/16)), "D", TRANS_FADE);
}

Posted on 2016-03-20 15:05:49

Kildorf

Glad you got it figured out! What was the problem?

(Also sorry for falling off the face of the Earth and never responding to you)

Posted on 2016-04-17 15:30:50 (last edited on 2016-04-17 15:31:14)

M_Dragon

Don't sweat it.

My friend had a look at the code and realized that since the player's x and y coordinates weren't written as strings, the compiler couldn't understand what was wanted (for some reason that we don't know), and so he just put in a few things turning the int x and y values to str, and vice versa when reading.

Posted on 2016-04-24 16:52:25


Displaying 1-7 of 7 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.