Forums › 💬 FlowCanvas › 🤩 Custom Nodes and Macros › Call Macro Function
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
using System.Linq; using UnityEngine; using ParadoxNotion.Design; using FlowCanvas.Macros; using NodeCanvas.Framework; namespace FlowCanvas.Nodes { [Name("Call Macro Function")] [Description("Calls a custom function that exists inside a nested Macro")] [Category("Functions/Custom")] [HasRefreshButton] public class MacroFunctionCall : FlowControlNode { [SerializeField] private Macro _targetMacro; [SerializeField] private string _functionName; private ValueInput[] portArgs; private object[] objectArgs; private object returnValue; private FlowOutput fOut; private System.WeakReference<CustomFunctionEvent> _sourceFunctionRef; private CustomFunctionEvent sourceFunction { get { CustomFunctionEvent reference = null; if (_sourceFunctionRef != null && _sourceFunctionRef.TryGetTarget(out reference)) { return reference; } if (_targetMacro != null && !string.IsNullOrEmpty(_functionName)) { var macroInstance = GetMacroInstance(); if (macroInstance != null) { reference = macroInstance.GetAllNodesOfType<CustomFunctionEvent>() .FirstOrDefault(f => f.identifier == _functionName); if (reference != null) { _sourceFunctionRef = new System.WeakReference<CustomFunctionEvent>(reference); } } } return reference; } } public Macro targetMacro { get { return _targetMacro; } set { if (_targetMacro != value) { _targetMacro = value; _sourceFunctionRef = null; GatherPorts(); } } } public string functionName { get { return _functionName; } set { if (_functionName != value) { _functionName = value; _sourceFunctionRef = null; GatherPorts(); } } } public override string name { get { if (_targetMacro != null && !string.IsNullOrEmpty(_functionName)) { return string.Format("Call {0}.{1}()", _targetMacro.name, _functionName); } return "Call Macro Function"; } } public override string description { get { return sourceFunction != null && !string.IsNullOrEmpty(sourceFunction.comments) ? sourceFunction.comments : base.description; } } private Macro GetMacroInstance() { if (_targetMacro == null) return null; if (graph != null) { var macroWrappers = flowGraph.GetAllNodesOfType<MacroNodeWrapper>().ToList(); foreach (var wrapper in macroWrappers) { if (wrapper.macro != null && wrapper.macro.name == _targetMacro.name) { return wrapper.macro; } } } return _targetMacro; } protected override void RegisterPorts() { AddFlowInput(" ", Invoke); if (sourceFunction != null) { var parameters = sourceFunction.parameters; portArgs = new ValueInput[parameters.Count]; for (var i = 0; i < parameters.Count; i++) { var index = i; var parameter = parameters[i]; portArgs[i] = AddValueInput(parameter.name, parameter.type, parameter.ID); } if (sourceFunction.returns.type != null) { AddValueOutput(sourceFunction.returns.name, sourceFunction.returns.ID, sourceFunction.returns.type, () => { return returnValue; }); } fOut = AddFlowOutput(" "); } } void Invoke(Flow f) { var macro = GetMacroInstance(); if (macro == null) { UnityEngine.Debug.LogWarning($"[MacroFunctionCall] Macro instance is null. Target: {_targetMacro?.name}"); return; } var func = macro.GetAllNodesOfType<CustomFunctionEvent>() .FirstOrDefault(fn => fn.identifier == _functionName); if (func == null) { UnityEngine.Debug.LogWarning($"[MacroFunctionCall] Function '{_functionName}' not found in running macro '{macro.name}'."); return; } if (objectArgs == null || objectArgs.Length != portArgs.Length) { objectArgs = new object[portArgs.Length]; } for (var i = 0; i < portArgs.Length; i++) { objectArgs[i] = portArgs[i].value; } func.InvokeAsync(f, (fx) => { this.returnValue = func.GetReturnValue(); if (fOut != null) { fOut.Call(fx); } }, objectArgs); } #if UNITY_EDITOR protected override void OnNodeExternalGUI() { if (sourceFunction != null && isSelected) { UnityEditor.Handles.color = Color.grey; UnityEditor.Handles.DrawAAPolyLine(rect.center, sourceFunction.rect.center); UnityEditor.Handles.color = Color.white; } } protected override void OnNodeInspectorGUI() { UnityEditor.EditorGUI.BeginChangeCheck(); _targetMacro = (Macro)UnityEditor.EditorGUILayout.ObjectField("Target Macro", _targetMacro, typeof(Macro), false); if (_targetMacro != null) { var macroInstance = GetMacroInstance(); if (macroInstance != null) { var functions = macroInstance.GetAllNodesOfType<CustomFunctionEvent>().ToList(); if (functions != null && functions.Count > 0) { var functionNames = functions.Select(f => f.identifier).ToArray(); var currentIndex = System.Array.IndexOf(functionNames, _functionName); if (currentIndex < 0) currentIndex = 0; var newIndex = UnityEditor.EditorGUILayout.Popup("Function", currentIndex, functionNames); if (newIndex >= 0 && newIndex < functionNames.Length) { _functionName = functionNames[newIndex]; } } else { UnityEditor.EditorGUILayout.HelpBox("No custom functions found in the selected macro.", UnityEditor.MessageType.Warning); } } } if (UnityEditor.EditorGUI.EndChangeCheck()) { _sourceFunctionRef = null; GatherPorts(); } base.OnNodeInspectorGUI(); } #endif } } |
Call Macro Function
Functions>custom>Call Macro Function
This node will allow you to call any custom function inside a nested macro and display input vars and output value. Will also work for a macro nested in a macro but must always be called from the root graph of the nested macro.
This allows the user to create a library of common functional use and then use those functions in a root graph. Very clean root graph.
Provides a dropdown menu for all functions in the macro graph. No misspelling complications.
Hey! Nice, thanks for sharing 🙂
