Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating Input Ports in NodeView and Getting Data From Those Ports #197

Closed
dannymate opened this issue Jan 28, 2022 · 1 comment
Closed

Comments

@dannymate
Copy link

dannymate commented Jan 28, 2022

So my current situation is that I've generated a few extra input ports from a NodeView. The issue is the AddPort method in NodeView doesn't actually add any ports. Only after copying the method from source and removing [inputPortViews.Add(p);](https://github.com/alelievr/NodeGraphProcessor/blob/bc71d48ba9dfc7e7062779d6d12b9ef269e521b1/Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseNodeView.cs#L362) did the ports show up. No errors are thrown.
Before Line Removed:
image
After Line Removed:
image

I tried using the InsertPort method as well but it was causing issues with the OUTPUT ports creating an error when trying to draw an edge.

DEBUG
ArgumentNullException: Value cannot be null.
Parameter name: key
System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry (TKey key) (at <2c464dcd3e6d4f6784d86bd1010f8293>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].TryGetValue (TKey key, TValue& value) (at <2c464dcd3e6d4f6784d86bd1010f8293>:0)
GraphProcessor.BaseEdgeDragHelper.HandleMouseDown (UnityEngine.UIElements.MouseDownEvent evt) (at Library/PackageCache/com.alelievr.node-graph-processor@1.3.0/Editor/Utils/BaseEdgeDragHelper.cs:145)
GraphProcessor.BaseEdgeConnector.OnMouseDown (UnityEngine.UIElements.MouseDownEvent e) (at Library/PackageCache/com.alelievr.node-graph-processor@1.3.0/Editor/Utils/BaseEdgeConnector.cs:77)
UnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/EventCallback.cs:79)
UnityEngine.UIElements.EventCallbackRegistry.InvokeCallbacks (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/EventCallbackRegistry.cs:353)
UnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/EventHandler.cs:186)
UnityEngine.UIElements.EventDispatchUtilities.PropagateEvent (UnityEngine.UIElements.EventBase evt) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/IEventDispatchingStrategy.cs:113)
UnityEngine.UIElements.MouseEventDispatchingStrategy.SendEventToRegularTarget (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/MouseEventDispatchingStrategy.cs:35)
UnityEngine.UIElements.MouseEventDispatchingStrategy.SendEventToTarget (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/MouseEventDispatchingStrategy.cs:26)
UnityEngine.UIElements.MouseEventDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel iPanel) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Events/MouseEventDispatchingStrategy.cs:19)
UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/EventDispatcher.cs:381)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/EventDispatcher.cs:344)
UnityEngine.UIElements.EventDispatcher.ProcessEventQueue () (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/EventDispatcher.cs:306)
UnityEngine.UIElements.EventDispatcher.OpenGate () (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/EventDispatcher.cs:270)
UnityEngine.UIElements.EventDispatcherGate.Dispose () (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/EventDispatcher.cs:75)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/EventDispatcher.cs:372)
UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/EventDispatcher.cs:222)
UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/Panel.cs:417)
UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/UIElementsUtility.cs:468)
UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Boolean& eventHandled) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/UIElementsUtility.cs:211)
UnityEngine.UIElements.UIEventRegistration.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/UIElementsUtility.cs:74)
UnityEngine.UIElements.UIEventRegistration+<>c.<.cctor>b__1_2 (System.Int32 i, System.IntPtr ptr) (at /home/bokken/buildslave/unity/build/ModuleOverrides/com.unity.ui/Core/UIElementsUtility.cs:28)
UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Boolean& result) (at /home/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)

This is the code I'm using to generate the ports:

Port Generation Code
public override void Enable()
    {
        var node = nodeTarget as BoolActionNode;
        DrawDefaultInspector();
        // Create your fields using node's variables and add them to the controlsContainer

        // controlsContainer.Add(new Label("Hello World !"));
        List<FieldPortInfo> inputFields = new List<FieldPortInfo>();
        foreach (var field in node.GetAction().GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic))
        {
            foreach (var attribute in field.GetCustomAttributes(typeof(InputAttribute), true))
            {
                if (attribute.GetType() != typeof(InputAttribute)) continue;

                inputFields.Add(new FieldPortInfo(field, attribute as InputAttribute));
                break;
            }
        }
        inputFields.Reverse();
        foreach (var input in inputFields)
        {
            // Debug.Log(input.fieldInfo.Name + " " + input.fieldInfo.FieldType);
            PortData portData = new PortData
            {
                displayName = input.inputAttribute.name,
                identifier = input.fieldInfo.Name,
                // sizeInPixel = 1000,
                vertical = false,
                acceptMultipleEdges = false,
                displayType = input.fieldInfo.FieldType,
                tooltip = "TESTINGss"
            };
            AddPortLocal(input.fieldInfo, Direction.Input, owner.connectorListener, portData);

            // InsertPort(CreatePortView(Direction.Input, input.fieldInfo, portData, owner.connectorListener), 0);
        }
    }

