-- client.lua
CreateThread(function()
-- if I add another 5th argument, arg2 is no longer nil
TriggerServerEvent('testing', 'mymessage', nil, 'foo')
end)
Tables are packed with the ‘without_hole’ configuration (adopted from MessagePack.lua), meaning arrays with nil values are to be encoded as maps. This generally conflicts with parameter sequences that use table.pack/table.unpack.
I’ve had a fix for this laying around for a while now (see below). However, I was considering moving this implementation directly to the msgpack library (e.g., a special function that overrides the default flags) to avoid the intermediate table creation.
msgpack.setoption('sentinel', false) -- Only allow encoding sentinels (decoding creates nils)
-- Replace nil's in varargs w/ msgpack sentinels
local select = select
local msgpack_null = msgpack.null
local function MsgpackSanitize(...)
local t = {...}
for i=1,select("#", ...) do
if t[i] == nil then
t[i] = msgpack_null
end
end
return t
end
-- Usage: msgpack.pack(MsgpackSanitize(...))
I was wondering about the same (patching the event or MessagePack)
I was actually going to suggest something similar, but using two tables: the original table, and nulls, instead of a placeholder since I wasn’t sure it could get mixed up with valid occurrences. Something very drafty:
local function test(...)
local t, n = {...}, select('#', ...)
local nulls = {}
for k = 1, n do
if t[k] == nil then
table.insert(nulls, k)
end
end
return t, nulls
end
And then eventually restore the original null positioning as enumerated arguments.
Anyway, that sounds good to me! Thanks for the quick reaction.