bevy/crates
Hennadii Chernyshchyk 71f8b4a92f Use default serde impls for Entity (#6194)
# Objective

Currently for entities we serialize only `id`. But this is not very expected behavior. For example, in networking, when the server sends its state, it contains entities and components. On the client, I create new objects and map them (using `EntityMap`) to those received from the server (to know which one matches which). And if `generation` field is missing, this mapping can be broken. Example:

1. Server sends an entity `Entity{ id: 2, generation: 1}` with components.
2. Client puts the received entity in a map and create a new entity that maps to this received entity. The new entity have different `id` and `generation`. Let's call it `Entity{ id: 12, generation: 4}`.
3. Client sends a command for `Entity{ id: 12, generation: 4}`. To do so, it maps local entity to the one from server. But `generation` field is 0 because it was omitted for serialization on the server. So it maps to `Entity{ id: 2, generation: 0}`.
4. Server receives `Entity{ id: 2, generation: 0}` which is invalid.

In my game I worked around it by [writing custom serialization](https://github.com/dollisgame/dollis/blob/master/src/core/network/entity_serde.rs) and using `serde(with = "...")`. But it feels like a bad default to me.

Using `Entity` over a custom `NetworkId` also have the following advantages:

1. Re-use `MapEntities` trait to map `Entity`s in replicated components.
2. Instead of server `Entity <-> NetworkId ` and `Entity <-> NetworkId`, we map entities only on client.
3. No need to handling uniqueness. It's a rare case, but makes things simpler. For example, I don't need to query for a resource to create an unique ID.

Closes #6143.

## Solution

Use default serde impls. If anyone want to avoid wasting memory on `generation`, they can create a new type that holds `u32`. This is what Bevy do for [DynamicEntity](https://docs.rs/bevy/latest/bevy/scene/struct.DynamicEntity.html) to serialize scenes. And I don't see any use case to serialize an entity id expect this one.

---

## Changelog

### Changed

- Entity now serializes / deserializes `generation` field.

## Migration Guide

- Entity now fully serialized. If you want to serialze only `id`, as it was before, you can create a new type that wraps `u32`.
2022-10-28 22:21:30 +00:00
..
bevy_animation add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
bevy_app add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
bevy_asset fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_audio Expose rodio's Source and Sample traits in bevy_audio (#6374) 2022-10-27 12:34:38 +00:00
bevy_core add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
bevy_core_pipeline Support arbitrary RenderTarget texture formats (#6380) 2022-10-26 23:12:12 +00:00
bevy_derive Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_diagnostic Add Exponential Moving Average into diagnostics (#4992) 2022-10-24 13:46:37 +00:00
bevy_dylib Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_dynamic_plugin Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_ecs Use default serde impls for Entity (#6194) 2022-10-28 22:21:30 +00:00
bevy_ecs_compile_fail_tests fix: specify required trybuild patch version (#6333) 2022-10-25 10:21:31 +00:00
bevy_encase_derive Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_gilrs feat: add GamepadInfo, expose gamepad names (#6342) 2022-10-24 14:33:50 +00:00
bevy_gltf fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_hierarchy Add set_parent and remove_parent to EntityCommands (#6189) 2022-10-24 14:33:49 +00:00
bevy_input Derive Reflect + FromReflect for input types (#6232) 2022-10-26 19:52:20 +00:00
bevy_internal Use plugin setup for resource only used at setup time (#6360) 2022-10-25 22:19:34 +00:00
bevy_log Update tracing-chrome to 0.6.0 (#6398) 2022-10-28 21:51:38 +00:00
bevy_macro_utils fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_math Add Camera::viewport_to_world (#6126) 2022-10-05 22:16:26 +00:00
bevy_mikktspace Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_pbr fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_ptr Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_reflect fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_render fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_scene fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_sprite fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_tasks fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_text fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_time elaborate on Timer docs (#6385) 2022-10-27 15:48:29 +00:00
bevy_transform Fix documentation for looking_at/look_at (#4696) 2022-10-12 20:18:20 +00:00
bevy_ui fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00
bevy_utils Remove Sync bound from Local (#5483) 2022-09-12 04:15:55 +00:00
bevy_window Plugins own their settings. Rework PluginGroup trait. (#6336) 2022-10-24 21:20:33 +00:00
bevy_winit fix nightly clippy warnings (#6395) 2022-10-28 21:03:01 +00:00