Pass table as reference through msg (SOLVED)

Is it possible to pass a table as a reference from one script to another using msg.post? I’m currently using the code below which seems to create a new copy of the table for the target script.

local data = {}
msg.post("#other_script", "message", {table = data})
1 Like

No, msg.post will encode/decode your message payload, so your table will be copied with another reference

There is several workarounds: for example, make data module with tables, store message data here and pass table key in message to get value in another place from this module. But be aware of different contexts (gui./go. functions, possible other stuff)

4 Likes

That’s actually the desired effect of messaging. The table is “posted” to the target script. If that’s not what you want, as insality notes, you are probably looking for a Lua Module.

You could also send various messages as and when it’s necessary. Messaging is super efficient and a great way of sharing data between scripts.

Good luck!

3 Likes

Thanks for the advice. My table data was already in a Lua module, so it’s been easy to message pass an index value rather than a reference.

2 Likes