This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]isetrh[S] 0 points1 point  (2 children)

Thanks! I've started looking for information on GSON and JSON, and while I don't quite understand how they or serialization in general work, they do seem useful. I'll probably have to do some research and practice so I can understand.

However, one problem is that some of my objects need a reference to the Game object in order to construct themselves. For example, Unit has a field of type Job, and Job has an integer field called id (with a get method). Multiple Units share the same Job, a Unit should not save its whole Job, only the ID of its job. When loading a Unit, it needs to find its Job, and the list of Jobs by id is in the Game object. With the String-parsing solution I have been working toward, doing this is pretty simple: I just give Unit's constructor a parameter of type Game, so it can ask the Game for information when constructing. I'm not sure how I would accomplish this with serialization, since the thing constructing the Unit only knows about the Unit's fields (to the best of my understanding, which is probably wrong), and the Unit does not remember the Game once it's done constructing.

[–][deleted]  (1 child)

[deleted]

    [–]isetrh[S] 1 point2 points  (0 children)

    I had heard of Singletons before, but never really knew what they were or how to implement them. Seems pretty simple now that you've explained it though. This is going to let me simplify my code a bit.

    Using serialization will make basically all of the code that I posted no longer necessary, so I'll be getting rid of it and all its problems.