Staredit Network > Forums > Technology & Computers > Topic: Programming a way to save
Programming a way to save
Apr 2 2011, 8:52 pm
By: Apos  

Apr 2 2011, 8:52 pm Apos Post #1

I order you to forgive yourself!

I'm currently making a program where I will need to save data to a file in order to reload it later in the program.
Using something that looks a bit like an .ini file, I managed to do most of what I wanted to do.

Example of how I save things right now:
[Dog1]
Name = Alice
Age = 12
Owner = Bob
Type = Labrador Retriver

[Dog2]
Name = Carol
Age = 5
Owner = Dave
Type = Unknown


I made a method that is able to go in [Dog1] for example and read a certain variable like Name (Which would give me "Alice").

Now, let's say I want a dog to be able to hold an array of friends, I'm not sure what would be the best way to save so it stays organised without having any conflicts with the arrays of the other dogs. Here is one way I thought about:

My first idea:
[Dog1]
Name = Alice
Age = 12
Owner = Bob
Type = Labrador Retriver
Friends = 2
Friend1 = Carol
Friend2 = Dave

[Dog2]
Name = Carol
Age = 5
Owner = Dave
Type = Unknown
Friends = 1
Friend1 = Alice


Would there be a better way to do it? I want to be able to have a way to save 2D arrays, 3D arrays, etc...

I hope it wasn't too hard to understand my question, the example about the dogs was chosen because it resembles to the real project I am working on without saying what I'm working on.




Apr 2 2011, 8:57 pm The Starport Post #2



You've basically discovered the reason lots of people use XML for configuration files. I prefer JSON myself, though.



None.

Apr 2 2011, 9:07 pm Lanthanide Post #3



Is there any particular requirement for this file to be human-readable? Storing it as a binary file is easier, and with arrays you can just dump the entire block of memory out, and then read it back in as a block. Assuming you're using C.



None.

Apr 2 2011, 9:23 pm Apos Post #4

I order you to forgive yourself!

The reason I do it like that is because that's the only way I knew how to do it. I use Java.

I believe the reason I do it so it's readable is to avoid the possibility that is becomes corrupted (false idea?). If anything goes wrong, I can restore it be hand.




Apr 3 2011, 12:31 am O)FaRTy1billion[MM] Post #5

👻 👾 👽 💪

Quote
Friends = 2
Friend1 = Carol
Friend2 = Dave
That's the only way I know to do it. Even if you were doing it in a binary format you would do it the same way.

For 2d or 3d you could just do like
Quote
Friends = 2,3
Friend1,1 = blah
Friend1,2 = Blah
Friend1,3 = blalhalh
Friend2,1 = Aaaaa
Friend2,2 = Awawa
Friend2,3 = asdf

EDIT:
Another way is to just save them as a list ...
like
Friends = Carol,Dave

2D or 3D might be a little more complicated ...



TinyMap2 - Latest in map compression! ( 7/09/14 - New build! )
EUD Action Enabler - Lightweight EUD/EPD support! (ChaosLauncher/MPQDraft support!)
EUDDB - topic - Help out by adding your EUDs! Or Submit reference files in the References tab!
MapSketch - New image->map generator!
EUDTrig - topic - Quickly and easily convert offsets to EUDs! (extended players supported)
SC2 Map Texture Mask Importer/Exporter - Edit texture placement in an image editor!
\:farty\: This page has been viewed [img]http://farty1billion.dyndns.org/Clicky.php?img.gif[/img] times!

Apr 3 2011, 1:00 am Jack Post #6

>be faceless void >mfw I have no face

Fairly relevant question: how would one go about making a program to edit XMLs? It would first have to read the XML and put the information into a GUI with editboxes, and then write any edits back into the XML. How do you go about parsing the XML?



Red classic.

"In short, their absurdities are so extreme that it is painful even to quote them."

Apr 3 2011, 1:24 am Apos Post #7

I order you to forgive yourself!

In Java, there is a library that allows you to do it: http://jaxp.java.net/ (Java API for XML Processing)

Also: Sax (French)

After a quick search, it seems like there are even more XML libraries for Java.




Apr 3 2011, 1:32 am Jack Post #8

>be faceless void >mfw I have no face

I want to make a new data editor for sc2. Will I be able to do that with that library? I'm not a great coder, so I'd have a whole bunch of edit boxes, and any modifications get written to the xml.



Red classic.

"In short, their absurdities are so extreme that it is painful even to quote them."

Apr 3 2011, 1:37 am Apos Post #9

I order you to forgive yourself!

Quote from Jack
I want to make a new data editor for sc2. Will I be able to do that with that library? I'm not a great coder, so I'd have a whole bunch of edit boxes, and any modifications get written to the xml.
Does the data editor use xml? If it does, then that's what those libraries are made for, handle xml parsing.




Apr 3 2011, 1:40 am NicholasBeige Post #10



I think its relevant to ask what language and compiler you are using for this?

Personally I would say the most appropriate method of saving arrays into your classes would be the list method.

Friends = peter, mandy, jethro, amadeus

Then you call a function on your Friends variable, maybe like Friends.Length to determine that Dog1 has 4 friends. Then to retrieve them you can go Friends.each Do: print. For example. That would certainly be the most efficient way to do it, since it only uses one array.

Your other method of using an integer variable and then a basic array also works, but obviously uses 2 variables
Friends = 3
Friend[1..3] = jeremiah,andre3000,patrick



None.

Apr 3 2011, 1:41 am Jack Post #11

>be faceless void >mfw I have no face

