makspll / bevy_mod_scripting

Bevy Scripting Plugin
Apache License 2.0
391 stars 30 forks source link

Can't run wrappers example #64

Closed LaserWitch closed 1 year ago

LaserWitch commented 1 year ago

I've cloned the repository locally and tried to run the wrappers example from it with cargo run --example wrappers --features="lua lua54 lua_script_api". I get this result after the initial startup info line

ERROR bevy_mod_scripting_lua: Runtime error in script `script.lua` callback error
stack traceback:
        [C]: in field 'make_ref_to_my_resource'
        [string "script.lua"]:3: in function 'once'
caused by: error converting Lua nil to userdata
After script run: MyThing {
    usize: 420,
    string: "I live in the bevy world, you can't touch me!",
}

I've tried this test on both the main branch and the 0.11 branch with the same result

If I read the code correctly, I believe it's supposed to error in the second script run, but not the first. It appears that the issue comes from


fn "make_ref_to_my_resource" => |ctx,()| {
    let globals = ctx.globals();
    let lua_world : LuaWorld = globals.("world")?; //This lineA

I changed it to

let lua_world_o = globals.get("world");
info!("{:#?}",lua_world_o);
let lua_world  :LuaWorld = lua_world_o.unwrap();

and got

wrappers: 
INFO wrappers: Err(
    FromLuaConversionError {
        from: "nil",
        to: "userdata",
        message: None,
    },
)
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromLuaConversionError { from: "nil", to: "userdata", message: None }', examples/wrappers.rs:69:52
)

Getting access to the world like this is really the thing I need most out of the crate right now, so any help would be very appreciated.

makspll commented 1 year ago

Hi I'll have a look! I was just looking at finalizing and releasing: https://github.com/makspll/bevy_mod_scripting/pull/63, so if there is an issue in that example it will be fixed on main very soon.

makspll commented 1 year ago

I think the issue is that "world" as a global has been moved into the BevyAPIProvider, which needs to be installed as in the bevy api examples

LaserWitch commented 1 year ago

I can confirm that your latest changes on that branch make it run properly on my machine. Thanks!

makspll commented 1 year ago

lovely, thanks for letting me know and good catch!

LaserWitch commented 1 year ago

Resolved as of the 0.11 branch merge