MrGVSV / bevy_proto

Create config files for entities in Bevy
Other
239 stars 25 forks source link

Added `#[proto_comp(with=...)]` and `#[proto_comp(into=...)]` attributes #8

Closed MrGVSV closed 2 years ago

MrGVSV commented 2 years ago

Based on this comment, added the following derive helper attributes for ProtoComponent:

#[proto_comp(into = "ActualComponent")]

Allows a From<T> to be used to created the inserted component. This is helpful for components that need a bit of setup (serialization isn't one-to-one), or for components that a user does not own (i.e., from external crates).

#[proto_comp(with = "my_function")]

Allows a common function to be used to generate or insert components. For ProtoComponent structs that share some common trait or otherwise can be inserted a certain way, this attribute allows that to be done in a clean and efficient way (without implementing ProtoComponent manually for each type).

New Examples

Added the attributes example to show off these new attributes.

API Changes

This was not only one of my first Bevy projects, but also one of my first Rust projects! I've learned a lot since then (and I'm still learning now). And coming back to this code, I found some parts that could be improved.

But the ones that stood out to me as "definitely remove" were the #[proto_comp(Copy)] and #[proto_comp(Clone)] field attributes. I realize now, that having a #[proto_comp(Copy)] attribute is almost pointless since a call to clone() will just use Copy internally.

Additionally, by cloning fields individually, we prevent the user from adding additional functionality/logic when cloning a ProtoComponent. It makes much more sense to just clone the entire struct, allowing users to either derive Clone or implement it themselves.

Therefore, the biggest change is that ProtoComponent structs must derive/implement Clone, unless they implement ProtoComponent manually. This was already required in a sense, but now it's more explicit.

Doing this also allows us to now support enums!

TL;DR