Quote from Apos
Quote from Jack
I want to make a new data editor for sc2. Will I be able to do that with that library? I'm not a great coder, so I'd have a whole bunch of edit boxes, and any modifications get written to the xml.
Does the data editor use xml? If it does, then that's what those libraries are made for, handle xml parsing.
All the unit info and data is stored in XMLs to the best of my knowledge, yes. I don't suppose you could whip up an example where you have an edit box and changing it + saving will place the info into a random xml file? You can use any xml.



Red classic.

"In short, their absurdities are so extreme that it is painful even to quote them."

Apr 3 2011, 1:50 am Apos Post #12

I order you to forgive yourself!

Quote from Jack
Quote from Apos
Quote from Jack
I want to make a new data editor for sc2. Will I be able to do that with that library? I'm not a great coder, so I'd have a whole bunch of edit boxes, and any modifications get written to the xml.
Does the data editor use xml? If it does, then that's what those libraries are made for, handle xml parsing.
All the unit info and data is stored in XMLs to the best of my knowledge, yes. I don't suppose you could whip up an example where you have an edit box and changing it + saving will place the info into a random xml file? You can use any xml.
Once I get familiar with the library, I guess it would be a simple task to make an example with a user friendly interface (I already have the interface code done.). At this time, I don't know anything about xml. (I heard about it but and saw a couple examples, but I don't know the details of the way it works.)

Quote from name:Cardinal
I think its relevant to ask what language and compiler you are using for this?
Compiler : javac 1.6.0_20

Quote from name:Cardinal
Personally I would say the most appropriate method of saving arrays into your classes would be the list method.

Friends = peter, mandy, jethro, amadeus

Then you call a function on your Friends variable, maybe like Friends.Length to determine that Dog1 has 4 friends. Then to retrieve them you can go Friends.each Do: print. For example. That would certainly be the most efficient way to do it, since it only uses one array.

Your other method of using an integer variable and then a basic array also works, but obviously uses 2 variables
Friends = 3
Friend[1..3] = jeremiah,andre3000,patrick
I guess if I put it on the same line, then it'll take an additional step to parse them.




Apr 3 2011, 2:48 am The Starport Post #13



Quote from Apos
The reason I do it like that is because that's the only way I knew how to do it. I use Java.

I believe the reason I do it so it's readable is to avoid the possibility that is becomes corrupted (false idea?). If anything goes wrong, I can restore it be hand.
Bam.

If restoring it by hand is so important (you shouldn't need to worry about that with Java's own serialization functionality, really), use an XML or JSON serialization library instead.



None.

Apr 3 2011, 4:21 am Apos Post #14

I order you to forgive yourself!

Quote from name:Tuxedo-Templar
Bam.

If restoring it by hand is so important (you shouldn't need to worry about that with Java's own serialization functionality, really), use an XML or JSON serialization library instead.
That's what I was looking for! That's so amazing! I had seen the Java serialization in use before, but never knew how to use it or what it was used for. I'll have to experiment now.




Apr 3 2011, 4:57 am Lanthanide Post #15



Yip, just use the java serialisation library. All of the default Java libraries have serialisation methods already (like String for example). So all you need to do for your classes is write the writeobject functions that will just be something like this:
Name.writeobject()
Age.writeobject()
Friends.writeobject()

That's the gist of it, very easy. You can choose how much you need to serialize, and other things that might be computed can be left out, and in your readObject function you can perform the calculations once you've read in sufficient values.

Tutorials, just ones I found off google, no idea if they're particularly good:
http://www.deitel.com/articles/java_tutorials/20050923/IntroductionToObjectSerialization_Page6.html
Using Java serialization for a client/server program: http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/serial.html



None.

Apr 3 2011, 6:50 am RexyRex Post #16



I <3 PHP
http://php.net/manual/en/function.serialize.php

$saved = serialize($yourBullshitData);
$restored = unserialize($saved);



None.

May 2 2011, 10:33 pm Apos Post #17

I order you to forgive yourself!

After thinking about it, I believe that in the long term, it is better to use xml as my way of saving.
Introducting the java streaming xml parser.




May 2 2011, 11:26 pm Lanthanide Post #18



Depends what your purpose for the data is. If you want it to be human readable and editable when it is on disk, XML is certainly an option, but if you're only doing something pretty basic, than simple INI file format would be easier.

Like all tools, XML is good for some things, and not good for others. It's certainly much much verbose at saving information, which is it's biggest downside. If you're not using the strict type/semantic checking, and it doesn't need to be human readable/editable, and you're not making complex decisions when you read the data in (eg, you just read it is as a single blob or array of data/structures), then there's really no compelling reason to use XML over straight binary files.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[05:02 am]
Oh_Man -- whereas just "press X to get 50 health back" is pretty mindless
[05:02 am]
Oh_Man -- because it adds anotherr level of player decision-making where u dont wanna walk too far away from the medic or u lose healing value
[05:01 am]
Oh_Man -- initially I thought it was weird why is he still using the basic pre-EUD medic healing system, but it's actually genius
[03:04 am]
Ultraviolet -- Vrael
Vrael shouted: I almost had a heart attack just thinking about calculating all the offsets it would take to do that kind of stuff
With the modern EUD editors, I don't think they're calculating nearly as many offsets as you might imagine. Still some fancy ass work that I'm sure took a ton of effort
[12:51 am]
Oh_Man -- definitely EUD
[2024-5-05. : 9:35 pm]
Vrael -- I almost had a heart attack just thinking about calculating all the offsets it would take to do that kind of stuff
[2024-5-05. : 9:35 pm]
Vrael -- that is insane
[2024-5-05. : 9:35 pm]
Vrael -- damn is that all EUD effects?
[2024-5-04. : 10:53 pm]
Oh_Man -- https://youtu.be/MHOZptE-_-c are yall seeing this map? it's insane
[2024-5-04. : 1:05 am]
Vrael -- I won't stand for people going around saying things like im not a total madman
Please log in to shout.


Members Online: Roy