NodeCanvas Forums › Support › Moving child nodes of priority selector should keep priorities intact › Reply To: Moving child nodes of priority selector should keep priorities intact
So I figure you re-order the connections so the graph doesn’t look ugly with all the crossed connections and I totally see why this is desirable behaviour. However if the connections have meta-information attached to them (like weights or priorities) these really should move with the connection otherwise this becomes quite confusing and sometimes you may not even notice it until you see strange behaviour in the game. I dug a bit into the code and I think I found a way to fix this without breaking the serialization.
In Editor.Nodes.cs in the TrySortConnectionsByPositionX
function I remember the old sorting of the list:
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 |
public void TrySortConnectionsByPositionX() { if ( !Application.isPlaying && graph != null && graph.isTree ) { if ( isChecked ) { return; } isChecked = true; // remember the old sorting var oldConnections = outConnections; // re-order outConnections = outConnections.OrderBy(c => c.targetNode.rect.center.x).ToList(); // call a callback so the node can fix connection metadata OnChildrenReordered(oldConnections); foreach ( var connection in inConnections.ToArray() ) { connection.sourceNode.TrySortConnectionsByPositionX(); } isChecked = false; } } Then in the priority selector I added this: public override void OnChildrenReordered(List<Connection> oldConnectionOrder) { var tempDict = new Dictionary<Connection, BBParameter<float>>(); for ( var i = 0; i < priorities.Count; i++ ) { tempDict[oldConnectionOrder<em class="d4pbbc-italic"></em>] = priorities<em class="d4pbbc-italic"></em>; } for ( var i = 0; i < outConnections.Count; i++ ) { var outConnection = outConnections<em class="d4pbbc-italic"></em>; priorities<em class="d4pbbc-italic"></em> = tempDict[outConnection]; } } |
It basically just sorts the connection metadata such that the order is consistent with the newly sorted connections. And I believe this should not have any effect on serialization as I don’t introduce any new fields. It is basically your suggestion with the inspector, just automated. Now the same thing could be implemented for every node type that tracks connection metadata (e.g. most of the selector nodes). And if you really want to keep the connection metadata and just want to move the node (as it is now) you could press e.g. the CTRL key and this would then just not call OnChildrenReordered.