Coder Social home page Coder Social logo

Comments (6)

konceptosociala avatar konceptosociala commented on August 16, 2024 1

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.

Ralith avatar Ralith commented on August 16, 2024

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.

konceptosociala avatar konceptosociala commented on August 16, 2024
#[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>()? {
                                         +++++
  1. What should I specifiy as T?
  2. Am I able to use string literals/Strings as ComponentId (e.g. stringify!($comp))?

from hecs.

Ralith avatar Ralith commented on August 16, 2024

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.

konceptosociala avatar konceptosociala commented on August 16, 2024

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.

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.

Ralith avatar Ralith commented on August 16, 2024

Using String for component IDs is probably not the reason that happened.

from hecs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.