///---------------------------------------------------------------------------------------------- ///Change variable type (creates new instance and keeps name and ID) public static Variable ChangeVariableType(this IBlackboard blackboard, Variable target, Type newType) { var variableType = typeof(Variable <>).RTMakeGenericType(new Type[] { newType }); var newVariable = (Variable)Activator.CreateInstance(variableType, new object[] { target.name, target.ID }); blackboard.variables[target.name] = newVariable; blackboard.TryInvokeOnVariableAdded(newVariable); return(newVariable); }
///Adds a new Variable in the blackboard defining name and type instead of value public static Variable AddVariable(this IBlackboard blackboard, string varName, Type type) { if (blackboard.variables.TryGetValue(varName, out Variable result)) { if (result.CanConvertTo(type)) { Logger.Log(string.Format("Variable with name '{0}' already exists in blackboard '{1}'. Returning existing instead of new.", varName, blackboard), LogTag.BLACKBOARD, blackboard); return(result); } else { Logger.LogError(string.Format("Variable with name '{0}' already exists in blackboard '{1}', but is of a different type! Returning null instead of new.", varName, blackboard), LogTag.BLACKBOARD, blackboard); return(null); } } var variableType = typeof(Variable <>).RTMakeGenericType(new Type[] { type }); var newVariable = (Variable)Activator.CreateInstance(variableType); newVariable.name = varName; blackboard.variables[varName] = newVariable; blackboard.TryInvokeOnVariableAdded(newVariable); return(newVariable); }