Repository navigation
Storing lua functions in rust #444
|
I'm trying to implement an approach where lua code is written like this app.register("start", function (payload)
-- some code
end)
app.register("playerConnect", function (payload)
-- some code
end)
app.register("playerDisonnect", function (payload)
-- some code
end)for this I need to implement the app.register function on the rust side which will save the functions passed to it and match them by name (hashmap). that's what i'm trying to do fn main() {
let mut handlers: HashMap<String, mlua::Function> = HashMap::new();
let lua = Lua::new();
let app = lua.create_table().unwrap();
let register = lua.create_function(move |lua, (name, func): (String, mlua::Function)| {
handlers.insert(name.to_string(), func);
Ok(())
}).unwrap();
app.set("register", register).unwrap();
lua.globals().set("app", app).unwrap();
let script = fs::read_to_string("main.lua").unwrap();
lua.load(script);
handlers.get("start").unwrap().call::<_, ()>(()).unwrap();
}I get the error |
Answered by
Randommist
Sep 1, 2024
Replies: 1 comment
|
the solution is to use |
0 replies
Answer selected by
Randommist
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the solution is to use
lua.create_registry_valueandlua.registry_valuefor more information: #290