Advertisement

help with something in my RPG

Started by March 04, 2002 08:47 PM
3 comments, last by hibiki_konzaki 22 years, 8 months ago
I''m writing a text based RPG in DOS based c++, and I need to know how to save the game, and how to end the game. Like the player chooses to exit the game in a menu, how would I do that without finishing main?
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
to save the game, you simply (hah!) have to figure out which variables must be saved to get the game back in the same place as it is now. generally, these include player stats (strength, HP, mana, whatever), equipment & items, et cetera... if you have a world where the player can change things and the changes stay (such as dropping items and they remain there forever, or if they can cut down trees, or anything of that sort) then you will have to store those too. once you figure this out, write a function that saves these variables to a file. you will also need a function to read this file, and put all the variables back... do you know how to do file i/o?
why would you end the game without ending main()? this might be possible, i dunno... but you should structure your game so that when the user wants to quit, main ends... i.e.:
int main(int argc, char* argv)  {  bool AllDone = false;  InitializeGame();  while(!AllDone)    {    OutputStuff();    AllDone = GetUserInput();    };  return 1;  }; 

OutputStuff() would be the function that puts the text in the console window, and GetUserInput() would get the players input, do whatever is supposed to be done, and returns TRUE if the player hit the "quit" key (false otherwise).
or something along those lines anyways...
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Advertisement
Thanks, and no I don''t know file i/o.. What i meant was how do i end it from the main game loop. Like skip everything else in main() and exit the program.
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
    enum { INIT = 0, MAINMENU, PLAY, QUIT };int State = INIT;int NextState;while( State != QUIT ){	switch( State )	{		case INIT:		{			NextState = Init();  // returns MAINMENU always			break;		}		case MAINMENU:		{			NextState = MainMenu();  // can return QUIT or PLAY			break;		}		case PLAY:		{			NextState = Play(); // can return MAINMENU or QUIT			break;		}	}	State = NextState;}    


Mainmenu should display the information and return one of the enumerated values or maybe even other ones that you would define such as LOAD and SAVE.

You'll have to read-up on file input and output, there are many ways of doing it you just have to learn a few and choose which you like best for what you're doing.

~Vendayan

Edited by - Vendayan on March 4, 2002 10:35:13 PM
"Never have a battle of wits with an unarmed man. He will surely attempt to disarm you as well"~Vendayan
C has these wonderful little functions called atexit() and exit().

When calling atexit(), you specify a function that will be called when the program exits. If you call atexit several times, you will stack several such functions, which will be called in reverse order (last in, first out).

And of course, exit() lets you exit your program...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement