Wouterdek/NodeNetwork

Is there any way to create an auto-expanding array of ports?

masonwheeler opened this issue · 2 comments

If you've worked with node graphs in Unity or Unreal, you've probably seen nodes that can take an arbitrary number of inputs, set up so that whenever you connect the last input, it creates a new port. (For example, a Sum node that lets you sum up any number of values.)

Is it possible to do the same thing with NodeNetwork? If so, how do you set it up? (And if not, could it be added?)

I dit it adding a little plus in the block , and by increasing the outputs vector

My project doesn't have nodes that pass values around, but I got the automatic endpoint creation by simply checking occasionally if the amount of connected endpoints was equal to the amount of total endpoints. Here's my code, which is run every second (if there's an event, it should be used, I was just doing this in a hurry).

        private void CheckOutputs()
        {
            int connectedOutputs = 0;

            foreach(var output in this.Outputs.Items)
            {
                if (output.Connections.Count > 0)
                {
                    connectedOutputs++;
                }
            }

            if (connectedOutputs >= this.Outputs.Count)
            {
                CreateNewOutput();
            }
            else if (connectedOutputs < this.Outputs.Count - 1)
            {
                RemoveExtraOutput();
            }
        }

        private void CreateNewOutput()
        {
            var newOutput = new ValueNodeOutputViewModel<bool>()
            {
                Name = "new",
                MaxConnections = 1,
                Value = Observable.Return(true)
            };

            this.Outputs.Add(newOutput);
        }

        private void RemoveExtraOutput()
        {
            var remove = new NodeOutputViewModel();

            foreach (var output in this.Outputs.Items)
            {
                if (output.Connections.Count <= 0)
                {
                    remove = output;
                    break;
                }
            }

            this.Outputs.Remove(remove);
        }

This code is on the custom node view model.