static BehaviorAction <T> GetAction <T>(SerializableParameter sourceParameter) { MethodInfo methodInfo = sourceParameter.BehaviorActionValue.method.Method; ParameterInfo[] methodParameters = methodInfo.GetParameters(); var parameterAccessor = sourceParameter.BehaviorActionParameters; Type returnType = typeof(Result); Assert.IsTrue(methodInfo.ReturnType == returnType); //If has no extra parameter than the context, we can just simply create and return the delegate if (methodParameters.Length == 1) { return((BehaviorAction <T>)methodInfo.CreateDelegate(typeof(BehaviorAction <T>))); } var parameters = new Expression[methodParameters.Length]; ParameterExpression contextParameter = Expression.Parameter(typeof(T)); //Assemble constant parameters for (int i = 0; i < parameters.Length - 1; i++) { parameters[i + 1] = Expression.Constant(parameterAccessor[i].GetValue()); } parameters[0] = contextParameter; var callExpression = Expression.Call(null, methodInfo, parameters); //Call expression with constant parameters var lambdaExpression = Expression.Lambda <BehaviorAction <T> >(callExpression, contextParameter); //Precompiled delegate return(lambdaExpression.Compile()); }
private static ParameterDTG ParameterDtg(SerializableParameter parameterModel) { return(new ParameterDTG() { Name = parameterModel.Name, MetadataName = parameterModel.MetadataName, Type = LoadType(parameterModel.Type) }); }
Location <T> LoadNode <T>(Location <T> parent, BehaviorTreeBlueprint <T> blueprint, int index) { TreeNodeData data = allNodes[index]; Type type = BehaviorTreeNodeAttribute.SerializedNameToType.TryGetValue(data.nodeTypeSerializableName); if (type == null) { throw new Exception($"Unexpected serialized node type name {data.nodeTypeSerializableName}."); } if (type.ContainsGenericParameters) { type = type.MakeGenericType(typeof(T)); } INodeType node; //Prepare node and parameters if (data.parameters == null || data.parameters.Length == 0) { node = (INodeType)Activator.CreateInstance(type); } else { object[] parameters = new object[data.parameters.Length]; for (int i = 0; i < data.parameters.Length; i++) { SerializableParameter parameter = data.parameters[i]; if (parameter.Type == ParameterType.behaviorAction) { parameter.LoadBehaviorAction(importData); parameters[i] = GetAction <T>(parameter); } else { parameters[i] = parameter.GetValue(); } } node = (INodeType)Activator.CreateInstance(type, parameters); } //Add to blueprint Location <T> location = blueprint.Add(parent, node, out bool success); if (!success) { throw new Exception($"Invalid node create from graph at GUID: {data.GUID}, position: {data.position}, serialized node name: {data.nodeTypeSerializableName}!"); } return(location); }
protected void GenerateParameterControl(SerializableParameter parameter, string controlName, Action <SingularSerializableParameter> clampAction = null) { var control = CreateParameterControl(parameter, controlName, clampAction); TryGetParameterContainer().Add(control); }