How To Access From Code

This page contains examples about the usage of GDIs graphs in your project.

Use A Graph In Your Game

The following example loads a graph at runtime and uses its chunk generator node to create landscapes. You can also find the script in the behaviour collection of GDI.

/// This is an example how to load a graph that contains a chunk generator
/// and how to use this generator programmatically.
/// It also adds a 'WASD' control to the 'Main Camera'.
public class GhunkGeneratorBehaviour : MonoBehaviour
{
	private ChunkGeneratorNode _chunkGenerator;
	private GameObject _camera;

	void Start ()
	{
		// Load the graph..
		string filePath = Application.streamingAssetsPath + "/Examples/BasicLandscape.json";
		Graph.Graph graph = Launcher.Instance.LoadGraph(filePath);

		if (graph == null)
		{
			Log.Error("Can not find graph file " + filePath);
			return;
		}

		// Get the chunk generator node..
		graph.ForceUpdateNodes();
		_chunkGenerator = (ChunkGeneratorNode) graph.GetFirstNodeWithType(typeof(ChunkGeneratorNode));

		if (_chunkGenerator == null) Log.Error("Can not find a chunk generator node in the graph.");

		// Get the main camera.
		_camera = GameObject.Find("Main Camera");
		if (_camera == null)
		{
			Log.Error("Can not camera with the name 'Main Camera'.");
			return;
		}

		_camera.AddComponent<KeyboardControls>();

		Debug.Log("OnStart");
	}


	void Update ()
	{
		if (_chunkGenerator != null && _camera != null)
		{
			// Update and request chunks...
			_chunkGenerator.UpdateChunks();
			_chunkGenerator.RequestChunks(_camera.transform.position);
		}
	}
}

Create A Graph Programmatically

Graphs can be created directly by a script. The following example creates a graph with some standard nodes. You can also find the script in the behaviour collection of GDI.
/// This class shows how to create graphs programmatically.
public class GraphByScriptBehaviour : MonoBehaviour
{

	void Start () 
	{

		// create the graph
		Graph.Graph graph = new Graph.Graph();

		// create an operator node
		var operator01 = (NumberOperatorNode) graph.CreateNode<NumberOperatorNode>();
		operator01.X = 200;
		operator01.Y = 40;
		operator01.SetMode(Operator.Add);
		graph.AddNode(operator01);

		// create a display node
		var diplay01 = (NumberDisplayNode) graph.CreateNode<NumberDisplayNode>();
		diplay01.X = 330;
		diplay01.Y = 80;
		graph.AddNode(diplay01);

		// link output and input sockets at the the index 0
		graph.Link(
			(InputSocket) diplay01.GetSocket(typeof(INumberConnection), typeof(InputSocket), 0),
			(OutputSocket) operator01.GetSocket(typeof(INumberConnection), typeof(OutputSocket), 0));

		// cerate a perlin noise node
		var perlinNoise = graph.CreateNode<UnityPerlinNoiseNode>();
		perlinNoise.X = 80;
		perlinNoise.Y = 250;
		graph.AddNode(perlinNoise);

		// create a display map node
		var displayMap = graph.CreateNode<DisplayMapNode>();
		displayMap.X = 300;
		displayMap.Y = 280;
		graph.AddNode(displayMap);

		// link the nodes
		graph.Link(
			(InputSocket) displayMap.GetSocket(typeof(INumberConnection), typeof(InputSocket), 0),
			(OutputSocket) perlinNoise.GetSocket(typeof(INumberConnection), typeof(OutputSocket), 0));


		// to test the serialization...

		// create a json out of the graph
		var serializedJSON = graph.ToJson();
		// dezeiralize the json back to a graph
		var deserializedGraph = Graph.Graph.FromJson(serializedJSON);

		// add the graph to the launcher to see it in the editor.
		Launcher.Instance.AddGraph(deserializedGraph);
	}

	void Update () {}
}

Subscribe To Editor Events

You can subscribe to editor events using the static EventManager class. This events are usually triggered by editing a graph in the editor window. The following example shows a register method that that you can call in the OnEnable or OnStart method of Unity. Take a look at the EventManager class to find the signatures of the methods you want to implement.

public void Register()
{
	EventManager.OnCreateGraph += OnCreate;
	EventManager.OnFocusGraph += OnFocus;
	EventManager.OnCloseGraph += OnClose;
	EventManager.OnLinkEdge += OnLink;
	EventManager.OnUnLinkSockets += OnUnLink;
	EventManager.OnUnLinkedSockets += OnUnLinked;
	EventManager.OnAddedNode += OnNodeAdded;
	EventManager.OnNodeRemoved += OnNodeRemoved;
	EventManager.OnChangedNode += OnNodeChanged;
	EventManager.OnFocusNode += OnFocusNode;
	EventManager.OnEditorWindowOpen += OnWindowOpen;
	EventManager.OnNodeRenamed += OnNodeRenamed;
	}

public void OnLink(Graph graph, Edge edge)
{
	Log.Info("OnLink: Node " + edge.Output.Parent.Id + " with Node " + edge.Input.Parent.Id);
	}

	// ...