Thread: Save/Load

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    9

    Save/Load

    If anyone knows how to create a Save/Load function, so that players can Save/load games please tell me. In detail.

  2. #2
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    What do you mean? Saving and loading is done by simple file access. You simply open a file for either writing (saving) or reading (loading). Then you read/write data from/to the file. Then you close it.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    8
    I am still new to game programming but I can tell you if you were to make a program that would load variables and settings from a file you should set up a reading engine (or something that can parse the saved file).

  4. #4
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    Here's some pseudo:

    Code:
    void writesave(character)
    {
    ofstream mygamesave("File1.sav" ios::ate);
    mygamesave << character1.getname();
    }
    
    and...
    
    datastructure readsave()
    {
    ifstream mygamedata("File1.sav");
    string thename;
    mygamedata >> thename;
    Character mrdata(thename);
    return datastructure;
    }
    Well, that's kind of the idea of it even if it's horrible pseudocode (sorry!).

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Save the game state - the variables that determine the current state of the game like character position, health, items carrying, etc., etc. Then load this data into the engine when the player wants to load and it should restore the game state to the one on disk. You are just saving a 'snapshot' of how the game was when the player saved the game. Since a game engine is simply the code that renders and plays the game, the data is what you need to save. Do not save engine variables to disk unless they are needed to restore the game state.

    So if you have a 2D shooter you might want to save the following:

    • Player score
    • Player health
    • Remaining lives
    • Number of baddies on screen at time of save
    • Number of sprites on screen at time of save
    • Positions of baddies and sprites on screen at time of save
    • All state information about baddies and sprites on screen at time of save
    • Powerups the player may have had when the game was saved


    Then when you load you simply create an 'object factory' that will create the game state based on the information on disk.


    Code:
    struct point2D
    {
      float fX;
      float fY;
      point2D(float _fX,float _fX):fX(_fX),fY(_fY) {}
    };
    
    
    class Baddie
    {
      point2D m_pPosition;
      point2D m_pVelocity;
      float m_fSpeed;
      point2D m_pCurrentTarget;
      
      public:
        Baddie(void):m_pPosition(0.0f,0.0f),m_pVelocity(0.0f,0.0f),m_fSpeed(0.0f),m_pCurrentTarget(0.0f,0.0f) {}
       
        virtual ~Baddie(void) {}
    
        void CreateFromParams(point2D pPos,point2D pVelocity,float fSpeed,point2D pCurrentTarget)
        {
           m_pPosition=pPos;
           m_pVelocity=pVelocity;
           m_fSpeed=fSpeed;
           m_pCurrentTarget=pCurrentTarget;
        }
    };
    Then in your load function you would load in the parameters for each bad guy, create a new instance of baddie, and call Create() and pass the parameters you loaded from disk.

    Simple example and it can get complex, but it should get you on the right track.
    Last edited by VirtualAce; 05-23-2005 at 09:25 PM.

  6. #6
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Or you could get even crazier. You could write a function for each object that serializes - or creates a chunk of data - that represents the object. Then you simply pass that chunk to the save game system. Loading would simply be the process of reversing that.

    The reason I mention serializing is that C# already supports it if you want to give that a go.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Serializing does not work in certain cases. Even the MFC documentation states that serialization is not beneficial for applications that need to do block writes of data or write lots of structures to disk.

    I would tend more toward non-serialized objects in game programming. The load and save functions only need to know certain things which can be readily passed as parameters and written to disk as a block.

    But it's up to you.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    9
    Wow that's alot of ideas, thanks guys. I'll be sure to post the game when it's finished.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You could serialize to memory and then write the memory to disk.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ save/load
    By XjX in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 02:01 PM
  2. Save/Load for ListBox
    By Brigs76 in forum C Programming
    Replies: 3
    Last Post: 06-16-2005, 07:04 AM
  3. Save/Load function not working :(
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 05-21-2003, 07:02 PM
  4. save/load problem
    By funkydude9 in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2002, 03:22 PM
  5. Replies: 3
    Last Post: 06-04-2002, 06:11 PM