Archived

This topic is now archived and is closed to further replies.

Xiachunyi

How do you store your game data

Recommended Posts

Hello, I was wondering how to store game data, I''m in the saving/loading phase of my game (until I can get my object selection in OpenGL to act right), and was wondering how to store the data. I was thinking about making my game data as true/false entries e.g. 0''s and 1''s, but wasn''t sure. Example of my thought up saved game file. Name Character array Element An interger Gold An interger that probably will need indication brackets or something of that effect. Mana Same thing as above Influence Same thing as above Spells Single 0''s and 1''s Matthew11.100.100.10.1000000100001110000001000 and so on. What do you think? Hmm... looks almost like an IP address.

Share this post


Link to post
Share on other sites
Um, why would you store it as ascii 1s and 0s? To save a string array followed by, say, a float, do this:

//inputs: char* String.text, int String.length, float TheFloat
FILE* File = fopen( "Test.lvl", "wb" );
fwrite( &String.length, 1, 4, File );
fwrite( String.text, 1, String.length, File );
fwrite( &TheFloat, 1, 4, File );
fclose( File );

To read the data:

FILE* File = fopen( "Test.lvl", "rb" );
fread( &String.length, 1, 4, File );
String.text = new char [String.length+1];
fread( String.text, 1, String.length, File );
String.text[String.length] = 0; // null-terminator
fread( &TheFloat, 1, 4, File );
fclose( File );


~CGameProgrammer( );

Share this post


Link to post
Share on other sites
Xiachunyi, I would suggest to use XML to save your data. Saving your data in a binary format like CGameProgrammer suggests brings some disadvantages, especially while you are in the development progress. Using a human readable format saves you much trouble. XML also offers the possibility to save binary parts, if absolutely necessary. TinyXML is a free, light XML parser that might come handy.

Regards
Matt

Lead programmer at sechsta sinn - http://www.sechsta-sinn.com - mailto:matthias@sechsta-sinn.com

Share this post


Link to post
Share on other sites
I was using this format:

  
int readfile(LPCTSTR pszFileName)
{
char temp, compare;
char storage[1000];
HANDLE hFile;
BOOL bSuccess = FALSE;

hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{

DWORD dwFileSize;

dwFileSize = GetFileSize(hFile, NULL);

if(dwFileSize != 0xFFFFFFFF)
{
DWORD dwRead;
if(ReadFile(hFile, storage, dwFileSize, &dwRead, NULL))
{
//I was thinking of using a for loop with isalpha(), ispunc(), and strcat/strcpy functions.

}

}
CloseHandle(hFile);
}
return 0;
}

Share this post


Link to post
Share on other sites
It appears that what you are asking is how to serialize your game data so that you can save it to disk and then later re-load the game data to obtain the original state of the game... right?

If so, you need to do some research on Abstract Factories and serialization of objects.

I made a post a few days ago that recieved zero responses (*grin*) in which I gave away most of my object serilization... I might add that I have changed that code a bit and if you''re are interested I can give you what I have so far...

Game On!

Dave "Dak Lozar" Loeser

Share this post


Link to post
Share on other sites