0
\$\begingroup\$

I'm building a game to learn rust, I've built the data loader, and part of the ECS, the part I'm having trouble with is transforming the templates of entities into instances actual entities, I.E transforms the template of an orc into 2,3 or more orcs.

I have a list of templates, each template has a list of what components that entity will have, and each component has the name of the component and what value that component will/may contain. Each template can also have a list of Feature packs that are just groupings of pre-defined lists of components.

---
orc:
  feature_packs:
    - physical
    - basic_identifiers_mob
  components:
    - component: char
      initial_value: T
goblin:
  feature_packs:
    - physical
    - basic_identifiers_mob
  components:
    - component: char
      initial_value: t

Before I continue I'm going to specify things here, I'm doing this because so far whenever I've asked about this, people just don't seem to read the question and then respond with unhelpful answers; I'm grateful that people were/are trying to help (really, I am), but not reading the question properly and then answering is not helpful and wastes both of our times.

  • I'm not trying to load gameplay data into the game, I'm loading templates
  • I'm using those templates to then generate the entities that will be used during gameplay
  • I can already load the data I want into the game in the following structure:
pub enum InitialValue {
    Char(char),
    String(String),
    Int(i32),
    Float(f32),
    Bool(bool),
    Range(Range<i32>),
    Point((i32,i32))
}


impl InitialValue {

    pub fn unwrap_char(&self) -> &char {
        match &self {
            InitialValue::Char(val) => val,
            _ => panic!("Stored value does not match unwrap type")
        }
    }

    pub fn unwrap_string(&self) -> &String {
        match &self {
            InitialValue::String(val) => val,
            _ => panic!("Stored value does not match unwrap type")
        }
    }

    pub fn unwrap_int(&self) -> &i32 {
        match &self {
            InitialValue::Int(val) => val,
            _ => panic!("Stored value does not match unwrap type")
        }
    }

    pub fn unwrap_float(&self) -> &f32 {
        match &self {
            InitialValue::Float(val) => val,
            _ => panic!("Stored value does not match unwrap type")
        }
    }

    pub fn unwrap_bool(&self) -> &bool {
        match &self {
            InitialValue::Bool(val) => val,
            _ => panic!("Stored value does not match unwrap type")
        }
    }

    pub fn unwrap_range(&self) -> &Range<i32> {
        match &self {
            InitialValue::Range(val) => val,
            _ => panic!("Stored value does not match unwrap type")
        }
    }

    pub fn unwrap_point(&self) -> &(i32, i32) {
        match &self {
            InitialValue::Point(val) => val,
            _ => panic!("Stored value does not match unwrap type")
        }
    }
}

#[derive(Debug, Deserialize)]
pub struct Component {
    #[serde(rename="component")]
    name: String,
    #[serde(default)]
    initial_value: Option<InitialValue>,
}

#[derive(Debug, Deserialize)]
pub struct Template {
    pub feature_packs: Vec<String>,
    pub components: Vec<Component>,
}

I've been trying to figure this out for months (on and off) and I've gotten nowhere, so,

  • How do I transform the templates into instances of entities?

  • Specifcally, How do I for a given Component.name find the component and then initialize it? OR is my aproach wrong and there's a better way.

  • And if I am doing it wrong, How do other games load data in and then use it to generate in game entities?
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.