summaryrefslogtreecommitdiff
path: root/Client/Assets/Behavior Designer/Runtime/Actions/Reflection/InvokeMethod.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Client/Assets/Behavior Designer/Runtime/Actions/Reflection/InvokeMethod.cs')
-rw-r--r--Client/Assets/Behavior Designer/Runtime/Actions/Reflection/InvokeMethod.cs86
1 files changed, 86 insertions, 0 deletions
diff --git a/Client/Assets/Behavior Designer/Runtime/Actions/Reflection/InvokeMethod.cs b/Client/Assets/Behavior Designer/Runtime/Actions/Reflection/InvokeMethod.cs
new file mode 100644
index 00000000..5780c350
--- /dev/null
+++ b/Client/Assets/Behavior Designer/Runtime/Actions/Reflection/InvokeMethod.cs
@@ -0,0 +1,86 @@
+using UnityEngine;
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+
+namespace BehaviorDesigner.Runtime.Tasks
+{
+ [TaskDescription("Invokes the specified method with the specified parameters. Can optionally store the return value. Returns success if the method was invoked.")]
+ [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=145")]
+ [TaskCategory("Reflection")]
+ [TaskIcon("{SkinColor}ReflectionIcon.png")]
+ public class InvokeMethod : Action
+ {
+ [Tooltip("The GameObject to invoke the method on")]
+ public SharedGameObject targetGameObject;
+ [Tooltip("The component to invoke the method on")]
+ public SharedString componentName;
+ [Tooltip("The name of the method")]
+ public SharedString methodName;
+ [Tooltip("The first parameter of the method")]
+ public SharedVariable parameter1;
+ [Tooltip("The second parameter of the method")]
+ public SharedVariable parameter2;
+ [Tooltip("The third parameter of the method")]
+ public SharedVariable parameter3;
+ [Tooltip("The fourth parameter of the method")]
+ public SharedVariable parameter4;
+ [Tooltip("Store the result of the invoke call")]
+ public SharedVariable storeResult;
+
+ public override TaskStatus OnUpdate()
+ {
+ var type = TaskUtility.GetTypeWithinAssembly(componentName.Value);
+ if (type == null) {
+ //Debug.LogWarning("Unable to invoke - type is null");
+ return TaskStatus.Failure;
+ }
+
+ var component = GetDefaultGameObject(targetGameObject.Value).GetComponent(type);
+ if (component == null) {
+ //Debug.LogWarning("Unable to invoke method with component " + componentName.Value);
+ return TaskStatus.Failure;
+ }
+
+ var parameterList = new List<object>();
+ var parameterTypeList = new List<Type>();
+ SharedVariable sharedVariable = null;
+ for (int i = 0; i < 4; ++i) {
+ var parameterField = GetType().GetField("parameter" + (i + 1));
+ if ((sharedVariable = parameterField.GetValue(this) as SharedVariable) != null) {
+ parameterList.Add(sharedVariable.GetValue());
+ parameterTypeList.Add(sharedVariable.GetType().GetProperty("Value").PropertyType);
+ } else {
+ break;
+ }
+ }
+ // If you are receiving a compiler error on the Windows Store platform see this topic:
+ // http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=46
+ var methodInfo = component.GetType().GetMethod(methodName.Value, parameterTypeList.ToArray());
+
+ if (methodInfo == null) {
+ //Debug.LogWarning("Unable to invoke method " + methodName.Value + " on component " + componentName.Value);
+ return TaskStatus.Failure;
+ }
+
+ var result = methodInfo.Invoke(component, parameterList.ToArray());
+ if (storeResult != null) {
+ storeResult.SetValue(result);
+ }
+
+ return TaskStatus.Success;
+ }
+
+ public override void OnReset()
+ {
+ targetGameObject = null;
+ componentName = null;
+ methodName = null;
+ parameter1 = null;
+ parameter2 = null;
+ parameter3 = null;
+ parameter4 = null;
+ storeResult = null;
+ }
+ }
+} \ No newline at end of file