It seems like the standard way to handle binary data in Lua is to use strings. It’s the way the Lua sockets functions and the strings are designed to handle binary values.
Problem is that it’s not reliable with Defold messages.
After long error searching why my network is breaking down all the time it seems like messages trims all strings with zero-chars (byte with value 0).
A string with 4 characters ( 65,66,67,0 ) will on_message be trimmed to 65,66,67
Even worse a string with 0,65,66,67,0 will be trimmed to an empty string.
As my whole network code is utilising messages I really would like the strings to be kept as is.
Is that possible to fix??? Also when handling network messages there must be a way to pass binary data using scripts without risking this.
For testing try this script:
function init(self)
local str = ""
str = string.char(65) .. string.char(66) .. string.char(67) .. string.char(0)
print(str, string.len(str))
msg.post(".", "send_string", { str = str })
end
function on_message(self, message_id, message, sender)
print("len:",string.len(message.str) )
end
Result:
DEBUG:SCRIPT: ABC 4
DEBUG:SCRIPT: len: 3