# Simple Graph Editor

## Demo scenes

In the Demo Scene > Scripts folder you can find a few example on how to use the graph API. Lets break the code down and see how they work:

#### Random path example

This example talks about the scene found in Simple-graph-editor > Demo scenes > Random path. In this scene we move one object around randomly based on the probabilities set on the edges (see the edge weights section for more information). The code used in this scene to move the object can be found in Simple-graph-editor > Demo scenes > Scripts > randomPath.cs and is the following:
``````
public class followWaypoints : MonoBehaviour {
public WayPoint target = null;
public float speed = 1f;
private Transform t;
void Start () {
t = this.gameObject.transform;
}
void Update () {
float distance = Vector3.Distance(t.position, target.transform.position);
t.position = Vector3.Lerp(t.position, target.transform.position, speed * Time.deltaTime / distance);
if (distance < 0.1)
target = target.getNextWaypoint();
}
}
``````
In the demo scene we have a white square moving around, this is the code attached to it that makes it possible. The square moves according to the weight assigned to the edges. At the top of the script we define a few variables and we also have a quick initialization on the Start() method:
``````
public WayPoint target = null;
public float speed = 1f;
private Transform t;
void Start () {
t = this.gameObject.transform;
}``````
• target: Must be set to the starting waypoint through the inspector, we have to start somewhere!
• speed: The speed at which the white square moves
• t (initialized in start): Transform of the current object
Now let's take a look at the Update() function. The first thing we do, with this line, is to calculate the distance from our object to the waypoint it is moving to:
``````
float distance = Vector3.Distance(t.position, target.transform.position);
``````
After that we move the object towards that waypoint, updating the t.position variable we assigned:
``````
t.position = Vector3.Lerp(t.position, target.transform.position, speed * Time.deltaTime / distance);
``````
And finally we check if we arrived to that waypoint by using the distance provided previously. If the distance is very small then we update our target with the next waypoint to follow:
``````
if (distance < 0.1)
target = target.getNextWaypoint();
}
``````
The .getNextWaypoint() function will return a waypoint, chosen randomly from the "outs" list based on the weight of the edges. You can also access all the information yourself and treat the waypoints in a way that suits you more by accessing the "outs" property. This is further explained in the next sections.

#### Predefined path example

This example talks about the scene found in Simple-graph-editor > Demo scenes > Predefined path. In this scene we move one object around based on a path we manually defined first. The code used in this scene to move the object can be found in Simple-graph-editor > Demo scenes > Scripts > predefinedPath.cs and is the following:
``````
public class predefinedPath : MonoBehaviour {
public WayPoint[] route;
public float speed = 1f;
private Transform t;
void Start () {
t = this.gameObject.transform;
}

int i = 0;
void Update () {
if (i >= route.Length) return;
float distance = Vector3.Distance(t.position, route[i].transform.position);
t.position = Vector3.Lerp(t.position, route[i].transform.position, speed * Time.deltaTime / distance);
if (distance < 0.1)
i++;
}
}``````
In this scene we have a square moving again, this is the script that makes it work. At the top of the script we define a few variables and initialize the transform of the current object inside the Start() function.
``````
public WayPoint[] route;
public float speed = 1f;
private Transform t;
void Start () {
t = this.gameObject.transform;
}

``````
• speed: The speed at which the white square moves
• t (initialized in start): Transform of the current object
• route: An array of waypoints that defines the path we will follow, this can be modified through the inspector to set the path or change the number of waypoints to follow.

The i variable tracks the current waypoint we are going to and is defined outside since the movement takes place across many frames.
Now let's take a look at the Update() function; the first thing we do is to check if we have already reached our destination, if we did then we do nothing and stay still:
``````
if (i >= route.Length) return;
``````
This script is very similar to the previous example; after that we calculate the distance from our object to the waypoint it is moving to:
``````
float distance = Vector3.Distance(t.position, route[i].transform.position);
``````
Then we move it towards that point at the speed we set. Notice that in this case the next waypoint we move towards is in the i position of the list, so we start at the position 0 and move our way up the list. When we are very close to the next waypoint we just add 1 to i to iterate through the array.
``````
t.position = Vector3.Lerp(t.position, route[i].transform.position, speed * Time.deltaTime / distance);
if (distance < 0.1)
i++;
``````

#### Finding path example

We won't go into a lot of detail with this one since it's an implementation of a BFS algorithm to find the path to the desired destiny waypoint, and that gets out of the scope of this guide. To do that the predefinedPath.cs script explained in the previous section was modified, First it finds the path using the BFS algorithm, then puts in on a list and finally iterates through that list as explained before. The example scene can be found at Simple-graph-editor > Demo scenes > Finding path and the script can be found at the path Simple-graph-editor > Demo scenes > Scripts > findPath.cs.

If you want to dig a bit and see how this code works try opening it in your editor, the code is well commented!

#### Projects

Simple Graph Editor
Simple Database

home
contact