bevy_mod_scripting icon indicating copy to clipboard operation
bevy_mod_scripting copied to clipboard

Expose the proxy types for Bevy builtins

Open Joakker opened this issue 1 year ago • 10 comments

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.

Joakker avatar May 18 '24 15:05 Joakker

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

makspll avatar May 18 '24 16:05 makspll

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

Joakker avatar May 18 '24 18:05 Joakker

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

makspll avatar May 18 '24 22:05 makspll

image

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

Joakker avatar May 19 '24 00:05 Joakker

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)
   }
}

makspll avatar May 19 '24 12:05 makspll

Nope, exactly the same error image

Joakker avatar May 19 '24 19:05 Joakker

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

makspll avatar May 19 '24 20:05 makspll

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

Joakker avatar May 19 '24 20:05 Joakker

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

makspll avatar May 19 '24 20:05 makspll

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

Joakker avatar May 19 '24 20:05 Joakker

Closing as these macros no longer exist since 0.9.0!

makspll avatar Feb 01 '25 19:02 makspll