Seneral / Node_Editor_Framework

A flexible and modular Node Editor Framework for creating node based displays and editors in Unity
https://nodeeditor.seneral.dev
MIT License
2.01k stars 414 forks source link

Is it possible to include functionality inside of NodeEditorSaveManager to create new canvas? #87

Closed snarlynarwhal closed 8 years ago

snarlynarwhal commented 8 years ago

I needed to be able to duplicate a node graph to to the same canvas and to a new canvas. I easily figured out how to duplicate a graph to the same canvas quite easily, but in order to duplicate to a new canvas, I need to create one. So I know I can call NodeEditorWindow.NewNodeCanvas() from an editor script, but I want to be able to call it from a node (via a button), which doesn't appear to be able to access editor scripts. What's the best way for me to accomplish this?

Seneral commented 8 years ago

Sorry for the late reply. Wasn't able to set up email notifications on my (new) phone yet so I have to learn to manually check emails lol Anyway, it is easily possible. Just create a new NodeCanvas like you would do with every scriptable object and then populate it with whatever you want using code. After that you can save it if you want. All this should not cause any interferences with the currently loaded canvas:) Example:

NodeCanvas canvas = ScriptableObject.CreateInstance<NodeCanvas> ();
// Populate canvas with some nodes and stuff
NodeCanvasSaveManager.SaveNodeCanvas ("Assets/CreatedCanvas.asset", canvas, false);

It's pseudocode but it should work. Let me know if you encountered any errors of if I misunderstood you:)

snarlynarwhal commented 8 years ago

No worries and thanks! I should have probably elaborated more haha. I want my Duplicate (new canvas) button to:

So the main obstacle is loading the created canvas into the node editor window. Here's what I have so far:

private void DupeNewCanvas() {

            string curCanvasName = NodeEditor.curNodeCanvas.name;

            if (curCanvasName == null || curCanvasName == "" || curCanvasName == "LastSession") {
                Debug.LogError("ERROR: Save to Scene before duplicating a machine to a new canvas.");
            } else {
                NodeEditorSaveManager.SaveSceneNodeCanvas(curCanvasName, ref NodeEditor.curNodeCanvas, true);
                NodeEditor.curNodeCanvas = CreateInstance<NodeCanvas>();
                NodeEditor.curNodeCanvas.name = "New Canvas";
                NodeEditor.curEditorState = CreateInstance<NodeEditorState>();
                NodeEditor.curEditorState.canvas = NodeEditor.curNodeCanvas;
                NodeEditor.curEditorState.name = "MainEditorState";
                NodeEditor.curNodeCanvas.editorStates = new NodeEditorState[] { NodeEditor.curEditorState };
                NodeEditorSaveManager.SaveNodeCanvas("Assets/Editor/Node_Editor/LastSession.asset", NodeEditor.curNodeCanvas, true);
            }
}

This works at emulating clicking "new canvas" but only once. I'm assuming some caching prevents it from working again since clearing that allows me to do it again. If I attempted to create the node right after these lines of code, I get a NodeEditor.curNodeCanvas is null error. I tried delaying the call, but the canvas appears to stay null for some reason.

Seneral commented 8 years ago

Use NodeEditorWindow.mainNodeCanvas instead! I will try to emphasize this more, NodeEditorWindow is not strictly part of the framework, more like a default browser to built upon. It is the 'user' of the framework and holds the current canvas. The framework itself does not hold the canvas - NodeEditor.curNodeCanvas is only assigned while a user - in this case the window - draws the canvas. For the rest of the frame, it is null. This allows multiple 'users' to make use of the framework at the same time:) Anyway, enough explaining the why. Replacing NodeEditor.curNodeCanvas with NodeEditorWindow.mainNodeCanvas should do it;)

I do have a new update in development which emphasizes this, it is basically a wrapper for holding canvases, called a user. More about it once I commit it:)

snarlynarwhal commented 8 years ago

There's no way to access that script from a node, though right? Since it's not an editor script? If not, I'll just add my button else where. :) If I do use say the NodeEditorWindow to add my code, how do I get my node? For example, usually to get the gameobject being displayed by the inspector, you use Selected.activeGameObject. How would I get the active node? I'm pretty new to scriptable objects tbh haha - sorry for my noobness lol. EDIT: Found a work around that will do for now. Thanks! :)

Seneral commented 8 years ago

You can try to put your script in an editor folder. Obviously it then won't be able to be used at runtime... Try Assets/Plugins/Node_Editor/Nodes/Editor and if that doesn't work Assets/Plugins/Editor/Node_Editor/Nodes or similar:)

Seneral commented 8 years ago

Cool that you've found a solution:)