Sign in to follow this  

Easy way to save lists to disc?

This topic is 4356 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi This has been on discussion but i never got it entirely straight. I got loads of dynamical lists in my game, normally holding pointers to instances of units, cities etc. Often many actors have pointers to the same object, and this lists changes but the object doesnt move. Anyway. If saving them to disc my idea is as follows: 1. Loop through the actual objects and give them unique IDs. 2. Loop through actors lists of pointers and translate each pointer to a more easily int or something according to the targets ID. 3. Save the objects and save actors arrays of "IDs" to the objects they used to point to. 4. Shut down. 5. At load, restore all objects and then restore actors lists by going through the objects and finding the one with the right ID, and add such a pointer to the actors list. Is this a good idea? Any better ideas that are not to complicated? Also, i dont know how to save a dynamically sized array of IDs to disc. The first player may have 10 units while the next has 200 so his array of unit-ID will be longer. Etc Thanks a bunch Suliman

Share this post


Link to post
Share on other sites
Hello,

Yeah, giving an ID to the element you want to save in order to retrieve relationships is a good solution.

To save a dynamic array, just save first the number of elements:

Backup List L:
Write size(L)
Write every element of L

Restore List L:
Read n
Read n elements and put them into L

Share this post


Link to post
Share on other sites
The addresses are unique ids. They just won't be valid addresses when you reload them. As long as you remember to replace them with correct addresses after you load them you can write them and read them back. Most likely though they should have a unique id assigned when they are created as well as a name. City at address 0x00689430 has invalid whatever isn't the handiest of messages. Addresses don't work real well when what they are pointing at is in an STL container either.

Share this post


Link to post
Share on other sites
ok thx

any tips on a solid and basic list-class out there if the standard lists are not very good? (many seem to complain about them)

I need no complex list-stuff in this class. But sorting due to size, pos or any other data contained in the lists-objects are always welcomed.

Suliman

Share this post


Link to post
Share on other sites
The plan is to have each object also point to it self:

When created, a object has a "selfPoint" initiated. As this object never moves during the execution, the value of this pointer can be used as unique ID right?

At load, pointers in lists find the restored object by comparing its value with the value of the objects "selfPoint".

After all pointers are corrected, objects updates their selfPoint to point to where the object lies now (after load they change position in the memory).

This would work, right?

Thx

Share this post


Link to post
Share on other sites

This topic is 4356 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this