Skip to content

added serde(default) to Transform#23474

Open
GageHowe wants to merge 6 commits intobevyengine:mainfrom
GageHowe:add-serde-default-to-transform
Open

added serde(default) to Transform#23474
GageHowe wants to merge 6 commits intobevyengine:mainfrom
GageHowe:add-serde-default-to-transform

Conversation

@GageHowe
Copy link
Copy Markdown

@GageHowe GageHowe commented Mar 22, 2026

Objective

Need serde(Default) for Transform. I think it would be very useful in cases where someone wants to add an object to their scene but doesn't want to have to specify rotation or scale, e.g.:

5: (
  components: {
    "common::types::GameObjectKind": Rifle,
    "bevy_transform::components::transform::Transform": (
      translation: (1.5, 800.0, 0.0),
    ),
  },
),

Solution

Added #[cfg_attr(feature = "serialize", serde(default))] before the Transform struct.

Testing

Successfully built bevy, but haven't tested extensively. Theoretically shouldn't break existing functionality, since Deriving Transform was invalid before this PR.

@github-actions
Copy link
Copy Markdown
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide, as well as our policy regarding AI usage, and we look forward to reviewing your pull request shortly ✨

@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Transform Translations, rotations and scales X-Contentious There are nontrivial implications that should be thought through D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed X-Contentious There are nontrivial implications that should be thought through S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 23, 2026
@alice-i-cecile
Copy link
Copy Markdown
Member

Deriving Transform was invalid before this PR

I think this was a typo, but I can't figure out what you mean here.

I think this is a reasonable idea, but I'd be more comfortable with this if there was tests in place for the desired functionality.

@GageHowe
Copy link
Copy Markdown
Author

I think this was a typo, but I can't figure out what you mean here.

Just that omitting, say, scale, was considered invalid and would be a ron parsing error, so I don't imagine this would break any existing functionality.

I'm a bit new. How should I go about making tests?

@alice-i-cecile
Copy link
Copy Markdown
Member

So, for unit tests in Rust use should make a mod tests module inside of the file that you're testing.

Then, inside of that module, you can define a test function, annotated with #[test].

In this case, we should be testing round-trip serialization with partially and fully omitted fields. @andriyDev may have more advice about the best way to do this.

Here's an example of a similar test from one of my crates: https://github.com/Leafwing-Studios/leafwing_manifest/blob/f5a2da1c742babe14d3c4816d4769bfa145e3b47/examples/raw_manifest.rs#L164

I think you should be able to get away without writing it to disk, and should prefer doing so if possible. Storing the string in memory for serialization-deserialization should work fine.

@GageHowe
Copy link
Copy Markdown
Author

How's this? All tests pass with cargo test -p bevy_transform --features serialize

Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, thanks for the tests. @cart, how does stuff like this align with your vision of .bsn assets? I know you have similar "fill in the blank" functionality intended there.

Copy link
Copy Markdown
Member

@cart cart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on board for this / see no harm in it. I view it as being similar, but orthogonal to the BSN design, which should not be using serde for this.

pub struct TransformTreeChanged;

#[cfg(all(test, feature = "serialize"))]
mod tests {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove these tests. They are testing ron's implementation and usage of serde(default), not code that we own or maintain. This is a waste of compute resources that could be spent elsewhere (or not spent at all).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alice-i-cecile thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we can cut them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Transform Translations, rotations and scales C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants