Saving/Loading Structs

Posted By: sheefo

Saving/Loading Structs - 02/09/07 14:49

Another question about structs

Is it possible to save/load a struct to/from a file in Lite-C? I just want to dump a struct in a file and load it later on, like you can do in C++.
I don't know how because GameDev is down, so if it is possible can someone show me code...?

I would really appreciate it.
Posted By: sheefo

Re: Saving/Loading Structs - 02/09/07 22:16

OK, if I promise not to ask another question about structs, will that convince you to reply?
Posted By: jcl

Re: Saving/Loading Structs - 02/10/07 02:45

file_save("struct.bin",&mystruct,sizeof(mystruct));

http://manual.conitec.net/afile_save.htm
Posted By: sheefo

Re: Saving/Loading Structs - 02/10/07 13:31

Thank you. But how can I use this to save multiple structs to the same file, or an array containing struct pointers? When I save an array of structs it turns out as 0 bytes.

I need to know how to load the multiple structs from the file as well.

My idea is to have one struct called HEADER which holds the some data, then I wanted to fill the rest of the file with PLAYER struct I got to handle players in my RTS. They are stored in an array "PLAYER* players[8];".
I then wanted to, when loading, read the header and ensure that the attribute 'application_name' matches my constant 'APPLICATION_NAME', and the 'application_version' is below the constant 'APPLICATION_VERSION'. Then I can continue loading all player structs.

Can you help me with this?
Posted By: FBL

Re: Saving/Loading Structs - 02/10/07 16:23

Pointers most likely will have to be reinitialized after loading, as the memory areas they point to might be different.

Maybe you can put all structs into a buffer first?
http://manual.conitec.net/aadd_buffer.htm
But I think it's only used for external files...


no idea...
Posted By: jcl

Re: Saving/Loading Structs - 02/11/07 16:50

Correct, saving struct pointers won't make much sense.

For saving struct content, you can put the structs in a "mother struct", like this

typedef struct MOTHER { CHILD1 child1; CHILD2 child2; ... } MOTHER;

where CHILD1, CHILD2 etc. are other structs, and then save the mother struct.

Two remarks:

- You can not save engine objects this way - you need game_save for that.
- You can also use game_save with SV_STRUCT for saving your structs.
Posted By: sheefo

Re: Saving/Loading Structs - 02/11/07 18:45

I like the idea of using 'game_save'. So let me get this right:
- use 'add_struct' on all structs I want to save
- use 'game_save' using SV_STRUCT and all structs I added will be saved to a file

Thats all?
Posted By: jcl

Re: Saving/Loading Structs - 02/12/07 16:21

Yes, that's the idea behind add_struct.
Posted By: sheefo

Re: Saving/Loading Structs - 02/12/07 17:24

I ran into another problem now, 'game_save' doesn't work. I am sure it is a bug, I posted a topic in Bug Hunt http://www.coniserver.net/ubbthreads/sho...;vc=#Post728343
© 2023 lite-C Forums