Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Saving game data to file/loading it  (Read 1807 times)
0 Members and 1 Guest are viewing this topic.
Offline Huw

Senior Member


Medals: 1
Projects: 2



« Posted 2012-10-08 18:41:01 »

Hi, I've recently started looking at how I can save game data, such as scores, ammo etc. I can't seem to find an easy way to do it, if anyone could tell me/point me in the right direction of how I can easily do this. I am not looking to save many variables, just a few integers and strings.

Apologies if this seems a nooby question, I'm just getting confused with it all.

I'm a java noob, but I'm learning. My little blog. My Games.
Offline Gjallar

JGO Coder


Medals: 13
Projects: 1


Follower of Nurgle


« Reply #1 - Posted 2012-10-08 18:43:24 »

The serializable interface is what you might be looking for
Offline Huw

Senior Member


Medals: 1
Projects: 2



« Reply #2 - Posted 2012-10-08 18:50:25 »

The serializable interface is what you might be looking for
Searching that now, will edit it once I've looked/tried it. Thankyou

EDIT: Thankyou! This seems to be working *appreciation to you*Smiley

I'm a java noob, but I'm learning. My little blog. My Games.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline __iCode__

Senior Newbie





« Reply #3 - Posted 2012-10-09 05:44:32 »

Writing to a text file would be really easy, but then people can  access it and change their stats. Apart from that I think that is very easy.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 117
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2012-10-09 05:49:50 »

Writing to a text file would be really easy, but then people can  access it and change their stats. Apart from that I think that is very easy.
Well the main thing for serializing is for making the file smaller.
Offline SkyAphid

Senior Member


Medals: 3
Projects: 1



« Reply #5 - Posted 2012-10-12 06:04:51 »

I suggest making a singleton status object, serializing it, and saving the object as a whole.

Google Object saving and singletons for Java.

Very simple and effective.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline concerto49

Junior Member





« Reply #6 - Posted 2012-10-12 06:42:50 »

But the issue with serialization is any changes to the class can potentially render it unusable, just to be aware of it.

High performance, fast network, affordable price VPS - Cloud Shards
Available in Texas, New York & Los Angeles
Need a VPS Upgrade?
Offline SkyAphid

Senior Member


Medals: 3
Projects: 1



« Reply #7 - Posted 2012-10-12 11:33:21 »

That's why you add a serial version ID. Java will convert it for you most of the time.

e.g.:

1  
2  
3  
public class DataManager implements Serializable{

   private static final long serialVersionUID = 1L;

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline Varkas
« Reply #8 - Posted 2012-10-12 12:43:05 »

By using the native serialization of Java you lock your code very close to the data representation on disk. It's easy, but if you later want to do things differently, you might find this solution a bit inflexible.

If you want small files, use GzipOutputStream.
If you want flexibility, try XML exports.

If you want security, try some sort of encryption and file signing. There is nothing secure if the player can access the file, but it raises the bar notably for such attempts. Often even the simple old XOR scrambling is enough to hide data from curious eyes.

Personally I use text files and FileWriter/BufferedReader for most things. I can always supply them with compressing and/or crypting Input/Output streams, and for debugging I can peek easily into the files.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Cero
« Reply #9 - Posted 2012-10-12 16:41:03 »

I am very happily using Kryo now.

And using the CompatibleFieldSerializer I have exactly the behavior I need:
Any fields that a loaded save-game file has, that my code doesnt have (anymore) will be ignored.
Any fields that might be missing from a save-game file, will just left out, hence those fields will be NULL, or actually the default value.

It's very nice, since, I use save-games for debugging as well: I walk around the world, do stuff; and then when I stop the game it automatically saves a auto-save-game, which will be load be default on next startup.
So in debugging wherever I go, whatever I do its saved... which is really helpful when you have to restart and need to have what you just did.

And it helps to avoid save-game compatibility problems for end-users if implemented properly.


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #10 - Posted 2012-10-12 18:48:39 »

Well the main thing for serializing is for making the file smaller.


Is this really true in the 21st century?
Offline Varkas
« Reply #11 - Posted 2012-10-12 21:45:34 »

Is this really true in the 21st century?

In my experience, no. Try to serialize a byte array with four elements. You'll end up with abaut 200 or more bytes of serialized data, instead of four. Even compressed (gzip) it still will be much more than four bytes.

Ok, this is an extreme example, but Java serialization comes with an overhead.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline sproingie
« Reply #12 - Posted 2012-10-12 21:59:07 »

In my experience, no. Try to serialize a byte array with four elements. You'll end up with abaut 200 or more bytes of serialized data, instead of four. Even compressed (gzip) it still will be much more than four bytes.

I got 28 bytes
Offline Varkas
« Reply #13 - Posted 2012-10-14 12:59:18 »

My memory was off quite a bit there, indeed. Also I had the byte array as a member of an object, which added more overhead, but most likely still not 200 bytes. It's been a long time since I did those tests.

Thanks for the correction Smiley

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Nate

JGO Kernel


Medals: 129
Projects: 3
Exp: 14 years


Esoteric Software


« Reply #14 - Posted 2012-10-15 08:15:42 »

With Java's built-in serialization the overhead is in the form of headers that are written per type the first time the type is encountered. This means the overhead is high percentage-wise for a small amount of data, but mostly negligible for a large amount of data. There are plenty of other reasons to avoid the built-in serialization though. Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (62 views)
2014-04-15 18:08:23

BurntPizza (60 views)
2014-04-15 03:46:01

UprightPath (73 views)
2014-04-14 17:39:50

UprightPath (56 views)
2014-04-14 17:35:47

Porlus (73 views)
2014-04-14 15:48:38

tom_mai78101 (99 views)
2014-04-10 04:04:31

BurntPizza (159 views)
2014-04-08 23:06:04

tom_mai78101 (254 views)
2014-04-05 13:34:39

trollwarrior1 (208 views)
2014-04-04 12:06:45

CJLetsGame (215 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!