Skip to content

Not sure how to load .lua files and their globals into the Lua struct #52

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

You must be logged in to vote

Loading stringified .lua file into Lua globals should work fine, eg:

fn main() {
    let lua = mlua::Lua::new();
    let source = std::fs::read_to_string("file.lua").unwrap();
    lua.load(&source).exec().unwrap();
    let globals = lua.globals();
    let main = globals.get::<_, mlua::Function>("main").unwrap();
    main.call::<_, ()>(()).unwrap();
}

file.lua:

function main()
    print("hello")
end

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@khvzak
Comment options

@TheLeonsver1
Comment options

@zackfall
Comment options

@khvzak
Comment options

Answer selected by TheLeonsver1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants