makspll / bevy_mod_scripting

Bevy Scripting Plugin
Apache License 2.0
411 stars 31 forks source link

Panic when mutating world from proxied raw method #115

Open Joakker opened 7 months ago

Joakker commented 7 months ago

As the title says, when altering the world from the world pointer you can get from the context argument in the raw method, it yields a panic


#[derive(Resource, Reflect, LuaProxy)
#[reflect(Resource, LuaProxy)]
#[
    derive(clone),
    functions[
        r#"
            #[lua(kind = "Method", raw)]
            fn update(&self, ctx: &Lua) -> Result<_, _> {
                let world = ctx.get_world();
                let world = world.write();

               // This is where it panics
                world.get_resource_mut::<SomethingElse>();
             }
        "#
    ]
]
struct MyProxy {
   cool_thing: String
}

I tried with the blocking and non blocking methods but to no avail

makspll commented 7 months ago

Hi, what is the exact error message?

Joakker commented 7 months ago

Concurrent read/write world access

makspll commented 7 months ago

I think it may be that the macro blocks access to the world earlier in that generated function, this may be avoidable in raw functions as they do not necessarily need access to the world

Joakker commented 7 months ago

That's precisely the problem. In my game there is a Controller component, which represents what faction a piece belongs to. I put a give_money() proxy method, so I need to reference and mutate the object that represents that faction