Dialogue Node children persists in the list after Undoing Node Creation

When I create a child node and undo it, the child node disappears but the children’s unique ID list doesn’t delete the child node’s uid. Also, the connection still stays because of this, but there’s no node to connect.


image

I’ve compared the code from this lecture’s commit and they’re more or less the same.

I’m guessing that the undo operation before node creation: Undo.RegisterCreatedObjectUndo deletes the created node but doesn’t update the list.

I was thinking of using something like Undo.undoRedoEvent (which gives us info about the undo group and name) to remove the child node from the Dialogue children list. But I’m not sure how to get the data of the child node after performing the Undo operation.

Undo.RegisterCreatedObjectUndo() by itself is only 1/2 the picture. This covers the creation of the new node as a ScriptableObject, but it doesn’t cover the modification to the current node itself.

        public void CreateNode(DialogueNode parent)
        {
            DialogueNode newNode = MakeNode(parent);
            //This covers the FileSystem's need to Undo the creation of the Node
            Undo.RegisterCreatedObjectUndo(newNode, "Created Dialogue Node");
            //This covers adding the node to the list of children.
            Undo.RecordObject(this, "Added Dialogue Node");
            AddNode(newNode);
        }

Undo.RecordObject(this, "Added Dialogue Node");
This line keeps track of the list of children of the Dialogue. I was referring to the list of string UIDs in the Dialogue Nodes.

I had to record the parent Dialogue Node for that:

    public void CreateNode(DialogueNode parent)
    {
        DialogueNode newNode = CreateInstance<DialogueNode>();
        newNode.name = Guid.NewGuid().ToString();

        Undo.RegisterCreatedObjectUndo(newNode, "Created Dialogue Node");

        if (parent != null)
        {
            Undo.RecordObject(parent, "Added child node");
            parent.childrenUIDList.Add(newNode.name);
        }

        dialogueNodes.Add(newNode);
        OnValidate();
    }

Also, in the DialougeEditor.cs, I added this line, inside the OnEnable method:

Undo.undoRedoPerformed += () => Repaint();

This makes sure to repaint every time we Undo. This sort of fixes the lag after every undo operation. I’m not fully confident.

Actually, that looks like a pretty good solution. You shouldn’t need to use a Lambda expression with the Undo.undoRedoPerformed, however. You should be able to assign it directly with

Undo.undoRedoPerformed += Repaint;

and be sure to unsubscrib in OnDisable

Undo.undoRedoPerformed-=Repaint;

or you might find yourself hip deep in Repaint instructions.

1 Like

Thanks for the tip, Brian.

Privacy & Terms