My next question is once these input ports are made how do I get the value from it programatically (preferably in the BaseNode)?

@dannymate
Copy link
Author

dannymate commented Jan 28, 2022

Thanks to Sheepy in the discord for pointing me in the rght direction I've managed to resolve this a different route.

Here's my code:

using System.Collections.Generic;
using System.Reflection;
using GraphProcessor;
using UnityEngine;

[System.Serializable]
public abstract class BaseActionNode<T> : BaseNode where T : TeleAction
{
    [Input(name = "In")]
    public ICollection<TeleAction> input;

    [Input("Action Data")]
    public Dictionary<string, object> actionData = new Dictionary<string, object>();

    [Output(name = "Out")]
    public ICollection<TeleAction> output;

    [SerializeField] public T action;

    public override bool isRenamable => true;
    private bool IsStartNode => this.GetPort("input", null).GetEdges().Count == 0;

    protected override void Process()
    {
        if (IsStartNode) input = new List<TeleAction>();

        UpdateActionWithCustomPortData();

        T actionClone = System.Activator.CreateInstance(typeof(T), action) as T;
        input.Add(actionClone);

        output = input;
    }

    protected virtual void UpdateActionWithCustomPortData()
    {
        // We clone due to reference issues
        Dictionary<string, object> actionDataClone = new Dictionary<string, object>(actionData);

        foreach (var field in GetInputFieldsOfAction())
        {
            if (!actionDataClone.ContainsKey(field.fieldInfo.Name))
            {
                field.fieldInfo.SetValue(action, default);
            }
            else
            {
                field.fieldInfo.SetValue(action, actionDataClone[field.fieldInfo.Name]);
            }
        }

        actionData.Clear();
    }

    #region Reflection Generation Of Ports

    private List<FieldPortInfo> GetInputFieldsOfAction()
    {
        List<FieldPortInfo> foundInputFields = new List<FieldPortInfo>();

        foreach (var field in typeof(T).GetFields(BindingFlags.Instance | BindingFlags.NonPublic))
        {
            foreach (var attribute in field.GetCustomAttributes(typeof(InputAttribute), true))
            {
                if (attribute.GetType() != typeof(InputAttribute)) continue;

                foundInputFields.Add(new FieldPortInfo(field, attribute as InputAttribute));
                break;
            }
        }

        return foundInputFields;
    }

    [CustomPortInput(nameof(actionData), typeof(object))]
    protected void PullInputs(List<SerializableEdge> connectedEdges)
    {
        if (actionData == null) actionData = new Dictionary<string, object>();
        foreach (SerializableEdge t in connectedEdges)
        {
            actionData.Add(t.inputPortIdentifier, t.passThroughBuffer);
        }
    }

    [CustomPortBehavior(nameof(actionData))]
    protected IEnumerable<PortData> ActionDataBehaviour(List<SerializableEdge> edges)
    {
        foreach (var field in GetInputFieldsOfAction())
        {
            yield return new PortData
            {
                displayName = field.inputAttribute.name,
                displayType = field.fieldInfo.FieldType,
                identifier = field.fieldInfo.Name,
                acceptMultipleEdges = false,
            };
        }
    }
    #endregion
}

public struct FieldPortInfo
{
    public FieldInfo fieldInfo;
    public InputAttribute inputAttribute;

    public FieldPortInfo(FieldInfo fieldInfo, InputAttribute inputAttribute)
    {
        this.fieldInfo = fieldInfo;
        this.inputAttribute = inputAttribute;
    }
}

With this code I'm able to get fields from a class inheriting from TeleAction with the Input attribute. I then use those fields to dynamically add input ports in the editor. I then take the data from those ports and update my action via reflection.

This is generic so any TeleAction type can have a node by just creating a child class. One thing to note if doing this yourself is that for the "CustomPortBehavior" & "CustomPortInput" methods to work in the children they need to be at least "protection" level methods.

NODE Script

using GraphProcessor;

[System.Serializable, NodeMenuItem("Custom/BoolActionNode")]
public class BoolActionNode : BaseActionNode<BoolDecisionAction>
{
    public override string name => "BoolActionNode";
}

Relevant TeleAction

    public class BoolDecisionAction : DecisionAction<BoolVariable[]>
    {
        [SerializeField, Input("Decision")] protected BoolVariable[] decisionObject;

        [Input("True Actions"), SerializeReference] protected List<TeleAction> trueActions;
        [Input("False Actions"), SerializeReference] protected List<TeleAction> falseActions;

        public override IEnumerator DoAction(IOriginator originator) {}
}

Visual Node:
image

This way I only have to edit the TeleAction file, no cross-referencing or missing a variable because I edited one file and not the other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant