Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

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

Closed
snarlynarwhal opened this issue Aug 5, 2016 · 6 comments

Comments

@snarlynarwhal
Copy link

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
Copy link
Owner

Seneral commented Aug 7, 2016

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
Copy link
Author

snarlynarwhal commented Aug 8, 2016

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

  • Save the current canvas to the scene
  • Create a new canvas (as if I pressed the new canvas button)
  • Then add duplicate nodes

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
Copy link
Owner

Seneral commented Aug 8, 2016

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
Copy link
Author

snarlynarwhal commented Aug 8, 2016

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
Copy link
Owner

Seneral commented Aug 8, 2016

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
Copy link
Owner

Seneral commented Aug 10, 2016

Cool that you've found a solution:)

@Seneral Seneral closed this as completed Aug 10, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants