Advertisement

Saving in a game

Started by December 23, 2000 01:12 PM
4 comments, last by mittens 24 years, 1 month ago
I am currently making a text RPG, and I need to know how to save information so the player doesn''t have to go through ten hours of gameplay, to figure out that he/she can''t save their game!!! So, any help would be greatly appreciated! Thanx. ------------------------------ The Shining Knight
I''m assuming that your game is a single player game here... If not, this won''t work so well. I''m also assuming you have a central loop that repeatedly gets input from the user and then processes it. I''ll further assume that the results of the processing depend on both the user''s input, and the current game state.

In order to implement a save game function in such a system, you need to do two things... First, you need a function that writes the current game state to a file. Depending on the language you are using, and the format you want the save file in (ASCII vs binary, etc.), this has varying levels of difficulty, although I doubt it would be too bad. Second, you need a function that opens a saved file, and copies all the game state information in it into the current game state in memory, then puts you back into the input/process loop.



Advertisement
I am using C++, and this is a single player game. How would I go about making those functions? Thanks a lot.

------------------------------
The Shining Knight
lets say that you have a struct, an interger, and a char array.
( im sure that you have much more data )....


struct ANYDATA
{
int x, y;
int vx, vy;
};

int variable = 10;
char array[10] = { 0 ,1,2,3, 4, 5, 6 ,7 8, 9 };
ANYDATA anydata;
// fill in the struct.....


// now save the data like this:

FILE *file;

// open the file to be written binary "wb"
if ( (file = fopen ( "saved.dat", "wb" )) != NULL )
{

//*********************************************************
/*
fwrite
Writes data to a stream.

size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

Function Required Header Compatibility
fwrite ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

fwrite returns the number of full items actually written, which may be less than count if an error occurs. Also, if an error occurs, the file-position indicator cannot be determined.

Parameters

buffer

Pointer to data to be written

size

Item size in bytes

count

Maximum number of items to be written

stream

Pointer to FILE structure

Remarks

The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written. If stream is opened in text mode, each carriage return is replaced with a carriage-return – linefeed pair. The replacement has no effect on the return value.

*/
//*******************************************************
// read carefully the description of the function
// i got that from the MSDN, so look the for more info
fwrite ( &anydata, sizeof ( ANYDATA ), 1, file );

// now lets save the interger
fwrite ( &variable, sizeof(int), 1, file );

// now lets save the array
fwrite ( array, sizeof ( char ), 10, file );


// now close the file

fclose ( file );
} // end if



// now saved.dat (witch could be anything you want like hdgd.hdg)
// has the data you saved.
// now lets see how to load it back to RAM
// you''ll need a struct of the same type, and interger, and an
// array of the same dimensions

FILE* file;
ANYDATA anydata;
int variable;
char* ptrBuff = NULL;


// allocate the memory for the array
ptrBuff = new char[10];

// open the file to be read binary "rb"
if ( ( file = fopen ( "saved.dat", "rb" ) ) != NULL )
{

// ****************************************************
/*
fread
Reads data from a stream.

size_t fread( void *buffer, size_t size, size_t count, FILE *stream );

Function Required Header Compatibility
fread ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged.

Parameters

buffer

Storage location for data

size

Item size in bytes

count

Maximum number of items to be read

stream

Pointer to FILE structure

Remarks

The fread function reads up to count items of size bytes from the input stream and stores them in buffer. The file pointer associated with stream (if there is one) is increased by the number of bytes actually read. If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters. The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs. The value of a partially read item cannot be determined.
*/
// ********************************************************

// to make your life easier, load them, the same way to saved
// them ( the same order ).
// first load the struct
fread ( &anydata, sizeof(ANYDATA), 1, file );

// load the interger
fread ( &variable, sizeof (int ), 1, file );

// load the array
fread ( ptrBuff, sizeof (char), 10, file );


fclose ( file );
} // end if

















Thanks a lot. That has helped a lot!!!

------------------------------
The Shining Knight
I would suggest just letting the user save their character after they create it. Then just add a little bit more and a little bit more as you go along. Play the game and everytime you do something in the game decide whether that is something that needs to be saved between sessions. If so then add that into the save game. I would suggest using fprintf/fscanf or a text stream starting out. That way you can easily look at what got written to the file as well as creating test files. If you are unfamilar with file I/O then write a test app and play with it before putting the code into your game. Once you have a good feel for what it can and can''t do you might want to play with some idea''s in a text editor to have some idea of where you are headed. The main thing is to make sure the file has a precise meaning and can easily be read using the means you are using to read it.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement