Sending data over UDP packets with string.pack() [SOLVED]

Trying to use string.pack (Lua 5.3 Reference Manual) to serialize some data for sending over user input and position data via UDP sockets. It looks like string.pack isn’t part of Lua 5.1 or LuaJIT.

I did see luaJIT has string.buffer (String Buffer Library), but it’s not enabled by default. But that doesn’t seem to be included in Defold as well.

Any recommendations for serializing strings/data to send over the wire? And help would be appreciated, thanks!

@AJirenius had a good optimized solution for this years ago…

Try this

https://fperrad.frama.io/lua-MessagePack/

You could also look into using protobuf for your client - server messages:

1 Like

Great, thank you both, will check out both options!

Trying to install the release (https://framagit.org/fperrad/lua-MessagePack/-/archive/0.5.4/lua-MessagePack-0.5.4.zip) as a dependency, but I get an error. I’ve noticed when installing Lua things (that aren’t Defold specific) I get errors, do I have to do something specific, or install it another way?

The dependency you add must be set up for sharing as a Defold library:

I recommend downloading and adding the file(s) to your project

Ah, got it! Since MessagePack is just a lua script (downloaded directly from releases), I added it directly to my utils folder, and then can require and use it anywhere with:

local mp = require("utils.MessagePack")
local data = {100, 50, 0}
mpac = mp.pack(data)

Thanks for all the help, appreciate it!

2 Likes