Comments (6)
I am sorry, I've just forgotten to serialize Camera, so nothing appears on the screen :)
Everything is working, thx for your job!
from hecs.
There's no built in mechanism for that, since there's no graceful way to dynamically check for trait implementations on an arbitrary type in Rust. I recommend solving it at the application layer. You could e.g. use a declarative macro to reduce boilerplate if desired.
from hecs.
#[macro_export]
macro_rules! world_serializer {
($ctx:ident, $($comp:ty),*) => {
impl SerializeContext for $ctx {
fn component_count(&self, archetype: &Archetype) -> usize {
archetype.component_types()
.filter(|&t|
$(
t == std::any::TypeId::of::<$comp>() ||
)*
false
)
.count()
}
fn serialize_component_ids<S: serde::ser::SerializeTuple>(
&mut self,
archetype: &Archetype,
mut out: S,
) -> Result<S::Ok, S::Error> {
$(
try_serialize_id::<$comp, _, _>(archetype, stringify!($comp), &mut out)?;
)*
out.end()
}
fn serialize_components<S: serde::ser::SerializeTuple>(
&mut self,
archetype: &Archetype,
mut out: S,
) -> Result<S::Ok, S::Error> {
$(
try_serialize::<$comp, _>(archetype, &mut out)?;
)*
out.end()
}
}
impl DeserializeContext for $ctx {
fn deserialize_component_ids<'de, A: serde::de::SeqAccess<'de>>(
&mut self,
mut seq: A,
) -> Result<ColumnBatchType, A::Error> {
self.components.clear();
let mut batch = ColumnBatchType::new();
while let Some(id) = seq.next_element()? {
match id.as_str() {
$(
stringify!($comp) => {
batch.add::<$comp>();
}
)*
_ => {},
}
self.components.push(id);
}
Ok(batch)
}
fn deserialize_components<'de, A: serde::de::SeqAccess<'de>>(
&mut self,
entity_count: u32,
mut seq: A,
batch: &mut ColumnBatchBuilder,
) -> Result<(), A::Error> {
for component in &self.components {
match component.as_str() {
$(
stringify!($comp) => {
deserialize_column::<$comp, _>(entity_count, &mut seq, batch)?;
}
)*
_ => {},
}
}
Ok(())
}
}
}
}
It says
type must be known at this point
| cannot infer type of the type parameter `T` declared on the associated function `next_element`
while let Some(id) = seq.next_element::<T>()? {
+++++
- What should I specifiy as T?
- Am I able to use string literals/
String
s as ComponentId (e.g. stringify!($comp))?
from hecs.
What should I specifiy as T?
The type of your component IDs. In your case, perhaps &str
or String
?
Am I able to use string literals/Strings as ComponentId (e.g. stringify!($comp))?
Yep, any serializable type should work fine. I like to use enums, but strings make it easier to maintain forwards-compatibility, so they're not a bad choice.
from hecs.
What should I specifiy as T?
The type of your component IDs. In your case, perhaps
&str
orString
?Am I able to use string literals/Strings as ComponentId (e.g. stringify!($comp))?
Yep, any serializable type should work fine. I like to use enums, but strings make it easier to maintain forwards-compatibility, so they're not a bad choice.
I also have thought so, and as saving world was successful, loading it made no error, but the world was just empty
So I considered using String inappropriate
from hecs.
Using String
for component IDs is probably not the reason that happened.
from hecs.
Related Issues (20)
- Panic when borrowing different components of the same type from different entities HOT 5
- Closure component HOT 1
- Cloning World? HOT 1
- Adding a World ID to `Entity` HOT 2
- World::query_one(_mut) is unsound HOT 2
- problem with macro 'Bundle' HOT 1
- Appreciation Thread
- CommandBuffer insert/remove ordering is a footgun
- can't derive Query HOT 1
- Panic in `Archetype::assert_type_info` is reachable HOT 4
- Better understanding or maybe Documentation? HOT 6
- Implementation of `Ord` on `Entity` HOT 2
- Dynamic scene spawning HOT 1
- [Question] State Machines and Animation, or Complex Components HOT 1
- [Question] Is the `index` field in the Archetype struct redundant? HOT 5
- [Question/Clarification] Are bundles suppose to be able to be used as queries? HOT 2
- Ability to create a query view directly from `World` HOT 5
- Component cloning HOT 4
- Determinism through serialization cycle HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hecs.