|
| 1 | ++++ |
| 2 | +title = "0.5 to 0.6" |
| 3 | +weight = 1 |
| 4 | +sort_by = "weight" |
| 5 | +template = "book-section.html" |
| 6 | +page_template = "book-section.html" |
| 7 | +[extra] |
| 8 | +long_title = "Migration Guide: 0.5 to 0.6" |
| 9 | ++++ |
| 10 | + |
| 11 | +### "AppBuilder" was merged into "App" |
| 12 | + |
| 13 | +All functions of {{rust_type(type="struct" crate="bevy_app" mod="" version="0.5.0" name="AppBuilder" no_mod=true)}} were merged into {{rust_type(type="struct" crate="bevy_app" mod="" version="0.6.0" name="App" no_mod=true)}}. |
| 14 | + |
| 15 | +In practice this means that you start constructing an {{rust_type(type="struct" crate="bevy_app" mod="" version="0.6.0" name="App" no_mod=true)}} by calling {{rust_type(type="struct" crate="bevy_app" mod="" version="0.6.0" name="App" no_mod=true method="new")}} instead of {{rust_type(type="struct" crate="bevy_app" mod="" version="0.5.0" name="App" no_mod=true method="build")}} and {{rust_type(type="trait" crate="bevy_app" mod="" version="0.5.0" name="Plugin" no_mod=true method="build")}} takes a {{rust_type(type="struct" crate="bevy_app" mod="" version="0.6.0" name="App" no_mod=true)}} instead of a {{rust_type(type="struct" crate="bevy_app" mod="" version="0.5.0" name="AppBuilder" no_mod=true)}} |
| 16 | + |
| 17 | +```rs |
| 18 | +// 0.5 |
| 19 | +fn main() { |
| 20 | + App::build() |
| 21 | + .add_plugin(SomePlugin) |
| 22 | + .run(); |
| 23 | +} |
| 24 | + |
| 25 | +impl Plugin for SomePlugin { |
| 26 | + fn build(&self, app: &mut AppBuilder) { |
| 27 | + |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// 0.6 |
| 32 | +fn main() { |
| 33 | + App::new() |
| 34 | + .add_plugin(SomePlugin) |
| 35 | + .run(); |
| 36 | +} |
| 37 | + |
| 38 | +impl Plugin for SomePlugin { |
| 39 | + fn build(&self, app: &mut App) { |
| 40 | + |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### The "Component" trait now needs to be derived |
| 46 | + |
| 47 | +Bevy no longer has a blanket implementation for the {{rust_type(type="trait" crate="bevy_ecs" mod="component" version="0.6.0" name="Component" no_mod=true)}} trait. |
| 48 | +Instead you need to derive (or manualy implement) the trait for every Type that needs it. |
| 49 | + |
| 50 | +```rust |
| 51 | +// 0.5 |
| 52 | +struct MyComponent; |
| 53 | + |
| 54 | +// 0.6 |
| 55 | +#[derive(Component)] |
| 56 | +struct MyComponent; |
| 57 | +``` |
| 58 | + |
| 59 | +In order to use foreign types as components, wrap them using the newtype pattern. |
| 60 | + |
| 61 | +```rust |
| 62 | +#[derive(Component)] |
| 63 | +struct Cooldown(std::time::Duration); |
| 64 | +``` |
| 65 | + |
| 66 | +### Setting the Component Storage is now done in "Component" Trait |
| 67 | + |
| 68 | +The change to deriving {{rust_type(type="trait" crate="bevy_ecs" mod="component" version="0.6.0" name="Component" no_mod=true)}}, enabled setting the Component Storage at compiletime instead of runtime. |
| 69 | + |
| 70 | +```rust |
| 71 | +// 0.5 |
| 72 | +appbuilder |
| 73 | + .world |
| 74 | + .register_component(ComponentDescriptor::new::<MyComponent>( |
| 75 | + StorageType::SparseSet, |
| 76 | + )) |
| 77 | + .unwrap(); |
| 78 | + |
| 79 | +// 0.6 |
| 80 | +#[derive(Component)] |
| 81 | +#[component(storage = "SparseSet")] |
| 82 | +struct MyComponent; |
| 83 | +``` |
| 84 | + |
| 85 | +### Calling ".system()" on a system is now optional |
| 86 | + |
| 87 | +When adding a system to Bevy it is no longer necessary to call `.system()` beforehand. |
| 88 | + |
| 89 | +```rust |
| 90 | +// 0.5 |
| 91 | +fn main() { |
| 92 | + App::build() |
| 93 | + .add_system(first_system.system()) |
| 94 | + .add_system(second_system.system()) |
| 95 | + .run(); |
| 96 | +} |
| 97 | + |
| 98 | +// 0.6 |
| 99 | +fn main() { |
| 100 | + App::new() |
| 101 | + .add_system(first_system) |
| 102 | + .add_system(second_system.system()) |
| 103 | + .run(); |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +System configuration Functions like `.label()` or `.config()` can now also be directly called on a system. |
| 108 | + |
| 109 | +```rust |
| 110 | +// 0.5 |
| 111 | +fn main() { |
| 112 | + App::build() |
| 113 | + .add_system(first_system.system().label("FirstSystem")) |
| 114 | + .add_system(second_system.system().after("FirstSystem")) |
| 115 | + .run(); |
| 116 | +} |
| 117 | + |
| 118 | +// 0.6 |
| 119 | +fn main() { |
| 120 | + App::new() |
| 121 | + .add_system(first_system.label("FirstSystem")) |
| 122 | + .add_system(second_system.after("FirstSystem")) |
| 123 | + .run(); |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### ".single()" and ".single_mut()" are now infallible |
| 128 | + |
| 129 | +The functions {{rust_type(type="struct" crate="bevy_ecs" mod="system" version="0.6.0" name="Query" no_mod=true method="single")}} and {{rust_type(type="struct" crate="bevy_ecs" mod="system" version="0.6.0" name="Query" no_mod=true method="single_mut")}} no longer return a {{rust_type(type="enum", crate="std" mod="result", name="Result", no_mod=true)}} and Panic instead, if not exactly one Entity was found. |
| 130 | + |
| 131 | +If you need the old behavior you can use the fallible {{rust_type(type="struct" crate="bevy_ecs" mod="system" version="0.6.0" name="Query" no_mod=true method="get_single")}} and {{rust_type(type="struct" crate="bevy_ecs" mod="system" version="0.6.0" name="Query" no_mod=true method="get_single_mut")}} instead. |
| 132 | + |
| 133 | +```rs |
| 134 | +// 0.5 |
| 135 | +fn player_system(query: Query<&Transform, With<Player>>) { |
| 136 | + let player_position = query.single().unwrap(); |
| 137 | + // do something with player_position |
| 138 | +} |
| 139 | + |
| 140 | +// 0.6 |
| 141 | +fn player_system_infallible(query: Query<&Transform, With<Player>>) { |
| 142 | + let player_position = query.single(); |
| 143 | + // do something with player_position |
| 144 | +} |
| 145 | + |
| 146 | +fn player_system_fallible(query: Query<&Transform, With<Player>>) { |
| 147 | + let player_position = query.get_single().unwrap(); |
| 148 | + // do something with player_position |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### "Light" and "LightBundle" are now "PointLight" and "PointLightBundle" |
| 153 | + |
| 154 | +```rust |
| 155 | +// 0.5 |
| 156 | +commands.spawn_bundle(LightBundle { |
| 157 | + light: Light { |
| 158 | + color: Color::rgb(1.0, 1.0, 1.0), |
| 159 | + depth: 0.1..50.0, |
| 160 | + fov: f32::to_radians(60.0), |
| 161 | + intensity: 200.0, |
| 162 | + range: 20.0, |
| 163 | + }, |
| 164 | + ..Default::default() |
| 165 | +}); |
| 166 | + |
| 167 | +// 0.6 |
| 168 | +commands.spawn_bundle(PointLightBundle { |
| 169 | + light: PointLight { |
| 170 | + color: Color::rgb(1.0, 1.0, 1.0), |
| 171 | + intensity: 200.0, |
| 172 | + range: 20.0, |
| 173 | + }, |
| 174 | + ..Default::default() |
| 175 | +}); |
| 176 | +``` |
| 177 | + |
| 178 | +The {{rust_type(type="struct" crate="bevy_pbr" mod="" version="0.5.0" name="Light" no_mod=true)}} and {{rust_type(type="struct" crate="bevy_pbr" mod="" version="0.5.0" name="LightBundle" no_mod=true)}} were renamed to {{rust_type(type="struct" crate="bevy_pbr" mod="" version="0.6.0" name="PointLight" no_mod=true)}} and {{rust_type(type="struct" crate="bevy_pbr" mod="" version="0.6.0" name="PointLightBundle" no_mod=true)}} to more clearly communicate the behavior of the Light Source. |
| 179 | +At the same time the `fov` and `depth` fields were removed from the {{rust_type(type="struct" crate="bevy_pbr" mod="" version="0.6.0" name="PointLight" no_mod=true)}} as they were unused. |
| 180 | + |
| 181 | +<!-- TODO: Remove this comment if https://github.com/bevyengine/bevy/pull/2367 is merged. |
| 182 | +
|
| 183 | +In addition the {{rust_type(type="struct" crate="bevy_pbr" mod="" version="0.6.0" name="DirectionalLight" no_mod=true)}} and {{rust_type(type="struct" crate="bevy_pbr" mod="" version="0.6.0" name="DirectionalLightBundle" no_mod=true)}} were introduced with `0.6`. |
| 184 | +
|
| 185 | +--> |
| 186 | + |
| 187 | +### System Param Lifetime Split |
| 188 | + |
| 189 | +The Lifetime of {{rust_type(type="trait" crate="bevy_ecs" mod="system" version="0.5.0" name="SystemParam" no_mod=true)}} was split in two separate Lifetimes. |
| 190 | + |
| 191 | +```rust |
| 192 | +// 0.5 |
| 193 | +type SystemParamAlias<'a> = (Res<'a, AssetServer>, Query<'a, &'static Transform>, Local<'a, i32>); |
| 194 | + |
| 195 | +#[derive(SystemParam)] |
| 196 | +struct SystemParamDerive<'a> { |
| 197 | + res: Res<'a, AssetServer>, |
| 198 | + query: Query<'a, &Transform>, |
| 199 | + local: Local<'a, i32>, |
| 200 | +} |
| 201 | + |
| 202 | +// 0.6 |
| 203 | +type SystemParamAlias<'w, 's> = (Res<'w, AssetServer>, Query<'w, 's, &'static Transform>, Local<'s, i32>); |
| 204 | + |
| 205 | +#[derive(SystemParam)] |
| 206 | +struct SystemParamDerive<'w, 's> { |
| 207 | + res: Res<'w, AssetServer>, |
| 208 | + query: Query<'w, 's, &'static Transform>, |
| 209 | + local: Local<'s, i32>, |
| 210 | +} |
| 211 | +``` |
| 212 | + |
| 213 | +### QuerySet declare "QueryState" instead of "Query" |
| 214 | +<!-- Adapt for ParamSet instead, if https://github.com/bevyengine/bevy/pull/2765 is merged --> |
| 215 | + |
| 216 | +Due to the [System Param Lifetime Split](#system-param-lifetime-split), {{rust_type(type="struct" crate="bevy_ecs" mod="system" name="QuerySet" version="0.6.0" no_mod=true plural=true)}} now need to specify their Queries with {{rust_type(type="struct" crate="bevy_ecs" mod="query" version="0.6.0" name="QuerySet" no_mod=true)}} instead of {{rust_type(type="struct" crate="bevy_ecs" mod="system" version="0.6.0" name="Query" no_mod=true)}}. |
| 217 | + |
| 218 | +```rust |
| 219 | +// 0.5 |
| 220 | +fn query_set(mut queries: QuerySet<(Query<&mut Transform>, Query<&Transform>)>) { |
| 221 | + |
| 222 | +} |
| 223 | + |
| 224 | +// 0.6 |
| 225 | +fn query_set(mut queries: QuerySet<(QueryState<&mut Transform>, QueryState<&Transform>)>) { |
| 226 | + |
| 227 | +} |
| 228 | +``` |
| 229 | + |
| 230 | +### "Input\<T\>.update()" is renamed to "Input\<T\>.clear()" |
| 231 | + |
| 232 | +The {{rust_type(type="struct" crate="bevy_input" mod="" version="0.5.0" name="Input" no_mod=true method="update")}} function was renamed to {{rust_type(type="struct" crate="bevy_input" mod="" version="0.6.0" name="Input" no_mod=true method="clear")}}. |
| 233 | + |
| 234 | +### "SystemState" is now "SystemMeta" |
| 235 | + |
| 236 | +The {{rust_type(type="struct" crate="bevy_ecs" mod="system" version="0.5.0" name="SystemState" no_mod=true)}} struct, which stores the metadata of a System, was renamed to {{rust_type(type="struct" crate="bevy_ecs" mod="system" version="0.6.0" name="SystemMeta" no_mod=true)}}. |
| 237 | + |
| 238 | +This was done to accommodate the new {{rust_type(type="struct" crate="bevy_ecs" mod="system" version="0.6.0" name="SystemState" no_mod=true)}} which allows easier cached access to {{rust_type(type="trait" crate="bevy_ecs" mod="system" version="0.6.0" name="SystemParam" no_mod=true plural=true)}} outside of a regular System. |
| 239 | +<!-- TODO: Link to entry for SystemState in the release blog post. --> |
0 commit comments