Understanding Lua Environment in Callbacks

Hello everyone, I’m currently exploring the Lua environment within callbacks and have some questions that I hope the community can help clarify.

From what I understand, each device instance runs in its own environment, and local variables defined in the startup are only accessible to that specific instance. This makes sense for isolation, but I’m curious about how this affects local functions. Are they duplicated across multiple instances, or is there a more efficient way to handle them?

Additionally, I’ve noticed that callbacks need to be global, but I’m not entirely sure why. If a callback is triggered from within a device, it runs in that device’s environment. So, why is it necessary for callbacks to be global? I assume the OS sets up the environment, but I’m not entirely clear on the mechanics.

While everything is working as expected, I believe understanding the underlying principles is crucial for optimizing my setup. Any insights or explanations would be greatly appreciated!

Thanks in advance for your help.

Best regards,
Keith