makspll / bevy_mod_scripting

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

Expose the proxy types for Bevy builtins #122

Open Joakker opened 3 months ago

Joakker commented 3 months ago

I'm trying to create a lua function that takes an entity as argument to perform actions on it from rust. However, the compiler complains that the Entity type doesn't implement IntoLua.

A way of circumventing this specific problem is to call the Entity::to_bits method in lua, then have the function take a number as argument, which then is transformed back with Entity::from_bits in rust, but I feel like it's too much boilerplate for something that could be solved by simply exposing the implementation to the user.

makspll commented 3 months ago

This is already the case, you might be confusing the dummy types in the generated provider files with the proxies which are generated by the macro. All proxies have a Lua prefix in the type name, for entity this is "LuaEntity" which you can use in your code to interact with the Builtins

Joakker commented 3 months ago

I may have expressed myself wrong. Here's what I want to do


#[derive(..., Reflect, LuaProxy)]
#[reflect(LuaProxyable)]
#[proxy(
    derive(clone),
   function[
        r#"
            // Error: LuaEntity not in this scope!
            #[lua(kind = "MutatingMethod")]
            fn add(&mut self, #[proxy] character: Entity) { self.0.push(character) }
        "#
   ]
)]
pub struct GarrisonedCharacter(Vec<LuaEntity>)

I tried using just the Entity, but LuaProxy complains that Entity doesn't implement IntoLua, so I can't use either

makspll commented 3 months ago

Hmm this should already work, is the Entity type imported and in scope? What's the exact error? And can you try proxying without an inlined body? (I.e. implement this function in rust fully)

Edit: oh but in your actual struct you should be working with Entities not LuaEntities

Joakker commented 3 months ago

image

Entity is imported through use bevy::prelude::*;

makspll commented 3 months ago

Try:


 #[derive(..., Reflect, LuaProxy)]
 #[reflect(LuaProxyable)]
 #[proxy(
     derive(clone),
    function[
         r#"
             #[lua(kind = "MutatingMethod")]
            fn(&mut self, #[proxy] entity: Entity)
         "#
    ]
 )]
 pub struct GarrisonedCharacter(Vec<Entity>)

Impl GarrisonedCharacter {

    pub fn add(&mut self, entity: Entity) {
         self.0.push(entity)
    }
}
Joakker commented 3 months ago

Nope, exactly the same error image

makspll commented 3 months ago

Oh I see the LuaEntity type is not in scope, can you import that

Joakker commented 3 months ago

I can't. That's exactly what I was asking about, since LuaEntity doesn't seem to appear in bevy_mod_scripting, bevy_mod_scripting_lua, bevy_mod_scripting_lua, bevy_mod_scripting_core or bevy_script_api. I had to import all of those to get LuaProxy to work properly

makspll commented 3 months ago

I can't. That's exactly what I was asking about, since LuaEntity doesn't seem to appear in bevy_mod_scripting, bevy_mod_scripting_lua, bevy_mod_scripting_lua, bevy_mod_scripting_core or bevy_script_api. I had to import all of those to get LuaProxy to work properly

Oh try the main branch of the library, it should be a public import there, but it's still unreleased

Joakker commented 3 months ago

Ok, that seems to have worked, thank you 😄

However, the LuaProxy macro is still complaining that Entity doesn't implement FromLua. Any insight on how to deal with this? image