Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (236)
games submitted by our members
Games in WIP (865)
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  
  Libgdx JSON saving problem?  (Read 3014 times)
0 Members and 1 Guest are viewing this topic.
Offline 04hockey

Senior Devvie


Medals: 6
Projects: 3
Exp: 6 years


Founder of Fanger Games.


« Posted 2014-11-04 03:44:03 »

Hi guys.

I'm making a game where I have some JSON serializable objects that I can save to a file, and load up at the next time the game is played.

The save structure is like this:

First, an arraylist containing all of the buildings on the map.

A building contains a couple of values unimportant to this issue, and a currentTask variable and an arraylist of tasks (tasks are their own custom class, FacilityTask.java). This is a real-time game, so if you schedule a task and close the game it will technically still be going on.

So upon opening a saved game, it checks to see if any tasks have been completed:
1  
2  
3  
4  
5  
6  
      if(performing && getRemainingTime() <= 0){ //if the facility is performing a task and the remaining the time of the task is <= 0 millseconds
         finishAction(taskScheduled, survivorPerforming); //terminate the current task scheduled
         performing = false; //setting the facility to not performing a task
         startingTime = 0; //reset the starting time of the current task
         endingTime = 0; //reset the ending time of the current task
      }


In the case of this test, the task we are testing is already completed once i open the game. (Keep in mind that if i never close the game, this feature works perfectly fine).

But opening a game with a completed task, all of the tasks within the facility are different, as if they have generated all new ones (ive taken away any code that generates the array of tasks within a facility).

Output of old task vs. new task:
1  
2  
com.fanger.zombie.FacilityTask@327a5b7f
com.fanger.zombie.FacilityTask@7f0d08bc


Because of this, finishing the task doesn't change anything because its not even a variable within the facility anymore. I know this isnt a problem with the saving/loading im doing with JSON because if i start a task, close it, and open it before its completed, the progress is saved and in-tact.

I don't really know what section of code to post, as this issue seems like it could be a problem with a few different classes and thousands of lines of code, so if you'd like to see something please ask me and ill post it.
Offline trollwarrior1
« Reply #1 - Posted 2014-11-04 05:56:24 »

So what is wrong? Putting value X to json, serializing it, saving it to file, reading it from file, parsing json and getting value X somehow is a different value than what you put in the json?
Offline Grunnt

JGO Kernel


Medals: 143
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #2 - Posted 2014-11-04 12:35:26 »

It's also not clear to me what the problem is. Could you specify what you mean by "all of the tasks within the facility are different, as if they have generated all new ones"? Because I have no idea what the result looks like.

As for this:
Output of old task vs. new task:
1  
2  
com.fanger.zombie.FacilityTask@327a5b7f
com.fanger.zombie.FacilityTask@7f0d08bc

This is not unexpected to me. The number you are seeing behind the @ sign is the object's hash code, which by default in most cases is the object's internal address. If you serialize an object, then deserialize it, you in fact create a new object (which is identical to the old object, but may be located somewhere else in memory). At least, that's how I always understood it to be.

As far as I know the LibGDX json serializer is not really smart enough to serialize references to objects (please correct me if I'm wrong), which means that if your save structure contains multiple references to the same object (e.g. many buildings refer to the same task objects) then things will get messed up. While serializing, every single time the task is encountered it is serialized to disk as a seperate object. When deserializing, these objects are instantiated again, but now as many seperate task objects instead of one. Maybe this is the problem you are having.

One way to fix this is to use custom serialization and assign ID's to your task objects, then reconstruct everything while deserializing. This is a bit messy, though. An alternative, and better, approach would be to use another serialization approach, e.g. just using Java's default serialization(which is actually quite good and easy to use) or use a library such as Kryo which is really fast.

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

Senior Devvie


Medals: 6
Projects: 3
Exp: 6 years


Founder of Fanger Games.


« Reply #3 - Posted 2014-11-04 14:48:17 »

So what is wrong? Putting value X to json, serializing it, saving it to file, reading it from file, parsing json and getting value X somehow is a different value than what you put in the json?
It's also not clear to me what the problem is. Could you specify what you mean by "all of the tasks within the facility are different, as if they have generated all new ones"? Because I have no idea what the result looks like.

As for this:
Output of old task vs. new task:
1  
2  
com.fanger.zombie.FacilityTask@327a5b7f
com.fanger.zombie.FacilityTask@7f0d08bc

This is not unexpected to me. The number you are seeing behind the @ sign is the object's hash code, which by default in most cases is the object's internal address. If you serialize an object, then deserialize it, you in fact create a new object (which is identical to the old object, but may be located somewhere else in memory). At least, that's how I always understood it to be.

As far as I know the LibGDX json serializer is not really smart enough to serialize references to objects (please correct me if I'm wrong), which means that if your save structure contains multiple references to the same object (e.g. many buildings refer to the same task objects) then things will get messed up. While serializing, every single time the task is encountered it is serialized to disk as a seperate object. When deserializing, these objects are instantiated again, but now as many seperate task objects instead of one. Maybe this is the problem you are having.

One way to fix this is to use custom serialization and assign ID's to your task objects, then reconstruct everything while deserializing. This is a bit messy, though. An alternative, and better, approach would be to use another serialization approach, e.g. just using Java's default serialization(which is actually quite good and easy to use) or use a library such as Kryo which is really fast.

Thanks for the replies guys, but I managed to figure out the problem last night before I went to sleep.

In line 2 from my first set of code, I call the method:
1  
         finishAction(taskScheduled, survivorPerforming);


That method is what sets a tasks enabled value to false so you cant do it again. The problem was, I was setting the reference to the actual task being performed, not the actual task stored within the facility. I think with saving/loading that becomes a new object which is what caused my problem.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (185 views)
2019-09-04 15:33:17

hadezbladez (4864 views)
2018-11-16 13:46:03

hadezbladez (1785 views)
2018-11-16 13:41:33

hadezbladez (5136 views)
2018-11-16 13:35:35

hadezbladez (1016 views)
2018-11-16 13:32:03

EgonOlsen (4367 views)
2018-06-10 19:43:48

EgonOlsen (5215 views)
2018-06-10 19:43:44

EgonOlsen (2946 views)
2018-06-10 19:43:20

DesertCoockie (3867 views)
2018-05-13 18:23:11

nelsongames (4293 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
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!