Serialize block data

I wanna be able to serialize my block placing system. So far, I believe I have a pretty good system in place, but I wanna be able to add extra info for the blocks.

Basically, since blocks are a 3x3x3 grid, I store each blocks data based on its relation to your spawn. And so it saves under a grid system, for example, the coordinates {-1, -1, -1} would save like so:

["-1"] = {
    ["-1"] = {
        ["-1"] = "2"
    }
}

And each coordinate is given a BlockId. To me, this is the best way to minimilize block info to as few characters as possible.

Other things to point out is I have the ability for 2 slab types to be placed inside 1 “3x3x3” grid area, and so need to save accordingly. This may look like:

["-1"] = {
    ["-1"] = {
        ["-1"] = "1-1:1/2-19:1"
    }
}

So, the 1 at the start symbolises it’s a bottom slab. I divide them up using a “/” and so I can use string functions to seperate this string, and I can know what the id for the top and bottom slabs are. So the example above is broken up like

"1" -- Slab position (bottom)
"-" -- Breaks up the string
"1:1" -- Id for the slab
"/" -- Seperates between the 2 slab positions
"2" -- Slab position (top)
"-" -- Breaks up the string
"19:1" -- Id for the slab

And once again, to my knowledge this is well organised and serialized. However, certain blocks have rotation values that need to be saved. So my question is how could I add those values as well?

Example of stairs, which have rotation values

I have a data module, and that tells me if the certain block has a rotation value that needs to be saved, so only certain blocks need to have their rotation saved. How could I serialize this well?

1 Like

Is your world infinite? Or can you place a bound on where players can place blocks ahead of time?

Anyway, to encode rotation you can use a number from 0-23. You need 24 different orientations for blocks which have no symmetries, because a block can point its front face in one of 6 directions, and rotate around its front face in one of 4 rotations, for 4*6=24 orientations.

You should avoid using decimal numbers encoded as strings, as they’ll always take 1 byte per ASCII character and usually won’t encode information very efficiently. Taking the coordinates for example, with 2 characters you can encode numbers from -9 to 99, while you can encode from -32766 to 32767 with those 16 bits if you use them optimally. Plus, you don’t necessarily have to save the position of each block in the first place, if you organize your world into chunks then you only have to store the positions of the chunks and not the blocks within the chunks.