I have a Model, which needs to be written to disk somehow and read afterwards (making a simple Java 2D game). Now, there’s a reference to Player, which has an insaneload of References to Blocks and Entities. In my project there are a HUGE amount of Entities and Blocks, and I really dont want to write a toString()/encode() then fromString()/decode()… The project is overdue anyway and over here its 4:05 AM :p.
So, what does a human in that situation do? Crawl here and ask about what Serialization does, and if it will work right away. I’m in the drama that it must work on the first go, no much space to experimenter, sadly.
The situation below needs to be entirely written to disk. Doable with a single serialize() function 😮 ?
Now, imagine this fictive classes(in reality I have 5800 lines of java code and a ton of intermixed data, that hurts)(no functions)(off the top of my head):
class InventorySlot { Object[] list; Class type; int quantity; }
class Inventory { InventorySlot[] slots; }
class Player { int hp; Inventory inv,palette; double x,y; }
enum Block { air, stone, cobble, dirt, etc; }
class Entity { double x,y,dir,speed,h,w; /*AND CUSTOMS! (derivers)*/ }
class EntitySpriteAnimationPuf { Particle[] parts; final double friction, dir, dirnoise; Color color; } // final vars!
class World
{
List<Chunk> chunks; // have an infinite map. each chunk is 64x64 Blocks.
Entity[65535] entities; // I have a lot of entites, both types and later their instances.
}
class Model // target class to be entirely written down
{
Player p;
WindowManager wm; // Yes, i have my own GUI. Yes, it looks wonderful. Yes its got a lot of data... Yes, ouch.
World w; // got this manually written down.
Vector<String> terminal; // Dont want this to be serialized...
boolean guiactive, pause;
// And a few more...
}
// AND A LOT MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE.
here is what u need to do with to load/save java object (exception handling is ignored)
make sure all the classes implement the Serializable interface
one thing to know is that, if you changed any of the serialized classes, then
most likely your “deserialization” will fail