If anyone knows how to create a Save/Load function, so that players can Save/load games please tell me. In detail.
If anyone knows how to create a Save/Load function, so that players can Save/load games please tell me. In detail.
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.
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).
Here's some pseudo:
Well, that's kind of the idea of it even if it's horrible pseudocode (sorry!).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; }
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.
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.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; } };
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.
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.
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.
Wow that's alot of ideas, thanks guys. I'll be sure to post the game when it's finished.
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