Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems loading scene #166

Closed
Nehliin opened this issue Aug 13, 2020 · 4 comments
Closed

Problems loading scene #166

Nehliin opened this issue Aug 13, 2020 · 4 comments
Labels
A-Assets Load files from disk to use for things like images, models, and sounds A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior

Comments

@Nehliin
Copy link

Nehliin commented Aug 13, 2020

Hello!

I have been trying out the scene loader and ran into some issues where no entities would be loaded at all. Here is a minimal example:

struct Player;
#[derive(Properties, Default)]
struct Enemy;
#[derive(Properties, Default)]
struct Health(u8);

fn test_world(mut commands: Commands) {
    commands
        .spawn(Camera2dComponents::default())
        .spawn((Player, Health(100)))
        .spawn((Enemy, Health(100)));
}

fn load_world(asset_server: Res<AssetServer>, mut scene_spawner: ResMut<SceneSpawner>) {
    let scene_handle: Handle<Scene> = asset_server.load("assets/scenes/start_scene.scn").unwrap();

    scene_spawner.load(scene_handle);
    println!("loaded");
    asset_server.watch_for_changes().unwrap();
}

fn print_system(mut query: Query<Entity>) {
    for entity in &mut query.iter() {
        println!("  Entity({})", entity.id());
    }
}

fn main() {
    App::build()
        .add_default_plugins()
        .register_component::<Health>()
        .register_component::<Player>()
        .register_component::<Enemy>()
        // This startup system is replaced with test_world the first time so the file can be saved
        .add_startup_system(load_world.system())
        .add_system(print_system.system())
        .run();
}

If I omit spawning the Camera2dComponents everything works as expected and the entities are spawned but if I leave it in I get no errors but no entities are actually loaded to the world. I was under the impression that I shouldn't have to register components that are "built in" to the engine. Is there something else I am missing? Here is the code I use to save the world (I use keyboard input to initiate the save logic but details are omitted):

fn save_scene(world: &mut World, resources: &mut Resources) {
    let type_registry = resources.get::<TypeRegistry>().unwrap();
    let scene = Scene::from_world(&world, &type_registry.component.read().unwrap());

    let mut file = File::create("assets/scenes/start_scene.scn").unwrap();
    file.write_all(
        scene
            .serialize_ron(&type_registry.property.read().unwrap())
            .unwrap()
            .as_bytes(),
    )
    .unwrap();
} 
@karroffel karroffel added A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events labels Aug 13, 2020
@Moxinilian
Copy link
Member

Sorry for the late reply! Would you mind telling me if you are still experiencing this issue with latest Bevy?

@martinfiers
Copy link

martinfiers commented May 1, 2021

Regarding the saving part, this is related to #1442 , just posting it here as reference. See also https://discord.com/channels/691052431525675048/692648082499829760/838080584190132274 about a discussion on direct world access vs the solution proposed in 1442.

@MinerSebas
Copy link
Contributor

Updating this to Bevy 0.5 everything works as expected:

use std::{fs::File, io::Write};

use bevy::prelude::*;
use bevy::reflect::*;

#[derive(Default, Reflect)]
#[reflect(Component)]
struct Player;
#[derive(Default, Reflect)]
#[reflect(Component)]
struct Enemy;
#[derive(Default, Reflect)]
#[reflect(Component)]
struct Health(u8);

fn test_world(mut commands: Commands) {
    commands.spawn_bundle(OrthographicCameraBundle::new_2d());
    commands.spawn_bundle((Player, Health(100)));
    commands.spawn_bundle((Enemy, Health(100)));
}

fn load_world(asset_server: Res<AssetServer>, mut scene_spawner: ResMut<SceneSpawner>) {
    let scene_handle: Handle<DynamicScene> = asset_server.load("scenes/start_scene.scn.ron");

    scene_spawner.spawn_dynamic(scene_handle);
    println!("loaded");
    asset_server.watch_for_changes().unwrap();
}

fn print_system(query: Query<Entity>) {
    for entity in &mut query.iter() {
        println!("  Entity({})", entity.id());
    }
}

fn save_scene(world: &mut World) {
    let type_registry = world.get_resource::<TypeRegistryArc>().unwrap();
    let scene = DynamicScene::from_world(&world, &type_registry);

    let mut file = File::create("assets/scenes/start_scene.scn.ron").unwrap();
    file.write_all(
        scene
            .serialize_ron(&type_registry)
            .unwrap()
            .as_bytes(),
    )
    .unwrap();
}

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .register_type::<Health>()
        .register_type::<Player>()
        .register_type::<Enemy>()
        // This startup system is replaced with test_world the first time so the file can be saved
        .add_startup_system(load_world.system())
        // Uncomment to create save file.
        //.add_startup_system(test_world.system())
        //.add_system(save_scene.exclusive_system().at_end())
        .add_system(print_system.system())
        .run();
}

@alice-i-cecile
Copy link
Member

alice-i-cecile commented May 7, 2021

Closing as resolved <3

BD103 pushed a commit to BD103/bevy that referenced this issue May 2, 2024
Resolve merge conflicts in `lienar-rgba-everywhere`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

6 participants