makspll / bevy_mod_scripting

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

Small cleaning up + Fix for crash when looping through empty Vec #87

Closed zwazel closed 9 months ago

zwazel commented 10 months ago

This pull request cleans the Rhai side of the project up a bit.

zwazel commented 10 months ago

I suggest we move the existing Wrapper example into lua, as it only really shows how to work with Lua. I'm working on the example for Rhai right now, so we can test the fixes

zwazel commented 10 months ago

btw while working with the Wrappers in Rhai, i noticed that i always have to manually implement RhaiCopy, even though it's completely empty.

#[derive(Resource, Reflect, Default, Clone, Debug)]
#[reflect(Resource, RhaiProxyable)]
pub struct MyThing {
    usize: usize,
    string: String,
    array: Vec<usize>,
}

impl RhaiCopy for MyThing {}

Is this wanted or an oversight?

zwazel commented 10 months ago

As i'm working on the wrapper example for rhai, i noticed following thing: When I Reflect on RhaiProxyable, I no longer can access the values of my Struct. seen in: https://github.com/makspll/bevy_mod_scripting/pull/87/commits/c282ada927a822eb88750f5e669df53421ebe543 When I don't use RhaiProxyable, everything works fine. as seen in: https://github.com/makspll/bevy_mod_scripting/pull/87/commits/59ac5629c7559e82619949ee44ef1cace006541f

Am I misunderstanding the need for RhaiProxyable?

makspll commented 10 months ago

As i'm working on the wrapper example for rhai, i noticed following thing: When I Reflect on RhaiProxyable, I no longer can access the values of my Struct. seen in: c282ada When I don't use RhaiProxyable, everything works fine. as seen in: 59ac562

Am I misunderstanding the need for RhaiProxyable?

So the discussion above might shed some more light on this but let me know if you are still confused. Basically if you don't register RhaiProxyable yourself, you will receive a ReflectedValue type object, which means you are operating using pure reflection, and have no function support.

Registering it yourself is needed when you need a custom proxy type which has custom methods and behaviours attached. But because Rhai doesn't have support for generating those nicely it's not easilly available here yet as seen in lua.

The need for RhaiCopy arises from the automatic implementation for RhaiProxyable which assumes your type is its own proxy and that it is converted into this proxy by copying, hence the name of the trait. This lets you register all Rhai goodies on your type in your APIProvider and use them that way. But of course since you are not using ReflectedValue anymore you lose the automatically generated getters for reflected fields.

Hope this makes sense

zwazel commented 9 months ago

So this pull request should be good now, applied all your suggestions. And am looking forward for the wrapper rewrite ;)