Skip to content

Commit 365e1af

Browse files
committed
scene loading
1 parent 11650b0 commit 365e1af

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

src/_posts/2026-03-31-fyrox-game-engine-1.0.0.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,76 @@ Animation blending state machine editor was improved as well, it is not only the
192192
new animation or renaming one is now performed via new input box control. This is much more intuitive than the old
193193
approach.
194194

195+
# Scene Loading
196+
197+
Scene and UI loading was refactored to be more flexible, `AsyncSceneLoader` was removed and now scene loading is performed
198+
via async tasks. There are three new helper methods in `PluginContext`: `load_scene`, `load_ui`, `load_scene_or_ui`.
199+
200+
The easiest way to load a scene or UI (and the most compact one) is just one line:
201+
202+
```rust
203+
impl Plugin for MyGame {
204+
fn init(&mut self, scene_path: Option<&str>, mut ctx: PluginContext) -> GameResult {
205+
ctx.load_scene_or_ui(scene_path.unwrap_or("data/path/to/scene.rgs"));
206+
Ok(())
207+
}
208+
}
209+
```
210+
211+
This method is used by default in all new projects generated by the project manager. This method automatically adds
212+
the loaded scene or UI to the engine internals.
213+
214+
In some cases you may want more control of how the scene is processed before adding it to the engine. A typical example
215+
of scene loading with full control of the process is something like this now:
216+
217+
218+
```rust
219+
#[derive(Visit, Clone, Reflect, Debug)]
220+
struct MyGame {
221+
main_scene: Handle<Scene>,
222+
}
223+
224+
impl MyGame {
225+
fn load_scene(&mut self, path: &str, ctx: &mut PluginContext) {
226+
// You may want to remove the previous scene first.
227+
if self.main_scene.is_some() {
228+
ctx.scenes.remove(self.main_scene)
229+
}
230+
231+
// Step 1. Kick off scene loading in a separate thread. This method could
232+
// be located in any place of your code.
233+
ctx.load_scene(
234+
path,
235+
false,
236+
|result, game: &mut MyGame, ctx: &mut PluginContext| {
237+
game.on_scene_loading_result(result, ctx)
238+
},
239+
);
240+
}
241+
242+
fn on_scene_loading_result(
243+
&mut self,
244+
result: SceneLoaderResult,
245+
ctx: &mut PluginContext,
246+
) -> GameResult {
247+
// Step 2.
248+
// Remember the new scene as main.
249+
// This method is called once a scene was fully loaded.
250+
self.main_scene = ctx.scenes.add(result?.payload);
251+
Ok(())
252+
}
253+
}
254+
255+
impl Plugin for MyGame {
256+
fn init(&mut self, scene_path: Option<&str>, mut ctx: PluginContext) -> GameResult {
257+
self.load_scene(scene_path.unwrap_or("path/to/your/scene.rgs"), &mut ctx);
258+
Ok(())
259+
}
260+
}
261+
```
262+
263+
It is quite verbose, but it is only needed if you need to do something special during scene loading.
264+
195265
# Book
196266

197267
[The book](https://fyrox-book.github.io/) was updated for 1.0.0, it now has all screenshots up-to-date,

0 commit comments

Comments
 (0)