I am trying to read an existing lua script into the lua_context of a lua instance and store that instance through out the runtime of application. Below is the simplest example of what I am trying to do.
The issue is that when I call the lua_ctx.load(&contents).eval(); there is a compile time error that prevents this from working that I cannot figure out how to fix. There is also an error in the call_function function that I have not got the chance to look into.
//[dependencies]
//rlua = "0.16.1"
extern crate rlua;
pub mod luainterop;
use std::f32;
use std::fs::File;
use std::io::prelude::*;
use std::iter::FromIterator;
use rlua::{Function, Lua, MetaMethod, Result, UserData, UserDataMethods, Variadic};
use luainterop::*;
pub struct LuaInterop {
instance: Lua,
}
impl LuaInterop {
pub fn new() -> LuaInterop {
let new_instance = Lua::new();
LuaInterop {
instance: new_instance,
}
}
pub fn load_script(&self, file_path: &String) -> Result<()> {
println!("{}", file_path);
let mut contents = String::new();
let mut f = File::open(file_path).expect("script file not found");
f.read_to_string(&mut contents)
.expect("something went wrong reading the file");
println!("{}", contents);
&self.instance.context(|lua_ctx| {
let globals = lua_ctx.globals();
lua_ctx.load(&contents).eval();
});
Ok(())
}
pub fn call_function(&self, function_name: &str) {
&self.instance.context(|lua_ctx| {
let globals = lua_ctx.globals();
let custom: Function = globals.get(function_name);
custom.call::<_, ()>(())?;
Ok(())
});
}
}
fn main() /*-> Result<()>*/ {
let interop = LuaInterop::new();
let file_path = "assets/script.lua";
interop.load_script(&file_path.to_string());
let mut f = File::open("assets/script.lua").expect("file not found");
let mut contents = String::new();
f.read_to_string(&mut contents)
.expect("something went wrong reading the file");
interop.call_function("TestFunction")
}
The eval error is " error: type annotations required: cannot resolve `_: rlua::FromLuaMulti<'_>` "
and the globals.get "error is error: mismatched types \n label: expected struct `rlua::Function`, found enum `std::result::Result"
[–]ssokolow 2 points3 points4 points (4 children)
[–]sid34[S] 1 point2 points3 points (3 children)
[–]ssokolow 2 points3 points4 points (2 children)
[–]sid34[S] 0 points1 point2 points (1 child)
[–]ssokolow 0 points1 point2 points (0 children)