bevy_mod_js_scripting icon indicating copy to clipboard operation
bevy_mod_js_scripting copied to clipboard

Include Function Bindings Through a New `ReflectMethods` Solution

Open zicklag opened this issue 3 years ago • 7 comments

It could be nice to provide bindings to glam so that we would get the same math API even in scripts.

zicklag avatar Aug 08 '22 17:08 zicklag

Should this just work because Vec3 implements Reflect as a struct, so transform.translation.x should result in an f32 value without having to hardcode anything specifically for glam?

jakobhellermann avatar Aug 08 '22 17:08 jakobhellermann

That does just work, but I don't think we get features like being able to multiply/add two vectors together, or functions like lerp(), length(), etc.

Thinking about things like this has made me wonder whether Reflect should allow us to call functions dynamically, too.

For instance there could be like a reflect_call where you pass it a path to a function, and you pass it a vector of objects that implement Reflect to use as arguments.

That would make this work for everything, not just glam, without any extra work.

zicklag avatar Aug 08 '22 17:08 zicklag

Calling methods on reflected values is something that I want to enable at some point. IMO that reflect_call shouldn't be on the Reflect trait itself, but in a struct (ReflectMethods that has a HashMap<&'static str, Box<dyn Fn(?)>>) that can be put into the TypeRegistry (similar to ReflectSerialize and ReflectDefault).

This can even be done in an external crate first and upstreamed into bevy later when the design is proven to work nicely.

jakobhellermann avatar Aug 08 '22 17:08 jakobhellermann

IMO that reflect_call shouldn't be on the Reflect trait itself,

I was wondering if a new trait would be better.

This can even be done in an external crate first and upstreamed into bevy later when the design is proven to work nicely.

Perfect. That means we can experiment without waiting for a Bevy release. This is getting exciting!

I've been trying to get powerful modding support into game engines since I started with Godot 5 years ago, so all this is a big deal for me!

zicklag avatar Aug 08 '22 17:08 zicklag

I started a small repro for trying out data representations for reflect method: https://github.com/jakobhellermann/bevy_reflect_fns If you have any comments on it please let me know :)

The next stop would be to try to integrate ReflectMethods into some scripting integration crate to see how it works in practice, and then look into upstreaming it.

jakobhellermann avatar Aug 09 '22 08:08 jakobhellermann

Initial implementation for function calls is done (excluding mutable references). Here's an example of a function call in script: https://github.com/jakobhellermann/bevy_mod_js_scripting/blob/399e1a05877b5bd9d648dfae746738cfdb426bad/assets/scripts/headless.ts#L32-L37

Next step is trying out some more methods (I'd like Input<KeyCode>::pressed to work) and maybe build a small camera controller purely in a script to make sure everything works.

jakobhellermann avatar Aug 12 '22 23:08 jakobhellermann

Amazing work!

maybe build a small camera controller purely in a script to make sure everything works.

That sounds like a great demo idea.

zicklag avatar Aug 12 '22 23:08 zicklag