The Foreword
Hi! I'm just your average, fairly beginner, computer science student doing personal programming side projects, and who is probably out of his depth and has no clue what he's doing. So, I might need some extra help/explanation to understand things.
The Context
Games and Branching Narratives
It's very frustrating to put this context into words, but this diagram/picture really helps frame my question (Note: Potential spoilers for The Stanley Parable):
Something that has always been on my mind is how games can have branching 'paths'. There is only so far you can go with nested if-statements before you get a horrible mess. Now, after some research - I discovered the 'digraph'...
What I've Done
Well, I began to plan out a way to implement a digraph. I found this guide: https://medium.com/basecs/from-theory-to-practice-representing-graphs-cfd782c5be38 To be very useful, and I was going to implement an adjacency list.
To traverse the graph, I'd create a "graph cursor". The graph cursor could be set to a position if needed, but otherwise had to follow the directional rules of whichever node it currently 'sat' on.
The nodes would contain the data for whatever the 'event' was (e.g. A story dialog event where the player would have to choose which fraction to side with), in addition, it would also contain rules for 'prerequisites' to disable certain branches if the player didn't meet them (e.g. Player needs at least 100 reputation before being able to access a dialog option/quest line).
Now, there is the issue of how in the world each node would have any knowledge of what the other nodes are (How to know which nodes to link together) - which can be solved by taking a page from the web, and assign each node with a resource identifier...
But, here's where my inexperience is really leading to my downfall.
The Problem
I defined node as an 'event' but that... isn't quite correct.
In an overview, a digraph is just a set of related data. The rules define the directional nature of the data, but does not define the conditions for choosing one path over another.
I'm trying to force the data structure to store 'events', as if that's meaningful to the digraph. The digraph doesn't care what data it stores, it just defines how it is stored!
So... what does that mean?
Well, I came up with an alternative model/abstraction. Based more so on webpages/choose your own adventure books. Rather then representing each nodes and the connections between nodes - there is just a list of "pages" with IDs. A page can contain outgoing links based on the ID. If a page is deleted or a link points to the wrong thing, you just get a 404 error, or just a wrong link.
But... you can create a digraph model of this too.
The Question
So at the end of it all, is there any point in trying to implement a digraph structure? At least in this sort of context, I mean, obviously it's useful for any graph theory heavy stuff.
But like, was there any advantages of the digraph structure in the first place?
I haven't implemented/attempted to create any of this yet... this is just what I've found out from planning things out.
I just want to make sure I've got it right this time.
In reply to @Doc Brown
Bad pseudocode examples of the kinda thing I mean:
class digraph {
private List<node> myNodes;
private Dictionary<Node, List<Node>> myAdjacencyList;
public Digraph() {
//Constructor
}
public list<node> NeighboursOf (Node node) {
// Gets neighbours of node
}
public list<node> GetNode (string NodeResourceID) {
// Gets neighbours of node based on ID.
}
// Add node method, Add Adjacency, Remove Adjacency, etc.
}
class node {
// Does whatever a node does.
// GetNeighbours method, GetDataStoredByNode, CheckNodeRules
}
vs
public List<string> pages;
class PageInterpreter {
public LoadPage() {
//Loads a page,
}
// BAsicly, a 'web page', with everything in the bag.
}
void RunEngineCode() (
if (current_page has updated) {
LoadPage(currentPage);
}
UpdateAllElementsOfLoadedPage();
redrawScreenIfNeeded();
//etc.
)
main {
while (true) {
RunEngineCode();
}
}
If my question has issues, please contact me so I can improve it.
If my question is wrong, well... I don't know what to say. I need help from someone more experienced then me. If this isn't the right place to put it - tell me where else I should go.
Thank you.
class digraph { private List<node> myNodes; private Dictionary<Node, List<Node>> myAdjacencyList; public Digraph() { //Constructor } public list<node> NeighboursOf (Node node) { // Gets neighbours of node } public list<node> GetNode (string NodeResourceID) { // Gets neighbours of node based on ID. } // Add node method, Add Adjacency, Remove Adjacency, etc. } class node { // Does whatever a node does. // GetNeighbours method, GetDataStoredByNode, CheckNodeRules }
Hmmm... I'll add it to the question.... bad formatting in this.