|
| 1 | ++++ |
| 2 | +title = "0.4 to 0.5" |
| 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.4 to 0.5" |
| 9 | ++++ |
| 10 | + |
| 11 | +<!-- TODO: link to release blog post here --> |
| 12 | + |
| 13 | +## `commands: &mut Commands` SystemParam is now `mut commands: Commands` |
| 14 | + |
| 15 | +```rust |
| 16 | +// 0.4 |
| 17 | +fn foo(commands: &mut Commands) { |
| 18 | +} |
| 19 | + |
| 20 | +// 0.5 |
| 21 | +fn foo(mut commands: Commands) { |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Systems using the old `commands: &mut Commands` syntax in 0.5 will fail to compile when calling `foo.system()`. |
| 26 | + |
| 27 | +This change was made because {{rust_type(type="struct" crate="bevy_ecs" version="0.5.0" name="Commands" no_mod=true)}} |
| 28 | +now holds an internal {{rust_type(type="struct" crate="bevy_ecs" version="0.5.0" name="World" no_mod=true)}} |
| 29 | +reference to enable safe entity allocations. |
| 30 | + |
| 31 | +Note: The internal {{rust_type(type="struct" crate="bevy_ecs" version="0.5.0" name="World" no_mod=true)}} reference requires two lifetime parameters to pass Commands into a non-system function: ```commands: &'a mut Commands<'b>``` |
| 32 | + |
| 33 | +## {{rust_type(type="struct" crate="bevy_ecs" version="0.5.0" name="Commands" no_mod=true)}} `insert()` API is now used for a single component |
| 34 | + |
| 35 | +```rust |
| 36 | +// 0.4 |
| 37 | +// component |
| 38 | +commands.insert_one(entity, MyComponent) |
| 39 | +commands.insert(entity, (MyComponent,)) |
| 40 | +// bundle |
| 41 | +commands.insert(entity, Bundle) |
| 42 | + |
| 43 | + |
| 44 | +// 0.5 |
| 45 | +// component |
| 46 | +commands.insert(entity, MyComponent) |
| 47 | +// bundle |
| 48 | +commands.insert_bundle(entity, MyBundle) |
| 49 | +``` |
| 50 | + |
| 51 | +Instead of using `commands.insert_one()` for a single component, use `commands.insert()`. |
| 52 | + |
| 53 | +This means that `commands.insert()` will no longer accept a bundle as an argument. For bundles, use `commands.insert_bundle()`. |
| 54 | + |
| 55 | +This change helps to clarify the difference between components and bundles, and brings {{rust_type(type="struct" crate="bevy_ecs" version="0.5.0" name="Commands" no_mod=true)}} into alignment with other Bevy APIs. It also eliminates the confusion associated with calling `commands.insert()` on a tuple for the single-component case. |
| 56 | + |
| 57 | +## {{rust_type(type="struct" crate="bevy_core" version="0.5.0" name="Timer" no_mod=true)}} uses `Duration` |
| 58 | + |
| 59 | +```rust |
| 60 | +// 0.4 |
| 61 | +if timer.tick(time.delta_seconds()).finished() { /* do stuff */ } |
| 62 | +timer.elapsed() // returns a bool |
| 63 | + |
| 64 | +// 0.5 |
| 65 | +if timer.tick(time.delta()).finished() { /* do stuff */ } |
| 66 | +timer.elapsed() // returns a `Duration` |
| 67 | +``` |
| 68 | + |
| 69 | +Most of the methods of {{rust_type(type="struct" crate="bevy_core" version="0.5.0" name="Timer" no_mod=true)}} |
| 70 | +now use `Duration` instead of `f32`. |
| 71 | + |
| 72 | +This change allows timers to have consistent, high precision. For convenience, there is also an |
| 73 | +`elapsed_secs` method that returns `f32`. Otherwise, when you need an `f32`, use the |
| 74 | +`as_secs_f32()` method on `Duration` to make the conversion. |
| 75 | + |
| 76 | +## Simplified Events |
| 77 | + |
| 78 | +```rust |
| 79 | +// 0.4 |
| 80 | +fn event_reader_system( |
| 81 | + mut my_event_reader: Local<EventReader<MyEvent>>, |
| 82 | + my_events: Res<Events<MyEvent>>, |
| 83 | +) { |
| 84 | + for my_event in my_event_reader.iter(&my_events) { |
| 85 | + // do things with your event |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// 0.5 |
| 90 | +fn event_reader_system(mut my_event_reader: EventReader<MyEvent>) { |
| 91 | + for my_event in my_event_reader.iter() { |
| 92 | + // do things with your event |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | +You no longer need two system parameters to read your events. One `EventReader` is sufficient. |
| 97 | + |
| 98 | +Following the above example of using an `EventReader` to read events, you can now use `EventWriter` to create new ones. |
| 99 | +```rust |
| 100 | +// 0.4 |
| 101 | +fn event_writer_system( |
| 102 | + mut my_events: ResMut<Events<MyEvent>>, |
| 103 | +) { |
| 104 | + my_events.send(MyEvent); |
| 105 | +} |
| 106 | + |
| 107 | +// 0.5 |
| 108 | +fn event_writer_system( |
| 109 | + mut my_events: EventWriter<MyEvent> |
| 110 | +) { |
| 111 | + my_events.send(MyEvent); |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## `AppBuilder::add_resource` is now called `AppBuilder::insert_resource` |
| 116 | + |
| 117 | +This is a small change to have function names on `AppBuilder` consistent with the `Commands` API. |
| 118 | + |
| 119 | +## TextBundle |
| 120 | + |
| 121 | +This bundle has been reworked to allow multiple differently-styled sections of text within a single bundle. `Text::with_section` was added to simplify the common case where you're only interested in one text section. |
| 122 | + |
| 123 | +```rust |
| 124 | +// 0.4 |
| 125 | +TextBundle { |
| 126 | + text: Text { |
| 127 | + value: "hello!".to_string(), |
| 128 | + font: asset_server.load("fonts/FiraSans-Bold.ttf"), |
| 129 | + style: TextStyle { |
| 130 | + font_size: 60.0, |
| 131 | + color: Color::WHITE, |
| 132 | + ..Default::default() |
| 133 | + }, |
| 134 | + }, |
| 135 | + ..Default::default() |
| 136 | +} |
| 137 | + |
| 138 | +// 0.5 |
| 139 | +TextBundle { |
| 140 | + text: Text::with_section( |
| 141 | + "hello!", |
| 142 | + TextStyle { |
| 143 | + font: asset_server.load("fonts/FiraSans-Bold.ttf"), |
| 144 | + font_size: 60.0, |
| 145 | + color: Color::WHITE, |
| 146 | + }, |
| 147 | + TextAlignment::default() |
| 148 | + ), |
| 149 | + ..Default::default() |
| 150 | +} |
| 151 | +``` |
0 commit comments