How would I implement an event registry? #262
-
Hi, I'm using rlua for a plugin system where I need to expose event handler functions. I wish to achieve this by capturing a set of predefined globals. If they're functions, call them when ever the corresponding event occurs. Take the following lua code: function on_event_1(event)
print("Event 1")
end
function not_an_event_handler()
print("Not an event handler")
end
I'd capture the handlers like so: lua.context(|ctx| {
// ...
ctx.load(/*code*/).exec();
if let Ok(handler) = ctx.globals().get::<_, rlua::Function>("on_event_1") {
self.on_event_1 = handler;
}
}); But I get issues because the My question would be about what the best way to achieve this would be? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi, Chris |
Beta Was this translation helpful? Give feedback.
-
Hi Chris, Thanks for your quick reply. I ended up using your suggestion and it's working well. (my project isn't huge yet so it was an easy implementation) Thanks |
Beta Was this translation helpful? Give feedback.
-
Great, glad to hear it! |
Beta Was this translation helpful? Give feedback.
Hi,
I think what you want is probably Context::create_registry_value to create a reference you can store outside of the context, along with Context::registry_value to retrieve it when needed.
Chris