pub fn create_game_instance(&mut self) -> usize {

        let mut to_execute : Vec<usize> = vec![];

        let mut startup_name : Option<String> = None;
        let mut locked_tree  : Option<usize> = None;

        let mut scope = Scope::new();

        let behavior = &mut self.game.behavior;

        // Collect name of the startup tree and the variables
        for (_id, node) in &behavior.data.nodes {
            if node.behavior_type == BehaviorNodeType::BehaviorType {
                if let Some(value )= node.values.get(&"startup".to_string()) {
                    startup_name = Some(value.4.clone());
                }
            } else
            if node.behavior_type == BehaviorNodeType::VariableNumber {
                if let Some(value )= node.values.get(&"value".to_string()) {
                    scope.push(node.name.clone(), value.0.clone());
                } else {
                    scope.push(node.name.clone(), 0.0_f64);
                }
            }
        }

        // Second pass parse the trees and find the startup tree
        for (id, node) in &behavior.data.nodes {
            if node.behavior_type == BehaviorNodeType::BehaviorTree {

                for c in &behavior.data.connections {
                    if c.0 == *id {
                        to_execute.push(c.0);
                        if let Some(startup) = startup_name.clone() {
                            if node.name == startup {
                                locked_tree = Some(node.id);
                            }
                        }
                    }
                }
            }
        }

        let index = self.instances.len();

        let mut instance = BehaviorInstance {id: thread_rng().gen_range(1..=u32::MAX) as usize, state: BehaviorInstanceState::Normal, name: behavior.name.clone(), behavior_id: behavior.data.id, tree_ids: to_execute.clone(), position: None, tile: None, target_instance_index: None, locked_tree, party: vec![], node_values: HashMap::new(), state_values: HashMap::new(), number_values: HashMap::new(), sleep_cycles: 0, systems_id: 0, action: None, instance_type: behavior::BehaviorInstanceType::GameLogic};

        // Make sure id is unique
        let mut has_id_already = true;
        while has_id_already {
            has_id_already = false;
            for index in 0..self.instances.len() {
                if self.instances[index].id == instance.id {
                    has_id_already = true;
                }
            }

            if has_id_already {
                instance.id += 1;
            }
        }

        self.instances.push(instance);
        self.scopes.push(scope);

        return index;
    }
/// Create the game instance and return it's instance index
Create the game instance and return it's instance index
Create the game instance and return it ' s instance index
pub fn create_game_instance(&mut self) -> usize {
        let mut to_execute : Vec<usize> = vec![];
        let mut startup_name : Option<String> = None;
        let mut locked_tree  : Option<usize> = None;
        let mut scope = Scope::new();
        let behavior = &mut self.game.behavior;
        // Collect name of the startup tree and the variables
        for (_id, node) in &behavior.data.nodes {
            if node.behavior_type == BehaviorNodeType::BehaviorType {
                if let Some(value )= node.values.get(&"startup".to_string()) {
                    startup_name = Some(value.4.clone());
                }
            } else
            if node.behavior_type == BehaviorNodeType::VariableNumber {
                if let Some(value )= node.values.get(&"value".to_string()) {
                    scope.push(node.name.clone(), value.0.clone());
                } else {
                    scope.push(node.name.clone(), 0.0_f64);
                }
            }
        }
        // Second pass parse the trees and find the startup tree
        for (id, node) in &behavior.data.nodes {
            if node.behavior_type == BehaviorNodeType::BehaviorTree {
                for c in &behavior.data.connections {
                    if c.0 == *id {
                        to_execute.push(c.0);
                        if let Some(startup) = startup_name.clone() {
                            if node.name == startup {
                                locked_tree = Some(node.id);
                            }
                        }
                    }
                }
            }
        }
        let index = self.instances.len();
        let mut instance = BehaviorInstance {id: thread_rng().gen_range(1..=u32::MAX) as usize, state: BehaviorInstanceState::Normal, name: behavior.name.clone(), behavior_id: behavior.data.id, tree_ids: to_execute.clone(), position: None, tile: None, target_instance_index: None, locked_tree, party: vec![], node_values: HashMap::new(), state_values: HashMap::new(), number_values: HashMap::new(), sleep_cycles: 0, systems_id: 0, action: None, instance_type: behavior::BehaviorInstanceType::GameLogic};
        // Make sure id is unique
        let mut has_id_already = true;
        while has_id_already {
            has_id_already = false;
            for index in 0..self.instances.len() {
                if self.instances[index].id == instance.id {
                    has_id_already = true;
                }
            }
            if has_id_already {
                instance.id += 1;
            }
        }
        self.instances.push(instance);
        self.scopes.push(scope);
        return index;
    }
pub fn create_game_instance ( & mut self ) -> usize { let mut to_execute : Vec < usize > = vec ! [ ] ; let mut startup_name : Option < String > = None ; let mut locked_tree : Option < usize > = None ; let mut scope = Scope :: new ( ) ; let behavior = & mut self . game . behavior ; // Collect name of the startup tree and the variables for ( _id , node ) in & behavior . data . nodes { if node . behavior_type == BehaviorNodeType :: BehaviorType { if let Some ( value ) = node . values . get ( & " " . to_string ( ) ) { startup_name = Some ( value . 4 . clone ( ) ) ; } } else if node . behavior_type == BehaviorNodeType :: VariableNumber { if let Some ( value ) = node . values . get ( & " " . to_string ( ) ) { scope . push ( node . name . clone ( ) , value . 0 . clone ( ) ) ; } else { scope . push ( node . name . clone ( ) , 0.0_f64 ) ; } } } // Second pass parse the trees and find the startup tree for ( id , node ) in & behavior . data . nodes { if node . behavior_type == BehaviorNodeType :: BehaviorTree { for c in & behavior . data . connections { if c . 0 == * id { to_execute . push ( c . 0 ) ; if let Some ( startup ) = startup_name . clone ( ) { if node . name == startup { locked_tree = Some ( node . id ) ; } } } } } } let index = self . instances . len ( ) ; let mut instance = BehaviorInstance { id : thread_rng ( ) . gen_range ( 1 ..= u32 :: MAX ) as usize , state : BehaviorInstanceState :: Normal , name : behavior . name . clone ( ) , behavior_id : behavior . data . id , tree_ids : to_execute . clone ( ) , position : None , tile : None , target_instance_index : None , locked_tree , party : vec ! [ ] , node_values : HashMap :: new ( ) , state_values : HashMap :: new ( ) , number_values : HashMap :: new ( ) , sleep_cycles : 0 , systems_id : 0 , action : None , instance_type : behavior :: BehaviorInstanceType :: GameLogic } ; // Make sure id is unique let mut has_id_already = true ; while has_id_already { has_id_already = false ; for index in 0 .. self . instances . len ( ) { if self . instances [ index ] . id == instance . id { has_id_already = true ; } } if has_id_already { instance . id += 1 ; } } self . instances . push ( instance ) ; self . scopes . push ( scope ) ; return index ; }
Create the game instance and return it's instance index
Create the game instance and return it ' s instance index
&mut self
&mut self