niklasei / bevy_common_assets Goto Github PK
View Code? Open in Web Editor NEWCollection of generic asset loaders for common file formats
License: Apache License 2.0
Collection of generic asset loaders for common file formats
License: Apache License 2.0
I could not get it to work with bevy_asset_loader
Is there some example code I can look at to be sure I'm using it correctly?
error:
thread 'main' panicked at 'Requested resource bevy_asset::asset_server::AssetServer does not exist in the `World`.
Did you forget to add it using `app.insert_resource` / `app.init_resource`?
Resources are also implicitly added via `app.add_event`,
and can be added by plugins.', /home/pinkponk/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_asset-0.8.1/src/assets.rs:324:43
Usage:
...
.with_collection::<WorldAssets>()
...
.add_plugin(JsonAssetPlugin::<WorldConfigJson>::new(&["json.world"]))
#[derive(serde::Deserialize, bevy::reflect::TypeUuid)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b38022c46"]
pub struct WorldConfigJson {
plants: Vec<String>,
spawns: Vec<SpawnsJson>,
}
#[derive(serde::Deserialize)]
pub struct SpawnsJson {
species: String,
nr_creatures: u32,
radius: f32,
age_factor: f32,
world_pos_factor: [f32; 2],
}
pub fn configure_world(
mut commands: Commands,
world_assets: Res<WorldAssets>,
mut world_config_jsons: ResMut<Assets<WorldConfigJson>>,
) {
...}
file:
{
"plants": [
"mushroom",
"grass",
"tree",
"bush",
"carrot"
],
"spawns": [
{
"species": "fox",
"nr_creatures": 3,
"radius": 10.0,
"age_factor": 0.5,
"world_pos_factor": [
0.1,
0.1
]
}
]
}
I'm trying to optimize my cargo build times for my project, and a major slow down I've noticed for recompiles is bevy_sprite. I'm making a 3d project, and I'd like to be able to remove bevy_sprite, since I don't need it, but this crate makes it a hard dependency.
A '2d' feature flag would let people who use this crate and bevy_asset_loader to remove bevy_sprite, and speed up re-compiles for 3d projects.
The code examples already use level.json
instead of json.level
. Do the same in the readme.
I have structs SpritesData
and SoundsData
, and I want to parse them from sprites.data.ron
and sounds.data.ron
respectively.
A new example would be nice.
Bevy now has AssetSaver
. This allows a cross-platform way of saving assets by:
AssetWriter
from an AssetSource
(from AssetServer
)AssetWriter::write
to get an AsyncWrite
object.AssetSaver::save
with that AsyncWrite
object.In the future this will likely be streamlined but for now we should support AssetSaver
to support this "canonical" workflow.
bevy 1.12.1
Currently figuring out how to use this crate, i added the plugin like so:
.add_plugins((
DefaultPlugins,
YamlAssetPlugin::<World>::new(&["world.yml"])
))
#[derive(Deserialize, Asset, TypePath, Debug)]
struct World {
name: String,
width: u8,
height: u8,
}
fn load_world(
server: Res<AssetServer>,
mut images: ResMut<Assets<Image>>,
mut worlds: ResMut<Assets<World>>,
mut commands: Commands
) {
let handle: Handle<World> = server.load("world.yaml");
if let Some(x) = worlds.get_mut(handle.id()) {
println!("{:?}", x);
} else {
println!("Did not find!")
}
}
This comes up with "did not find!" and the error found in the title: ERROR bevy_asset::server: no 'AssetLoader' found for the following extension: yaml
This error also occurs when changing everything to 'yml'.
The yaml file is very simple and as far as i know, this should work?
name: Cloob
width: 64
height: 32
What is going wrong?
Thanks in advance!
Edit: The example contains wrong code, worlds.get_mut(handle.id())
should be in a Update
script. It still gives the error regardless.
It would be great if there was added support for CSV files, the csv
crate already has support for serde!
Could this repository include a common asset type and loader for loading generic serde_json::Value
, toml_edit::Document
, etc. types from files with the generic format extension like .json
/.toml
rather than a specific extension?
I built something closely related to this for loading plugin settings from specific configuration files: https://github.com/devnev/bevy_settings_loader
But in my plugin, the settings are resource singletons and not identified by file extensions but by the full path to the file. So I add a generic .json
/ .toml
loader and then deserialise from the loaded value into the destination type for the asset matching the settings path.
For that I've added generic JsonAsset
and TomlAsset
types that wrap serde_json::Value
and toml_edit::Document
loaded from arbitrary files with the corresponding extension. However, my repository/package/plugin is too specific for something that generic, whereas this repository seems more suitable and has most of the code for it already.
The main missing pieces is a wrapper implementing Asset
for serde_json::Value
etc, which would allow other plugins to avoid duplicate registration of loaders for the corresponding extension.
This would allow for all xml file types (custom, html, etc.) to be used as assets.
I would be happy to code it up myself with a little guidance if needed!
I'm trying to app.register_asset_processor::<LoadTransformAndSave<RonAssetLoader<MyAsset>, _, _>>(LoadTransformAndSave::new(..., ...)
, but I cannot, bc RonAssetLoader
is private
So I'm trying to replicate something similar to this
struct Projectile {
sprite: Handle<Image>
}
Where an Asset called Projectile has a reference to another asset inside.
Hello,
I try to load this JSON file:
[
{
"name": "Foyer",
"x": 0,
"y": 0,
"width": 3,
"height": 3,
"color": "lightgray",
"description": "Ein großes, einladendes Foyer mit Marmorboden, hohen Decken und einem majestätischen Kronleuchter. An den Wänden hängen alte Gemälde der Familie Blackwood.",
"image": "foyer.jpg"
},
{
"name": "Bibliothek",
"x": 3,
"y": 0,
"width": 3,
"height": 3,
"color": "lightblue",
"description": "Eine ruhige Bibliothek mit hohen Bücherregalen, vollgestopft mit alten Büchern. In der Mitte steht ein massiver Eichentisch, auf dem noch geöffnete Bücher und ein Glas Whiskey stehen.",
"image": "bibliothek.jpg"
}
]
Part of my program:
#[derive(Debug, serde::Deserialize, Asset, TypePath)]
struct Room {
name: String,
x: i32,
y: i32,
width: u32,
height: u32,
color: String,
description: String,
image: String,
}
#[derive(serde::Deserialize, Asset, TypePath)]
struct Rooms {
rooms: Vec<Room>,
}
#[derive(Resource)]
struct RoomsHandle(Handle<Rooms>);
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
JsonAssetPlugin::<Rooms>::new(&["rooms.json"]),
))
.insert_resource(Msaa::Off)
.init_state::<AppState>()
.add_systems(Startup, setup)
.add_systems(Update, spawn_plane.run_if(in_state(AppState::Loading)))
.run();
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>
) {
let rooms = RoomsHandle(asset_server.load("cases/Mord im Herrenhaus/data/rooms.json"));
commands.insert_resource(rooms);
...
}
fn spawn_plane(
mut commands: Commands,
room: Res<RoomsHandle>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut rooms: ResMut<Assets<Rooms>>,
mut state: ResMut<NextState<AppState>>,
) {
if let Some(room) = rooms.remove(room.0.id()) {
for r in room.rooms {
println!("{:?}",r);
}
state.set(AppState::Running);
}
}
There I get the message " Failed to load asset 'cases/Mord im Herrenhaus/data/rooms.json' with asset loader 'bevy_common_assets::json::JsonAssetLoader<rust_bevy_murder_mystery_game::Rooms>': Could not parse the JSON: invalid type: map, expected a sequence at line 2 column 4
"
Whats wrong?
Best regards Gerhard
Could you please add an example of how to read a struct in addition to the vector?
Use https://doc.rust-lang.org/beta/unstable-book/language-features/doc-cfg.html to have notes on docs.rs for required crate features.
First and foremost, thanks for having a changelog at all!
I wanted to suggest to look at Keep A Changelog, because I really like that spec for Changelogs and I think it might further improve this projects log.
If desired, I could try creating the PR for that too :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.