Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upSerialization API #18
Comments
This comment has been minimized.
This comment has been minimized.
|
Woah, this is gonna be interesting! |
This comment has been minimized.
This comment has been minimized.
|
Would it be alright for I'm assuming this will mainly be used for things like building entities all together from files similar to the amethyst issue that was referenced amethyst/amethyst#112 since serializing singular components is fairly simple. Along with maybe serializing an entire world at an instance. |
This comment has been minimized.
This comment has been minimized.
|
Yes, behind a feature gate.
|
This comment has been minimized.
This comment has been minimized.
|
So I figured out a way to have it abstracted a bit: pub trait Deserializer<T> {
/// Deserialize data into an entity.
fn deserialize(&mut World, T) -> Result<(), Error>;
/// Called when the world has a component registered.
fn register_component<C>(&mut self) { }
/// Called when the world has a resource registered.
fn register_resource<R>(&mut self) { }
/// Maybe some other methods related to the world's state being changed?
}
pub trait Serializer<T> {
/// Serialize a part of the world and return it.
fn serialize(&mut World) -> Result<T, Error>;
/// Called when the world has a component registered.
fn register_component<C>(&mut self) { }
/// Called when the world has a resource registered.
fn register_resource<R>(&mut self) { }
} However, I've run into a bit of a wall. In the files you need some way to identify the component as itself, which would probably be some sort of built in static id for each ---
Component1:
field1: 5
field2: false
Component2:
field3: [1, 1, 1].json {
"Component1": {
"field1": 5,
"field2": false
},
"Component2": {
"field3": [1, 1, 1]
}
}So I'm thinking of maybe having the |
This comment has been minimized.
This comment has been minimized.
ghost
commented
Mar 9, 2017
•
This seems reasonable to me. If we put this in a separate trait, we could have a |
This comment has been minimized.
This comment has been minimized.
ghost
commented
Mar 10, 2017
|
Maybe something like this? |
This comment has been minimized.
This comment has been minimized.
ghost
commented
Mar 10, 2017
•
|
I think deserializing a world with serde would be a challenge. Because the world needs components to be registered for deserialization to work, you need to create the world before deserializing it, but the deserialize trait needs to produce an owned value out of nothing. A trait such as the one suggested by @Aceeri would be ideal, but I'm not sure how to make this compatible with serde. EDIT: This is wrong. You can use |
This comment has been minimized.
This comment has been minimized.
ghost
commented
Mar 16, 2017
|
For |
This comment has been minimized.
This comment has been minimized.
nchashch
commented
Apr 15, 2017
|
TL;DR: We need a way to serialize and deserialize components, which depend on an outside resource (like For a component, serialized and deserialized forms of which correspond one to one, like: Velocity:
x: 1.0
y: 2.0Serialization or deserialization can be done in one step: serialized component | But there are important kinds of components for which, serialized and deserialized forms don't correspond. For example A serialized version of Renderable:
mesh: "sphere.obj"
ambient: "ambient.dds"
diffuse: "diffuse.dds"
specular: "specular.dds"So additional steps would be required in order to get from serialized serialized Maybe we could store
Since filenames would probably be stored in deserialized version serialization would remain one step: deserialized Renderable | |
This comment has been minimized.
This comment has been minimized.
|
@nchashch I think that could just be done with custom implementations of Serialize/Deserialize for those types of components. You might need a secondary "helper" component though I suppose. Something like: #[derive(Serialize, Deserialize)]
struct RenderableSerialize {
mesh: String, // Could also maybe be Path? Not sure if serde implements that.
ambient: String,
diffuse: String,
specular: String,
}
impl Component for RenderableSerialize { ... }
struct RenderableSerializeSystem {
added: HashSet<Entity>, // possible memory leak-ish stuff if we dont remove entities that die though
}
impl System<()> for RenderableSerializeSystem {
fn run(&mut self, arg: RunArg, _: ()) {
let (entities, renderables_s, renderables) = arg.fetch(|w| {
(w.entities(), w.read::<RenderableSerialize>(), w.write::<Renderable>())
};
for (entity, renderable_s) in (&entities, &renderables_s).join() {
if !self.added.contains(entity) {
let renderable = { ... }; // construct renderable from pathes in asset manager
renderables.insert(entity, renderable);
self.added.insert(entity);
}
}
}
} |
This comment has been minimized.
This comment has been minimized.
|
I guess we can close this now. @kvark |
This comment has been minimized.
This comment has been minimized.
|
Hooray! |
kvark commentedApr 10, 2016
No description provided.