Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / saving/loading arrays

Author
Message
Grenadiac111
14
Years of Service
User Offline
Joined: 23rd Dec 2009
Location:
Posted: 27th Mar 2010 03:35
first i must appolagize for asking this but i could not find a post that covered what i was asking. I am making a zombie survival game wich will obviously require me to save a lot of floats, ints, and chars (such as ammo and camera position) to a file so the program can access it later but so far i cannot figure out how to save multiple varriables to one file. I assumed that dgdk would have a command for saving arrays but i cannot find one. so i would like to know how to save an array to a file and how to read that array from the file for use.
if this requires another lib could you please post a link to the lib's download site.
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 27th Mar 2010 04:26 Edited at: 27th Mar 2010 04:31
C++ nor GDK can guess how you want your data to be serialized, so you must handle this yourself. The easiest way to just dump an array into a file would be to first store the amount of bytes you wish to write as an unsigned integer then just copy that amount from your array into the target file. This can be done very easily, assuming you're using fstream, you can just write the size: "myFileStream << sizeof(WhateverType) * indices;" then copy the data in: "myFileStream.write( myArrayPtr, sizeof(WhateverType) * indices )". The inverse is very similar, just get the size via >> then get the data block using this read size via read().

However, this has the same issues as a shallow copy in that if your data stores any pointers or anything that stores pointers internally, such as a std::string, then it won't load correctly if the data it points to changes, which for a load/save system it most certainly will. The better way to handle this is to overload the << operator for ostream for your class you wish to save(if you don't have a class then you're doing it wrong) then manually handle saving of the data, for some classes that may just be the above example, but for others you may want to push strings into the stream so only their contents get written etc. You would also do the same for >> from ostream to handle loading.

A quick Google search finds THIS.

So in short, there isn't and shouldn't ever be some magical function that just saves any array because more often than not, it will cause problems.

Grenadiac111
14
Years of Service
User Offline
Joined: 23rd Dec 2009
Location:
Posted: 27th Mar 2010 06:28 Edited at: 27th Mar 2010 07:35
so what your saying is that i would have to store a certain number of bytes to a file and then and then send an equal number of bytes from the array to the file? wait what do you mean if im not using a class then im doing it wrong, does that mean that the data i wish to save must be in a class or do i have the wrong idea all together?
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 27th Mar 2010 07:39
Quote: "so what your saying is that i would have to store a certain number of bytes to a file and then and then send an equal number of bytes from the array to the file?"


I'm saying that's one method, and it may be sufficient for your needs.


Quote: "also ive heard that some people have been saving their data to an image file would this be practicle for a game with large numbers of variables?"


Err... you wouldn't save your 'a lot of floats, ints, and chars' to an image file. But if you're making some type of pacman-style game then I guess you could, but there's not really any point unless you're too lazy to make a level editor.

Grenadiac111
14
Years of Service
User Offline
Joined: 23rd Dec 2009
Location:
Posted: 27th Mar 2010 08:35 Edited at: 27th Mar 2010 09:03
i didnt think saving to an image would work i just wanted a profesional opinion as this will be my first project in wich im saving any info. but on another note could i just save each varriable to a seperate line in the file and then use a for loop to read each line and place each varriable into an array for use in the code and then reverse this process to save the varriables back to the file (ive done this with strings but not numbers)?
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 27th Mar 2010 11:14
Yes, the example in the link I posted in my first message effectively does this, except it will add spaces between non-string values.

Grenadiac111
14
Years of Service
User Offline
Joined: 23rd Dec 2009
Location:
Posted: 27th Mar 2010 17:14 Edited at: 27th Mar 2010 17:35
ah so i guess my next move should be to adapt my code that reads and records each line of a file to a string to a code that reads and records each line to an int type. the following code is my system for recording strings could you post an adapted version that could save the data to an array of int type?

oh and before i forget is the reason that the save commands have to be in a class because dgdk and fstream conflict when included in the same .cpp?
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 28th Mar 2010 07:19
Quote: "oh and before i forget is the reason that the save commands have to be in a class because dgdk and fstream conflict when included in the same .cpp? "


No, if they do then you need to search this forum with the term 'linker' and click on one of the trillion threads discussing the conflict.

The reason why using a class is important is because you can overload the << operator for ostream specifically for your class, that is, you can write code to handle saving of your class, and same for loading. If it's not part of a class, i.e. in the global scope then you can't use << for arrays and such because its size cannot be found at runtime so it'll probably just write the pointer to the array or the first element.

But in this case if the data you're saving is only those 4 lines then you probably won't need a class, the examples I linked to above show you how it's done, as does THIS and loads of other tutorials you can find with a quick Google search.

Grenadiac111
14
Years of Service
User Offline
Joined: 23rd Dec 2009
Location:
Posted: 28th Mar 2010 08:08 Edited at: 29th Mar 2010 01:55
acctualy the code sample i posted earlier was from a previous project im estimating there to be about 150 to 300 variables of just about every type needing to be saved in my current project (a very rough estimate at the moment). thanks for the link to functionx.com that site helped alot.
ive altered my code to save and load all sorts of file types and that leaves me with one last question that i could not find the answer to, can i tell the code to start reading the file from a certain line so that i could skip a few lines in the file that i may not want to read at the moment?

Login to post a reply

Server time is: 2024-11-20 01:35:59
Your offset time is: 2024-11-20 01:35:59