Creating a visual novel using Graph Databases
Tags: apacheage, postgres
Published: 2023-03-30 22:57:34

## INTRODUCTION
Visual Novels are minimal games that work by telling a story based on user defined choices. The user is presented with scenarios and choices that lead to other scenarios based on the choice made by the user. To explain the concept using Graph databases, the user is expected to have some type of Graph database installed on their system before reading this blog post. This tutorial uses Apache AGE to achieve the same purpose.
Read more about Apache AGE here: https://age.apache.org/
GitHub here: https://github.com/apache/age

## IDEA
The idea is to store scenarios as vertices in the Graph database. The edges represent decisions made by the user. The user is allowed to carve a path through the graph as per his choices and have a brand new experience of his own.

Let's create a simple scenario that presents user with some choices, that lead to further different scenarios.

Scene (1): -
Jojo goes camping with his friends in the mountains. At night he hears a voice outside his camp. He has two options, either to do nothing, or to go out to investigate.

Doing nothing will lead to Scene 2: Voice getting louder.
Going outside to investigate will lead to alternate Scene 2: Outside being too dark to see anything.

Let vertices represent the scenarios. 

The idea is to store different scenarios in a graph like format which has a starting point and an end point. It can have a tree like structure (without any cycles) or it can have a cycles within or overall a cyclic structure. Either way it does not matter.

## SETTING UP
Here is a simple query to implement the said Vertices-scenarios and edges-choices structure.

```
CREATE (s1:SCENE {number:'s1_1', text:'Jojo goes camping with his friends in the mountains. At night he hears a voice outside his camp.'}),
(s2:SCENE {number:'s2_1', text:'Voice getting louder.'}),
(s3:SCENE {number:'s2_2', text:'Outside is too dark to see anything.'}),
(s1)-[:CHOICE {number:'c1_1', text:'Do nothing'}]->(s2),
(s1)-[:CHOICE {number:'c1_2', text:'Go out to investigate'}]->(s3)
RETURN s1, s2, s3
```
Here we have a number property with vertices and edges.
Where the letter denotes whether it is a scene or a choice.
The first number represents the scene it is, and the second number represents which alternative scene/choice it is.

## LET'S START
Let's use the following query to get the text of the first scene.

```
SELECT * from cypher('vn', $$
        MATCH (V:SCENARIO {number:'s1'})
        RETURN V.text
$$) as (V agtype);
```
Output:-

```
"Jojo goes camping with his friends in the mountains. At night he hears a voice outside his camp."
```

The following query can be used to get the available choices texts.

```
SELECT * from cypher('vn', $$
        MATCH (n:SCENARIO {number:'s1'})-[c]-(m)
        RETURN c.text
$$) as (V agtype);
```
Output:-

```
"Do nothing"
"Go out to investigate"
```
Let's pick choice 2.
The query would like something like this:-

```
SELECT * from cypher('vn', $$
        MATCH (V:SCENARIO {number:'s1'})-[c:CHOICE {number:'c1_2'}]->(m)
        RETURN m.text
$$) as (V agtype);
```

The aim of this blog post was to showcase the application of graph databases in creating Visual Novels. It is a very basic tutorial, however, given the kind of game you wish to make, the implementation can be much different.