Skip to content

How can I pass in a global table, allow user to modify it via script and then read the updated values from Rust? #370

Answered by khvzak
jamesmcl113 asked this question in Q&A
Discussion options

You must be logged in to vote

To get access to the Config fields, you need to implement accessors (yet; future mlua versions will have a derive macro for this)
For your case the code example would be:

#[derive(Serialize, Deserialize, Clone, mlua::FromLua)]
pub struct Config {
    a: i32,
    b: String,
}

impl mlua::UserData for Config {
    fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) {
        fields.add_field_method_get("a", |_, this| Ok(this.a));
        fields.add_field_method_get("b", |_, this| Ok(this.b.clone()));
        fields.add_field_method_set("a", |_, this, a| {
            this.a = a;
            Ok(())
        });
        fields.add_field_method_set("b", |_, this, b| {

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jamesmcl113
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants