Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot deserialize Vec3 in Resources #15726

Closed
mrchantey opened this issue Oct 8, 2024 · 0 comments · Fixed by #15753
Closed

Cannot deserialize Vec3 in Resources #15726

mrchantey opened this issue Oct 8, 2024 · 0 comments · Fixed by #15753
Assignees
Labels
A-Math Fundamental domain-agnostic mathematical operations A-Reflection Runtime information about types C-Bug An unexpected or incorrect behavior
Milestone

Comments

@mrchantey
Copy link
Contributor

Bevy version

0.15.0-dev - rev 0c959f77007c29eead7f902bddd3342a1ecbca20

What you did

Attempted to serialize and then deserialize a resource with a Vec3

What went wrong

A Vec3 is serialized as a 'list' in components but a 'map' in resources. Issue is constistent in both ron and serde_json

(
  resources: {
    "test::MyType": ((
      x: 1.0,
      y: 2.0,
      z: 3.0,
    )),
  },
  entities: {
    4294967296: (
      components: {
        "test::MyType": ((1.0, 2.0, 3.0)),
      },
    ),
  },
)

Full Reproducable

#[cfg(test)]
mod test {
	use anyhow::Result;
	use bevy::prelude::*;
	use bevy::reflect::erased_serde::__private::serde::de::DeserializeSeed;
	use bevy::scene::ron;
	use bevy::scene::serde::SceneDeserializer;
	use bevy::scene::serde::SceneSerializer;

	#[derive(Resource, Component, Reflect)]
	#[reflect(Resource, Component)]
	struct MyType(pub Vec3);

	#[test]
	fn works() -> Result<()> {
		let mut app = App::new();
		app.register_type::<MyType>();

		app.insert_resource(MyType(Vec3::new(1., 2., 3.)));
		let entity = app.world_mut().spawn(MyType(Vec3::new(1., 2., 3.))).id();

		let world = app.world();

		let scene = DynamicSceneBuilder::from_world(world)
			.extract_resources()
			.extract_entities(vec![entity].into_iter())
			.build();

		let registry = app.world().resource::<AppTypeRegistry>().read();
		let serializer = SceneSerializer::new(&scene, &registry);

		let str = ron::ser::to_string_pretty(&serializer, Default::default())?;

		
		let mut deserializer = ron::de::Deserializer::from_str(&str)?;
		
		println!("{}", str);
		SceneDeserializer {
			type_registry: &registry,
		}
		.deserialize(&mut deserializer)?;

		Ok(())
	}
}

Additional information

Possibly related to #15174, some discussion in discord here

@mrchantey mrchantey added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Oct 8, 2024
@LiamGallagher737 LiamGallagher737 added A-Math Fundamental domain-agnostic mathematical operations A-Reflection Runtime information about types and removed S-Needs-Triage This issue needs to be labelled labels Oct 8, 2024
@MrGVSV MrGVSV self-assigned this Oct 8, 2024
@MrGVSV MrGVSV added this to the 0.15 milestone Oct 8, 2024
github-merge-queue bot pushed a commit that referenced this issue Oct 9, 2024
# Objective

Fixes #15726

The extraction logic for components makes use of `FromReflect` to try
and ensure we have a concrete type for serialization. However, we did
not do the same for resources.

The reason we're seeing this for the glam types is that #15174 also made
a change to rely on the glam type's `Serialize` and `Deserialize` impls,
which I don't think should have been merged (I'll put up a PR addressing
this specifically soon).

## Solution

Use `FromReflect` on extracted resources.

## Testing

You can test locally by running:

```
cargo test --package bevy_scene
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations A-Reflection Runtime information about types C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants