A while ago I made this module to make writing and reading buffers easy out of my curiosity of them. After some thought and finally having the ability to post on DevForum for the first time I’d open it up to the public for those who may find themselves dealing with buffers!
Why use EZBuffer?
- Comes with a built in read/write cursor
- Can make dealing with buffers a lot easier
- Dynamic resizing writing functions included
- Easily moddable, it’s just a basic bridge module
(Module provided after tutorial explanation)
So, how do we even use it?
I can give you a basic explanation and examples!
(Dump() is absolutely not required and advised against in usage outside of debugging and learning)
local EZBuffer = require(script.EZbuffer)
local MyBuffer = EZBuffer.new(4)
MyBuffer:write("i8", 0)
MyBuffer:write("u8", 255)
MyBuffer:write("i16", 1400)
MyBuffer:dump()
Now, what if we aren’t too sure how many bytes we need? That’s where the dynamics come into play!
local MyBuffer = EZBuffer.new(2)
MyBuffer:write("i8", 0) -- Writing is all compact to :write(itype), see extra info in comments for more
MyBuffer:write("u8", 255)
MyBuffer:writeDynamic("i16", 455)
MyBuffer:dump()
Writing a string? Got you covered!
local MyBuffer = EZBuffer.new(10)
MyBuffer:writeStringDynamic("Hello World!") -- Can ALSO be just WriteString if you can predict it's size
MyBuffer:dump()
– Reading is very similar to writing, except we are, of course, reading instead
local MyBuffer = EZBuffer.new(10)
MyBuffer:writeStringDynamic("Hello World!")
MyBuffer:position(0) -- Now we are at the start!
local Remaining = MyBuffer:remaining() -- This function tells you how far the buffer goes since the start!
print(MyBuffer:readString(Remaining)) -- Showing you that you can read strings as well!
Of course you can also just use :read() to read bytes normally, above I just showed a string example
local MyBuffer = EZBuffer.new(10)
MyBuffer:writeStringDynamic("Hello World!")
MyBuffer:position(0)
print(string.char(MyBuffer:read("u8")))
Here is another example, where we store an RGB color within just 3 bytes! (And show an example of chaining)
local MyBuffer = EZBuffer.new(3)
MyBuffer:write("u8", 255):write("u8", 0):write("u8", 146) -- Chaining is supported here!
MyBuffer:dump()
Sorry if these aren’t the best examples, but I am sure you can get the point!
Extra Note:
! To pull the actual buffer, do MyBuffer:getBuffer()
local result = MyBuffer:getBuffer()
Module: https://create.roblox.com/store/asset/74638794833287/EZbuffer