pub fn eval_dynamic_expression_instance_editor(instance_index: usize, id: (BehaviorType, usize, usize, String), data: &mut GameData) -> bool {
    add_target_to_scope(instance_index, data);

    let original = data.scopes[instance_index].clone();

    if let Some(ast) = data.ast.get(&id) {
        let r = data.engine.eval_ast_with_scope::<Dynamic>(&mut  data.scopes[instance_index], ast);
        if r.is_ok() {
            apply_scope_to_target(instance_index, data);

            let mut key_to_change: Option<String> = None;
            let mut new_value : Option<f64> = None;

            if let Some(behavior) = data.behaviors.get_mut(&data.instances[instance_index].behavior_id) {
                for (_index, node) in &behavior.data.nodes {
                    if node.behavior_type == BehaviorNodeType::VariableNumber {

                        let o = original.get_value::<f64>(node.name.as_str());
                        let n = data.scopes[instance_index].get_value::<f64>(node.name.as_str());

                        if n.is_some() && o.is_some() && Some(o) != Some(n) {
                            key_to_change = Some(node.name.clone());
                            new_value = Some(n.unwrap());
                        }
                    }
                }
            }

            if let Some(key) = key_to_change {
                if let Some(value) = new_value {
                    if let Some(behavior) = data.behaviors.get_mut(&id.1) {

                        // Insert the node id of the changed variable to the list
                        // Note: Only need todo when run in editor
                        for (_index, node) in &behavior.data.nodes {
                            if node.name == key && node.behavior_type == BehaviorNodeType::VariableNumber {
                                data.changed_variables.push((instance_index, behavior.data.id, node.id, value));
                                //println!("{:?}", (instance_index, behavior.data.id, node.id));
                            }
                        }
                    }
                }
            }

            return true
        } else {
            println!("{:?}", r);
        }
    } else {
        if let Some(value) = get_node_value((id.1, id.2, &id.3), data, id.0) {
            let script = replace_target_variables(value.4);
            if let Some(ast) = data.engine.compile_with_scope(&mut data.scopes[instance_index], script.as_str()).ok() {
                let r = data.engine.eval_ast_with_scope::<Dynamic>(&mut data.scopes[instance_index], &ast);
                if r.is_ok() {

                    data.ast.insert(id.clone(), ast);
                    apply_scope_to_target(instance_index, data);

                    let mut key_to_change: Option<String> = None;
                    let mut new_value : Option<f64> = None;

                    if let Some(behavior) = data.behaviors.get_mut(&data.instances[instance_index].behavior_id) {
                        for (_index, node) in &behavior.data.nodes {
                            if node.behavior_type == BehaviorNodeType::VariableNumber {

                                let o = original.get_value::<f64>(node.name.as_str());
                                let n = data.scopes[instance_index].get_value::<f64>(node.name.as_str());

                                if n.is_some() && o.is_some() && Some(o) != Some(n) {
                                    key_to_change = Some(node.name.clone());
                                    new_value = Some(n.unwrap());
                                }
                            }
                        }
                    }

                    if let Some(key) = key_to_change {
                        if let Some(value) = new_value {
                            if let Some(behavior) = data.behaviors.get_mut(&id.1) {

                                // Insert the node id of the changed variable to the list
                                // Note: Only need todo when run in editor
                                for (_index, node) in &behavior.data.nodes {
                                    if node.name == key && node.behavior_type == BehaviorNodeType::VariableNumber {
                                        data.changed_variables.push((instance_index, behavior.data.id, node.id, value));
                                        //println!("{:?}", (instance_index, behavior.data.id, node.id));
                                    }
                                }
                            }
                        }
                    }

                    return true
                } else {
                    println!("{:?}", r);
                }
            } else {
                println!("failed to compile {}", script);
            }
        }
    }
    false
}
/// Evaluates a dynamic script in the given instance.
/// We have to send the editor the variables which have been updated for visual display.
Evaluates a dynamic script in the given instance.
We have to send the editor the variables which have been updated for visual display.
Evaluates a dynamic script in the given instance . We have to send the editor the variables which have been updated for visual display .
pub fn eval_dynamic_expression_instance_editor(instance_index: usize, id: (BehaviorType, usize, usize, String), data: &mut GameData) -> bool {
    add_target_to_scope(instance_index, data);
    let original = data.scopes[instance_index].clone();
    if let Some(ast) = data.ast.get(&id) {
        let r = data.engine.eval_ast_with_scope::<Dynamic>(&mut  data.scopes[instance_index], ast);
        if r.is_ok() {
            apply_scope_to_target(instance_index, data);
            let mut key_to_change: Option<String> = None;
            let mut new_value : Option<f64> = None;
            if let Some(behavior) = data.behaviors.get_mut(&data.instances[instance_index].behavior_id) {
                for (_index, node) in &behavior.data.nodes {
                    if node.behavior_type == BehaviorNodeType::VariableNumber {
                        let o = original.get_value::<f64>(node.name.as_str());
                        let n = data.scopes[instance_index].get_value::<f64>(node.name.as_str());
                        if n.is_some() && o.is_some() && Some(o) != Some(n) {
                            key_to_change = Some(node.name.clone());
                            new_value = Some(n.unwrap());
                        }
                    }
                }
            }
            if let Some(key) = key_to_change {
                if let Some(value) = new_value {
                    if let Some(behavior) = data.behaviors.get_mut(&id.1) {
                        // Insert the node id of the changed variable to the list
                        // Note: Only need todo when run in editor
                        for (_index, node) in &behavior.data.nodes {
                            if node.name == key && node.behavior_type == BehaviorNodeType::VariableNumber {
                                data.changed_variables.push((instance_index, behavior.data.id, node.id, value));
                                //println!("{:?}", (instance_index, behavior.data.id, node.id));
                            }
                        }
                    }
                }
            }
            return true
        } else {
            println!("{:?}", r);
        }
    } else {
        if let Some(value) = get_node_value((id.1, id.2, &id.3), data, id.0) {
            let script = replace_target_variables(value.4);
            if let Some(ast) = data.engine.compile_with_scope(&mut data.scopes[instance_index], script.as_str()).ok() {
                let r = data.engine.eval_ast_with_scope::<Dynamic>(&mut data.scopes[instance_index], &ast);
                if r.is_ok() {
                    data.ast.insert(id.clone(), ast);
                    apply_scope_to_target(instance_index, data);
                    let mut key_to_change: Option<String> = None;
                    let mut new_value : Option<f64> = None;
                    if let Some(behavior) = data.behaviors.get_mut(&data.instances[instance_index].behavior_id) {
                        for (_index, node) in &behavior.data.nodes {
                            if node.behavior_type == BehaviorNodeType::VariableNumber {
                                let o = original.get_value::<f64>(node.name.as_str());
                                let n = data.scopes[instance_index].get_value::<f64>(node.name.as_str());
                                if n.is_some() && o.is_some() && Some(o) != Some(n) {
                                    key_to_change = Some(node.name.clone());
                                    new_value = Some(n.unwrap());
                                }
                            }
                        }
                    }
                    if let Some(key) = key_to_change {
                        if let Some(value) = new_value {
                            if let Some(behavior) = data.behaviors.get_mut(&id.1) {
                                // Insert the node id of the changed variable to the list
                                // Note: Only need todo when run in editor
                                for (_index, node) in &behavior.data.nodes {
                                    if node.name == key && node.behavior_type == BehaviorNodeType::VariableNumber {
                                        data.changed_variables.push((instance_index, behavior.data.id, node.id, value));
                                        //println!("{:?}", (instance_index, behavior.data.id, node.id));
                                    }
                                }
                            }
                        }
                    }
                    return true
                } else {
                    println!("{:?}", r);
                }
            } else {
                println!("failed to compile {}", script);
            }
        }
    }
    false
}
pub fn eval_dynamic_expression_instance_editor ( instance_index : usize , id : ( BehaviorType , usize , usize , String ) , data : & mut GameData ) -> bool { add_target_to_scope ( instance_index , data ) ; let original = data . scopes [ instance_index ] . clone ( ) ; if let Some ( ast ) = data . ast . get ( & id ) { let r = data . engine . eval_ast_with_scope :: < Dynamic > ( & mut data . scopes [ instance_index ] , ast ) ; if r . is_ok ( ) { apply_scope_to_target ( instance_index , data ) ; let mut key_to_change : Option < String > = None ; let mut new_value : Option < f64 > = None ; if let Some ( behavior ) = data . behaviors . get_mut ( & data . instances [ instance_index ] . behavior_id ) { for ( _index , node ) in & behavior . data . nodes { if node . behavior_type == BehaviorNodeType :: VariableNumber { let o = original . get_value :: < f64 > ( node . name . as_str ( ) ) ; let n = data . scopes [ instance_index ] . get_value :: < f64 > ( node . name . as_str ( ) ) ; if n . is_some ( ) && o . is_some ( ) && Some ( o ) != Some ( n ) { key_to_change = Some ( node . name . clone ( ) ) ; new_value = Some ( n . unwrap ( ) ) ; } } } } if let Some ( key ) = key_to_change { if let Some ( value ) = new_value { if let Some ( behavior ) = data . behaviors . get_mut ( & id . 1 ) { // Insert the node id of the changed variable to the list // Note: Only need todo when run in editor for ( _index , node ) in & behavior . data . nodes { if node . name == key && node . behavior_type == BehaviorNodeType :: VariableNumber { data . changed_variables . push ( ( instance_index , behavior . data . id , node . id , value ) ) ; //println!("{:?}", (instance_index, behavior.data.id, node.id)); } } } } } return true } else { println ! ( " " r ) ; } } else { if let Some ( value ) = get_node_value ( ( id . 1 , id . 2 , & id . 3 ) , data , id . 0 ) { let script = replace_target_variables ( value . 4 ) ; if let Some ( ast ) = data . engine . compile_with_scope ( & mut data . scopes [ instance_index ] , script . as_str ( ) ) . ok ( ) { let r = data . engine . eval_ast_with_scope :: < Dynamic > ( & mut data . scopes [ instance_index ] , & ast ) ; if r . is_ok ( ) { data . ast . insert ( id . clone ( ) , ast ) ; apply_scope_to_target ( instance_index , data ) ; let mut key_to_change : Option < String > = None ; let mut new_value : Option < f64 > = None ; if let Some ( behavior ) = data . behaviors . get_mut ( & data . instances [ instance_index ] . behavior_id ) { for ( _index , node ) in & behavior . data . nodes { if node . behavior_type == BehaviorNodeType :: VariableNumber { let o = original . get_value :: < f64 > ( node . name . as_str ( ) ) ; let n = data . scopes [ instance_index ] . get_value :: < f64 > ( node . name . as_str ( ) ) ; if n . is_some ( ) && o . is_some ( ) && Some ( o ) != Some ( n ) { key_to_change = Some ( node . name . clone ( ) ) ; new_value = Some ( n . unwrap ( ) ) ; } } } } if let Some ( key ) = key_to_change { if let Some ( value ) = new_value { if let Some ( behavior ) = data . behaviors . get_mut ( & id . 1 ) { // Insert the node id of the changed variable to the list // Note: Only need todo when run in editor for ( _index , node ) in & behavior . data . nodes { if node . name == key && node . behavior_type == BehaviorNodeType :: VariableNumber { data . changed_variables . push ( ( instance_index , behavior . data . id , node . id , value ) ) ; //println!("{:?}", (instance_index, behavior.data.id, node.id)); } } } } } return true } else { println ! ( " " r ) ; } } else { println ! ( " " script ) ; } } } false }
Evaluates a dynamic script in the given instance.
Evaluates a dynamic script in the given instance .
instance_index
usize
id
(BehaviorType, usize, usize, String)
data
&mut GameData
instance_index
usize
id
(BehaviorType, usize, usize, String)
data
&mut GameData