BufferConverter is a simple module to convert regular datatypes into buffers.
Documentation:
SerializedBuffer<T> Type
All this type does is store the datatype that its storing. Very simple.
ReadOptions & WriteOptions
The ReadOptions
and WriteOptions
type both determine how BufferConverter
will serialize or deserialize certain datatypes.
Here are what each option does:
-
errorOnException :: boolean?
- This option determines wether or not the module with throw an error or a warn if it encounters something wrong.
Useful if you don’t want the entire thing to stop
- This option determines wether or not the module with throw an error or a warn if it encounters something wrong.
-
keys :: boolean?
- This option determines wether or not keys will be written/read, depending on wether or not youre using this in a
WriteOptions
or aReadOptions
object.
Useful if you want to save space by not serializing keys, and just want to write arrays.
- This option determines wether or not keys will be written/read, depending on wether or not youre using this in a
-
write/readInstanceAsCopy :: boolean?
- This option determines wether or not
Instance
s will be fully serialized with all of their properties and children, or just their unique id reference.
Useful if you want to useBufferConverter
for, say, a base-saving system instead of a networking system.
- This option determines wether or not
-
numbersAs :: "(u8"|"i8"|"u16"|"i16"|"u32"|"i32"|"f32"|"f64")?
- This option determines how numbers will be written and read as.
Useful if you don’t wanna waste space storing a few numbers.
Note that this option applies to ALL numbers written and read, use carefully!
- This option determines how numbers will be written and read as.
Serializable Datatypes & Their Respective Sizes
-
Table : Size = (amount of keys and values (or just the amount of values if you’ve set
keys
to false) + sum of all the sizes of each key and value (or just the sum of all the values if you’ve setkeys
to false) + 1 (null terminator)) bytes -
Number : Size = (4 bytes default (as i32), variable if using the
numbersAs
option) bytes -
String : Size = (amount of letters + 1 (null terminator)) bytes
-
Boolean : Size = 1 bytes
-
CFrame : Size = 22 bytes
-
Vector3 : Size = 12 bytes
-
Vector3int16 : Size = 6 bytes
-
Vector2 : Size = 8 bytes
-
Vector2int16 : Size = 4 bytes
-
Color3 : Size = 3 bytes
-
EnumItem: Size = 3 bytes
-
Instance : Size = (if not writeInstanceAsCopy then 2 else too long, read the code here:)
Functions
BufferConverter has 2 functions, One for serializing, one for deserializing.
-
BufferConverter.Serialize<T>(item: T, options: WriteOptions): SerializedBuffer<T>
- This function takes in any serializable datatype T (see serializable datatypes), and returns it as a buffer (as a SerializedBuffer to store the datatype so that it can be returned later by
BufferConverter.Deserialize()
).
- This function takes in any serializable datatype T (see serializable datatypes), and returns it as a buffer (as a SerializedBuffer to store the datatype so that it can be returned later by
-
BufferConverter.Deserialize<T>(packet: SerializedBuffer<T>, options: ReadOptions): T
- This function takes in a
SerializedBuffer
, and returns the datatype it’s storing, T.
- This function takes in a
And that’s basically it. No need to get complicated here!
Example Usage
local Converter = require(path_to_converter)
local tbl = {
Hi = 10,
Lol = "Foo"
}
local packet = Converter.Serialize(tbl)
print(Converter.Deserialize(packet)) -- the table earlier
Serializing parts and children:
local Converter = require(game.ReplicatedStorage.Modules.Utility.Converter)
local target = workspace.Target
-- Should clone 3 times
local buf = Converter.Serialize({
target,
target,
target
}, {writeInstanceAsCopy = true})
local new = Converter.Deserialize(buf, {readInstanceAsCopy = true})
for _, v in new do
v.Parent = workspace
end
Result (spread out so its easier to see that Target has been cloned 3 times with all of its children):
Serializing More Instances
For reasons of being lazy , I didn’t finish the Instance full copy serializer. You can add your own by editing the Properties module here:
In the getProperties function, just simply add more elseif statements. If you need custom applier functions, simply prefix the key with METHOD_ and it will attempt to find the method name in the methods table, and call it passing the instance and the value.
Download BufferConverter here! →
Converter.rbxm (9.6 KB)
If you find any bugs/errors, let me know!
And if you haven’t already, check out my other module SimpleZone! →
SimpleZone: A simpler and newer alternative to ZonePlus - #14 by athar_adv