-
Notifications
You must be signed in to change notification settings - Fork 366
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
Comments
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) {}
} 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. |
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:
After Line Removed:
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
This is the code I'm using to generate the ports:
Port Generation Code
My next question is once these input ports are made how do I get the value from it programatically (preferably in the BaseNode)?
The text was updated successfully, but these errors were encountered: