Problem using Defold messages with binary data (as strings) (DEF-2821) (SOLVED)

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

1 Like

Hmm, sounds a bit strange, but maybe there are some assumptions made by the engine in regard to the string contents. @Mathias_Westerdahl, any input?

Some workarounds:

  1. Don’t send the string. Put it in a shared Lua table and send the index instead.
  2. Base64 encode the string before sending it.

I looked at it and we misrepresent that string under the hood, thus causing this behavior. I’ve added ticket DEF-2821 for this.

1 Like

Null terminated C strings say hi?

4 Likes

Awesome. I have created a workaround (with bytes as numbers in a table), but if definitely would be nice to have binary-blobs as strings.

1 Like

Fixed in Defold 1.2.116

3 Likes