Save/Load System?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
The_Dark_Avenger
Posts: 2
Joined: Thu Nov 03, 2005 1:28 pm

Save/Load System?

Post by The_Dark_Avenger »

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.
Ishtm
Posts: 37
Joined: Mon May 09, 2005 8:03 pm

Post by Ishtm »

supose you have this struct...

Code: Select all

struct SPlayer
{
    short int Health;
    short int Weapon;
};
SPlayer myPlayer;
for write that you have to...

Code: Select all

irr::io::IWriteFile fwrite = IrrDevice->getFileSystem()->createAndOpenFile(file);
fwrite->write(reinterpret_cast<char *>(&myPlayer), sizeof(SPlayer));
For loading you have to do the same but with IReadFile and file method.
Twinsen
Posts: 27
Joined: Thu Nov 03, 2005 2:05 pm
Location: Gold Coast

Post by Twinsen »

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 :P
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.
hybrid

Post by hybrid »

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.
FlyHigh
Posts: 111
Joined: Wed Mar 23, 2005 12:05 am

Post by FlyHigh »

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:

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();
This means you can use it with cout, as well as stringstreams and outputfiles.
Post Reply