Thread: saving a game in console mode

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    saving a game in console mode

    I'm in the process of making a text RPG/adventure game. It will be very tedious to have to start over once you die. That's why I'd like to supply the option to save the game. It's made up of structs mainly, that's the info that would need to be saved. How would I go about saving this info and loading it in the future?

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    well the simplest way i would say is this,

    define a format for the file such as

    File Type Specifier such as SAVE?
    File Size Specifier // type int
    PLYR // a four byte id tag for the data to follow
    Data Size // size of the data to follow
    Name Length // you could put a size here or let a null end it instead
    PlayerName
    Player Data // such as Hp,MP, strength, ect...
    INVN // Inventory Marker?
    Inventory Size
    a continuous group of inventory structs follows

    and write to the file to fit those blue prints then read it out the same way! so a plain text converted read of the file would look like this(not like opening it in notepad! note pad would show the numbers as jibberish characters)

    Code:
    SAVE    // 4 character file id
    60        // 4 byte file size
    PLYR    // 4 character Block id
    15       // 4 byte int holding player data block size
    3         // 4 byte int holding name size
    Bob     // variable size string holding player name 
    40       // 4 byte int holding HP
    30       // 4 byte int holding MP
    INVN   // 4 character block id
    10       // 4 byte int holding  Inventory block size
    5         // 4 byte int holding item name size
    Sword // variable size item name
    
    ect...ect...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    I think he means the functions to save and load save files. I would use sscanf to input the file. It is the easiest and most flexable function to use.
    Code:
    void readstr(FILE *f,char *string)			
    {
       do						
      {
      fgets(string, 255, f); // Gets A String Of 255 Chars Max From f (File)
      } while ((string[0] == '/') || (string[0] == '\n'));// Until End Of Line Is Reached
     return;						
    }
    ............................................
    
    char oneline[255];
      readstr(filein,oneline); // Reads In The Next Line Of Text
      sscanf(oneline, "%f %f %f", &rx, &ry, &rz); // Searches For 3 Floating Point Numbers, Store In rx,ry & rz
    This will scan one line from a binary formated(not ASCII text!) file and assign it to the respective variables. I copied this code from a model loading function in OpenGL, but it does the job. I'm afraid I don't know much about output, though. Hope this helps you!

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    could you perhaps be a little more specific, i've never done anything like it before, im not sure what everything does or anything.

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    me or crossbow?


    if me then

    >could you perhaps be a little more specific,

    about what exactly? how this would work out in code ect?

    >i've never done anything like it before, im not sure what everything does or anything.

    don't worry, took me a while to get it too.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  6. #6
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    here

    Heres the code in a nutshell

    player.h
    Code:
    //if definitions
    
    /* Im gonna use a class just because I don't know the layout of a struct just switch it out. */
    
    class player
    {
    public:
    plaer ();
    ~player ();
    
    protected:
    int life;
    char name[10];
    int age;
    }
    
    #endif
    main01.cpp
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include "player.h"
    
    void save ();
    
    player p;
    
    int main()
    {
    char pick;
    
    cout<<"1. Play"<<endl;
    cout<<"2. Save"<<endl;
    
    switch(pick)
    {
    case '1':
    //play
    break;
    case '2':
    save();
    break;
    }
    
    return 0;
    }
    
    void save()
    {
    
    ofstream save;
    save.open("player.xxx"); //change the xxx to intial of game thingy
    save<<p.life;
    save<<'\n';
    save<<p.age;
    save<<'\n';
    save<<p.name;
    save<<'\n'
    save<<"--------";
    save.close();
    }
    +++
    ++
    + Sekti
    ++
    +++

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    isn't it it if save that way, it will be a text file?

  8. #8
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    yes this will be in a text file [human readable] format.
    -

  9. #9
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Why not overload the input and output operators for the class? That would be much cleaner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-22-2008, 07:20 PM
  2. console game
    By Prophet-ex in forum Game Programming
    Replies: 7
    Last Post: 01-04-2005, 09:41 AM
  3. Console Screen Mode
    By GaPe in forum Windows Programming
    Replies: 9
    Last Post: 02-09-2003, 12:39 PM
  4. How to change a mouse cursor in console mode
    By GaPe in forum Windows Programming
    Replies: 10
    Last Post: 07-03-2002, 07:42 AM
  5. Saving a text game
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2002, 01:33 PM