libgdx saving map

I don’t know how you can actually reach this limit, but you could try writing the string in portions: write 10 tiles, write the string to file, reset the string and do the same step again.

Thats what i thought of but im not too sure how i would come across doing this in libgdx xml??

Just looked up the source. XmlWriter directly writes through to the given writer object, so nothing is stored. Post your source again.

Also you could try using kryo, but I don’t know how good this works on android. But I would assume, that it works good enough, since the author also was a core contributor to libgdx.

To keep it simple and yet still more efficient: Maybe just write each tile on a line?

Something like:


blockDef blockID x y width height vx vy isCollision isObject objectID

Writing this format:


int num = 0;
StringBuilder sb = new StringBuilder();

for(int i = 0;i < Level.lengthX;i++) {
    for(int j = 0;j < Level.lengthY;j++) {
        Tile t = Level.tiles[i][j];
        sb.append(t.getBlockDef());
        sb.append(t.getBlockDef());
        sb.append(t.getBlockID());
        sb.append(t.getX());
        sb.append(t.getY());
        sb.append(t.getWidth());
        sb.append(t.getHeight());
        sb.append(0);
        sb.append(0);
        sb.append(t.isCollision());
        sb.append(t.isObject());
        sb.append(t.getObjectID());
        sb.append("\n");
        
        if (num++ % 10 == 0) { // write a chunk every 10 tiles
            file.writeString(sb.toString(), true); // true to append to file
            sb.setLength(0); // clear stringbuilder for next chunk
        }
    }
}

if (sb.length() > 0)
     file.writeString(sb.toString(), true);

This means that no more than 10 tiles worth of string will be in memory at a time. You can play with this number to balance memory usage vs. # of disk writes.
If it’s too slow then look into making the writes asynchronous.

Reading should be simple using a BufferedReader or Scanner to read one line at a time.

I would probably drop the xml approach altogether when saving masses of
more or less binary data.
Or compressing the raw tile definition data into a Base64 block, if xml stores also other leveldata.

Looks really good however when i run it. It doesnt work at all could you just add me on skype please tommohawkaction

I don’t think anyone is going to add you on Skype when we have a perfectly good forum for issues like this.

Please post the stacktrace of your latest issue!