Sending multiple strings in a single event

There may come a time during development when you wish to send multiple strings as part of an event payload.

There are a few ways this can be achieved.

One is by packing multiple 1s0's together like in this example:

--- @param gre#context mapargs
function CBTestMultipleStrings(mapargs)
  local event = {}
  event.name = "MultipleStrings"
  event.format = "1s0 FirstName 1s0 LastName"
  event.data = {FirstName="Daniel", LastName="Gonzalez"}
  gre.send_event(event)
end


--- @param gre#context mapargs
function ReceiveMultipleStrings(mapargs)
  local event_data = mapargs.context_event_data
  for k,v in pairs(event_data) do
    print(k,v)
  end
end

This will print:

FirstName	Daniel
LastName	Gonzalez

However, strings don't always need to be defined as 1s0's.

For example, If you know that your strings will not exceed a certain size, you can simply specify that maximum size. e.g. if none of your strings will exceed 32 bytes, you can simply change them to 1s32 instead.

Another, more streamlined approach would be to pack all of the strings into a single 1s0. From there you can easily parse the string in Lua to separate the individual strings out again. 

This approach enables the use of certain utilities like JSON to perform this action for you. But it is entirely up to you how you wish to approach it.

 

 

 

0

Comments

0 comments

Please sign in to leave a comment.

Didn't find what you were looking for?

New post