bevy_mod_js_scripting
bevy_mod_js_scripting copied to clipboard
Include Function Bindings Through a New `ReflectMethods` Solution
It could be nice to provide bindings to glam so that we would get the same math API even in scripts.
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?
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.
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.
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!
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.
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.
Amazing work!
maybe build a small camera controller purely in a script to make sure everything works.
That sounds like a great demo idea.