Hi everyone,
I've searched the forum for "save/load", "save/load system", and "file" but haven't found what I'm looking for. I'm looking for info on how to build a save/load system for use in Irrlicht, one that'll save a number of things to a file (for example: health, your position, what you're carrying, your age, etc).
Can anyone help, or point me in the right direction to look if there's already been a post about this on here?
Thanks.
Save/Load System?
supose you have this struct...
for write that you have to...
For loading you have to do the same but with IReadFile and file method.
Code: Select all
struct SPlayer
{
short int Health;
short int Weapon;
};
SPlayer myPlayer;
Code: Select all
irr::io::IWriteFile fwrite = IrrDevice->getFileSystem()->createAndOpenFile(file);
fwrite->write(reinterpret_cast<char *>(&myPlayer), sizeof(SPlayer));
sweet dude, thankz 4 ur example, 2day i started IO by using fstream, i didnt relise that irrlicht had it built into it.
EDIT: just wondering, with fstream you can check if the file is empty, just woondering how this is done through irrlicht, reason being that in my game initalizeation state i check 1st if the option.ini file is empty and if so then load a set of default parameters for window creation.
thankz
EDIT Again
Okay, i playd around with ur code and couldnt get it to work, i modified it a bit and now its workn charms, really good.
heres the save to file feature:
irr::io::IWriteFile *fwrite = device->getFileSystem()->createAndWriteFile("Optsdion.ini", false);
//The false is whether or not you wish to append to the file, ie true, add stuff to the existing file, or false, delete all and rewrite.
fwrite->write(reinterpret_cast<char *> (&Ms), sizeof(player));
next i added incase you want to store stuff to it:
fwrite->write("cher", 4)
//Store a char called "cher", the 4 represents how many characters to save, it also works for u32's ect
Okay what i havent worked out yet is to load the data back to your player, i am trying to implement a featuure where you want to recall the specific data back to ur object "or whatever"
e.g:
psedeuo:
Player->setHealth( type, position);
so u specify wat type you want and its position in the file.
I think that where you have allocated memory and store it in the file is where it saves the relative player data?
thankz again and i hope this helped
EDIT: just wondering, with fstream you can check if the file is empty, just woondering how this is done through irrlicht, reason being that in my game initalizeation state i check 1st if the option.ini file is empty and if so then load a set of default parameters for window creation.
thankz
EDIT Again
Okay, i playd around with ur code and couldnt get it to work, i modified it a bit and now its workn charms, really good.
heres the save to file feature:
irr::io::IWriteFile *fwrite = device->getFileSystem()->createAndWriteFile("Optsdion.ini", false);
//The false is whether or not you wish to append to the file, ie true, add stuff to the existing file, or false, delete all and rewrite.
fwrite->write(reinterpret_cast<char *> (&Ms), sizeof(player));
next i added incase you want to store stuff to it:
fwrite->write("cher", 4)
//Store a char called "cher", the 4 represents how many characters to save, it also works for u32's ect
Okay what i havent worked out yet is to load the data back to your player, i am trying to implement a featuure where you want to recall the specific data back to ur object "or whatever"
e.g:
psedeuo:
Player->setHealth( type, position);
so u specify wat type you want and its position in the file.
I think that where you have allocated memory and store it in the file is where it saves the relative player data?
thankz again and i hope this helped
Last edited by Twinsen on Thu Nov 03, 2005 3:40 pm, edited 1 time in total.
Please note that this way you cannot exchange the files between architectures. If you switch from 32bit to 64bit (or vice versa) the file cannot be read anymore, same holds for PC<->Mac. It would be better to enhance the XML interface with some convenience methods and use an XML based file I/O. Would also enhance portability of games that use the Windows .INI interface.
If ever one of your objects you wish to save includes:
pointers, (including irr::string and irr::array and irr::list )
inherited classes, (anything which derives from irr::IUnknown)
than your in a lot of trouble with that method, as you will write the pointer address and when you load it back you will get a pointer to random memory.
I would overload the istream or ostream operators:
This means you can use it with cout, as well as stringstreams and outputfiles.
pointers, (including irr::string and irr::array and irr::list )
inherited classes, (anything which derives from irr::IUnknown)
than your in a lot of trouble with that method, as you will write the pointer address and when you load it back you will get a pointer to random memory.
I would overload the istream or ostream operators:
Code: Select all
std::ostream& operator << (std::ostream& os, const player &p)
{
return os << p.health << "," << p.name << "," << p.weapon;
}
//and later in your code do:
player Bob;
ofstream file;
file.open ("player.txt");
file << Bob;
file.close();