We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I can capture rust variable with chunk! macro like
chunk!
let name = "Rustacean"; lua.load(chunk! { print("hello, " .. $name) }).exec()
But what if I have external script inside a file script.lua, is it able to capture rust mutable reference to Lua?
script.lua
The text was updated successfully, but these errors were encountered:
My proximate solution is like
main.rs:
main.rs
use std::{path::Path, fmt::Display}; use mlua::{Lua, UserData}; #[derive(Clone)] struct MyStruct(i32); impl Display for MyStruct { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } } impl UserData for MyStruct { fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) { fields.add_field_method_get("val", |_, this| { Ok(this.0) }); } fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) { methods.add_method_mut("add", |_, this, value: i32| { this.0 += value; Ok(()) }); } } fn main() -> Result<(), Box<dyn std::error::Error>> { let lua = Lua::new(); let mut my_struct = MyStruct(12); for _ in 0..5 { lua.globals().set("MyStruct", my_struct)?; lua.load(Path::new("script.lua")).exec()?; my_struct = lua.globals().get::<&str, MyStruct>("MyStruct")?; } Ok(()) }
script.lua:
local my_struct = MyStruct; print("Old value: ", my_struct.val) my_struct:add(15) print("New value: ", my_struct.val)
Output:
Old value: 12 New value: 27 Old value: 27 New value: 42 Old value: 42 New value: 57 Old value: 57 New value: 72 Old value: 72 New value: 87
Sorry, something went wrong.
No branches or pull requests
I can capture rust variable with
chunk!
macro likeBut what if I have external script inside a file
script.lua
, is it able to capture rust mutable reference to Lua?The text was updated successfully, but these errors were encountered: