From 0b008c77ebe65c749a053e24b60dadbb3260e87d Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Fri, 26 Oct 2018 00:31:53 +0200 Subject: [PATCH] Fixed edge case where swapping connections connected to the same port would cause a "port is already connected" error --- Scripts/NodePort.cs | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 35ba41f..3f55795 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -296,29 +296,28 @@ namespace XNode { int aConnectionCount = connections.Count; int bConnectionCount = targetPort.connections.Count; + List portConnections = new List(); + List targetPortConnections = new List(); + + // Cache port connections + for (int i = 0; i < aConnectionCount; i++) + portConnections.Add(connections[i].Port); + + // Cache target port connections + for (int i = 0; i < bConnectionCount; i++) + targetPortConnections.Add(targetPort.connections[i].Port); + + ClearConnections(); + targetPort.ClearConnections(); + + // Add port connections to targetPort + for (int i = 0; i < portConnections.Count; i++) + targetPort.Connect(portConnections[i]); + // Add target port connections to this one - for (int i = 0; i < aConnectionCount; i++) { - PortConnection connection = connections[i]; - NodePort otherPort = connection.Port; - targetPort.Connect(otherPort); - } + for (int i = 0; i < targetPortConnections.Count; i++) + Connect(targetPortConnections[i]); - // Add connections to target port - for (int i = 0; i < bConnectionCount; i++) { - PortConnection connection = targetPort.connections[i]; - NodePort otherPort = connection.Port; - Connect(otherPort); - } - - // Remove starting connections on this port - for (int i = aConnectionCount - 1; i >= 0; i--) { - Disconnect(i); - } - - // Remove starting connections on target port - for (int i = bConnectionCount - 1; i >= 0; i--) { - targetPort.Disconnect(i); - } } /// Copy all connections pointing to a node and add them to this one