FyroxEngine / Fyrox

3D and 2D game engine written in Rust
https://fyrox.rs
MIT License
7.69k stars 348 forks source link

How to get &mut Scene? #540

Closed Wandalen closed 12 months ago

Wandalen commented 1 year ago

Hi! Most snippets from tutorial expect &mut Scene. But for it's not obvious how to get one?

fn create_camera(scene: &mut Scene) -> Handle<Node> {
    CameraBuilder::new(
        // Here we passing a base builder. Note that, since we can build Base node separately
        // we can pass any custom values to it while building.
        BaseBuilder::new().with_local_transform(
            TransformBuilder::new()
                .with_local_position(Vector3::new(2.0, 0.0, 3.0))
                .build(),
        ),
    ) 
    // Here we just setting desired Camera properties.
    .with_fov(60.0f32.to_radians())
    .build(&mut scene.graph)
}

There is Handle<Scene> in constructor of Game, but it's not obvious how to get &mut Scene out of Handle< Scene >. I'm confused. Please clarify how to draw a sprite.

Wandalen commented 1 year ago

Same question regarding renderer?

fn set_quality_settings(renderer: &mut Renderer) {
    let mut settings = QualitySettings::high();

    // Disable something.
    settings.use_ssao = false;
    settings.fxaa = false;

    // Apply.
    Log::verify(renderer.set_quality_settings(&settings))
}
Wandalen commented 1 year ago

I've found zero examples which use any kind of builder, but UI. Frustrating :(

mrDIMAS commented 1 year ago

Hi! You need to borrow a scene from the container: let scene = &mut context.scenes[self.scene]. It can be done in pretty much any methods of your plugin. Keep in mind, that game template uses async scene loader and self.scene will be set to a correct value on after the scene is fully loaded. So ideally, you should do a checked borrow if let Some(scene) = context.scenes.try_get_mut(self.scene) { ... }. Also, if you need to catch the moment when you scene is fully loaded, then you could place your code in the place where the scene is fetched from the async loader (here for example - https://github.com/FyroxEngine/Fyrox-demo-projects/blob/main/sound/game/src/lib.rs#L137).

About the renderer: you can access it from graphics context. For example: if let GraphicsContext::Initialized(ref mut graphics_context) = plugin.graphics_context { ... }. Notice the GraphicsContext::Initialized - since creation of the graphics context is delayed to Event::Resumed, at the start of your app, there's no GraphicsContext, it will be created shortly after. If you need to catch the moment when it is fully initialized, use Plugin::on_graphics_context_initialized method.

These quirks are here, because the engine supports WebAssembly and Android. Both of these platforms enforces their limitations and we have to live with them.

mrDIMAS commented 1 year ago

I'll add this information to the book.

mrDIMAS commented 12 months ago

Added: 1) Fetching scenes - https://fyrox-book.github.io/fyrox/scene/scene.html#where-all-my-scenes-located 2) Fetching renderer - https://fyrox-book.github.io/fyrox/rendering/settings.html#how-to-apply I'm closing the issue.