Thread: saving/inventory help please

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    178

    saving/inventory help please

    I'm currently making a pretty cool text/adventure rpg game. I'm running into two dilemmas, please keep in mind this is simply just console programming, no win32 or anything. First of all, almost everything is stored in a structure.
    stat.maxhp;
    stat.hp;
    stat.exp;
    etc etc etc;

    and I want the user to be able to save this to a text file to be able to load next time they play, how exactly do you do this.

    I'm also having trouble thinking of a way to handle the inventory. I'm pretty sure a structure is the best way, but that's about all i can come up with. I want the user to be able to access their inventory at any point in the game and be able to see what is in it as long as use it. And it be removed after it is used. Would I have to create a seperate place in the structure for each. ex

    struct inv
    {
    potions;
    numpotions;
    elixir;
    numelixir;
    }
    to store the name as well as the number that they have. And i'dd probably have to create a function to handle each of the items.

    int takepotion()
    int takeelixer()
    etc etc;

    does anyone know a halfway simple way to do this. Thanks.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    When saving variables, you should save them to binary files, not text files.

    1) Make a type of your stuct

    typedef struct
    {
    ...
    }StructType;

    2) Save it using filepointers (C) or fstream (C++)

    This is an example of file pointers:
    Code:
    StructType MyStruct;
    
    ...
    
    FILE* File;
    File=fopen("Save.dat", "wb");
    if(File!=NULL)
    {
       write(&MyStruct, sizeof(StructType), 1, File);
       close(File);
    }
    You basically do the same when reading the file, but use read instead of write
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    >>and I want the user to be able to save this to a text file to be able to load next time they play, how exactly do you do this.

    this is the basic stuff...

    Code:
    #include<fstream.h> //used for file-handling
    
    int main()
    {
    int points,health,armor;
    int indata;
    
    ofstream fout;  //used to open file for writing
    ifstream fin;   //used for reading
    
    fout.open("P1STATS.DAT");
    fout<<points<<"\n"<<health<<"\n"<<armor<<"\n";
    fout.close(); //close the file after work
    
    fin.open("P1STATS.DAT");
    while(!fin.eof())
    {
     fin>>indata;
     cout<<indata<<"\n";
    }
    fin.close();
    
    return 0;
    }

    you could also search/look around about files in c/c++/game boards as it is asked a lot of times.



    >>I'm also having trouble thinking of a way to handle the inventory. I'm pretty sure a structure is the best way...

    structures will be fine. if the data is global then the user can access the data any time he wants.

    But this is basically done by the programming done by you. if you want him to access, then code accordingly else dont put any code where he can access.
    -

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    178

    inventory/saving

    Thanks a lot guys, i'll give it a shot

Popular pages Recent additions subscribe to a feed