Rust

Create an entity fully programmatically in Rust might be a bit trickier as if we don't want to rely in using the protobuf templating we must use the EntityBuilder instead.

To create an entity programmatically using Rust you can follow these steps:

Step One

Import the necessary modules and types:

#![allow(unused)]
fn main() {
use alacrity_lib::prelude::*;
}

Step Two

Create a new entity instance though the EntityBuilder helper:

#![allow(unused)]
fn main() {
let mut warrior_001 = EntityBuilder::default()
    .with_id(uuid::Uuid::new_v4())
    .with_name("Warrior Character")
    .with_description("Ad-Hoc instance of a Warrior Human Playable Character")
    .with_attributes(
        [
            {
                "max_hit_point".to_string(),
                Attribute::new("max_hit_point", Value::from(16)).unwrap(),                
            },
            {
                "str".to_string(),
                Attribute::new("str", Value::from(8)).unwrap(),
            },
            {
                "dex".to_string(),
                Attribute::new("dex", Value::from(2)).unwrap(),
            },
            {
                "con".to_string(),
                Attribute::new("con", Value::from(6)).unwrap(),
            },
            {
                "int".to_string(),
                Attribute::new("int", Value::from(1)).unwrap(),
            },
            {

                "wis".to_string(),
                Attribute::new("wis", Value::from(1)).unwrap(),
            },
            {

                "cha".to_string(),
                Attribute::new("cha", Value::from(3)).unwrap(),
            },
        ]
        .iter()
        .cloned()
        .collect::<HashMap<String, Attribute>>(),
    )
    .with_version("0.0.1".to_string())
    .build()
    .unwrap();
}

Step Three

Make any optional modifications:

#![allow(unused)]
fn main() {
if let Err(e) = warrior_001.add_tags(vec![
    Tag::new("humanoid", Value::from("human")),
    Tag::new("playable_character", Value::from(1)),
], true) {
    // handle error
}

if let Err(e) = warrior_001.add_properties(vec![
    Property::new("mental_condition", Value::from("good")),
    Property::new("level", Value::from(1)),
    Property::new("class", Value::from("warrior")),
    Property::new("ftue_completed", Value::from(false)),
    Property::new("current_tracking_quest", Value::from(0)).hide_from_conditions(true),
    Property::new("last_check_point", Value::from("none")).hide_from_conditions(true),
]) {
    // handle error
}
}

Using the Protobuf Templates API

Instantiating an entity through the protobuf templates API might be more convenient for you depending in your use case. This is regularly not recommended but there is nothing wrong in using it if you feel more comfortable doing it.

Protobuf Step One

Add the alacrity protobuf template library so we can use templating:

#![allow(unused)]
fn main() {
use alacrity_pb::tpl;
}

Protobuf Step Two

Instantiate a new entity through the protobuf templating API:

#![allow(unused)]
fn main() {
let warrior_001 = Entity::try_from(&tpl::EntityPrototype {
    id: uuid::Uuid::new_v4().to_string(),
    name: "Warrior Character",
    description: "Ad-Hoc instance of a Warrior Human Playable Character",
    tags: vec![tpl::Tag {
        name: "humanoid".to_string(),
        value: Some(tpl::tag::Value::StringValue("human".to_string())),
    }, tpl::Tag {
        name: "playable_character".to_string(),
        value: Some(tpl::tag::Value::IntValue(1)),
    }],
    attributes: vec![tpl::entity_prototype::Attribute {
        name: "max_hit_point".to_string(),
        description: "Character's Max Hit Points".to_string(),
        value: 16.0,
    }, tpl::entity_prototype::Attribute {
        name: "str".to_string(),
        description: "Character's Strength".to_string(),
        value: 8.0,
    }, tpl::entity_prototype::Attribute {
        name: "dex".to_string(),
        description: "Character's Dexterity".to_string(),
        value: 2.0,
    }, tpl::entity_prototype::Attribute {
        name: "con".to_string(),
        description: "Character's Constitution".to_string(),
        value: 6.0,
    }, tpl::entity_prototype::Attribute {
        name: "int".to_string(),
        description: "Character's Intelligence".to_string(),
        value: 1.0,
    }, tpl::entity_prototype::Attribute {
        name: "wis".to_string(),
        description: "Character's Wisdom".to_string(),
        value: 1.0,
    }, tpl::entity_prototype::Attribute {
        name: "cha".to_string(),
        description: "Character's Charisma".to_string(),
        value: 3.0,
    }],
    properties: vec![tpl::entity_prototype::Property {
        name: "mental_condition".to_string(),
        description: "Character current mental condition".to_string(),
        appears_in_conditionals: true,
        value: Some(tpl::entity_prototype::property::Value::NumericValue(10.0)),
    }, tpl::entity_prototype::Property {
        name: "level".to_string(),
        description: "Character current level".to_string(),
        appears_in_conditionals: true,
        value: Some(tpl::entity_prototype::property::Value::NumericValue(1.0)),
    }, tpl::entity_prototype::Property {
        name: "class".to_string(),
        description: "Character Class".to_string(),
        appears_in_conditionals: true,
        value: Some(tpl::entity_prototype::property::Value::StringValue("warrior".to_string())),
    }, tpl::entity_prototype::Property {
        name: "ftue_completed".to_string(),
        description: "Character has completed the FTUE?".to_string(),
        appears_in_conditionals: false,
        value: Some(tpl::entity_prototype::property::Value::BooleanValue(false)),
    }, tpl::entity_prototype::Property {
        name: "current_tracking_quest".to_string(),
        description: "Character current being tracked or active quest".to_string(),
        appears_in_conditionals: false,
        value: Some(tpl::entity_prototype::property::Value::NumericValue(1.0)),
    }, tpl::entity_prototype::Property {
        name: "last_check_point".to_string(),
        description: "Character last check point where it will return in case of death".to_string(),
        appears_in_conditionals: false,
        value: Some(tpl::entity_prototype::property::Value::StringValue("none".to_string())),
    }],
    ..Default::default()
});

match warrior_001 {
    Ok(warrior_001) => {
        // do something with warrior_001
    }
    Err(e) => {
        // handle error
    }
}
}