2
\$\begingroup\$

Hello guys I'm try to make some games that have more to it than just simple high score scrolling shooters etc.

I'm trying to design my own systm of handling narrative. I want to be able to place NPC's in the game world, with scripts attached to them. Some of the NPC's would just have n random greetings. Others will be mission givers and will offer you a quest of sorts. (The quest could be go speak to Bob in the pub, or it might be bring me the Holy Grail and Crown Jewels, or something in between).

I've wracked my brain for a long while over this but sadly keep slipping back to the only way I know how, which is to manually code all the strings that people can say and have a horrible web of if statements and other such insanity to give me a rudimental dialogue system. Its not good enough even with just a few characters, but if I had dozens/hundreds of characters it would be even worse.

I've read lots about it already but I'm still very confused when I start trying on my own project. I've read that Json/XML might be the way to go and that I can add custom markups in the Json text to call methods in my code, but I dont even know if this is right and if so how I can do it.

The back of my brain is screaming at me that there is a simpler way to do it, but I've sat here for a very long time without managing to think of ideas.

Here is a diagram I drew which pretty much shows the flow of how I want my NPC to behave. If anyone can see this and then tell me how they might do something like this that would be fantastic!! (note: if Json is the correct advise, please give me a exact example of how exactly to format the Json in order to check the bools I mention in my flowchart - thanks!):

enter image description here

note: The circles denote an output from the data as Text for the dialogue system. The bools will need to be checked against by the data file to see which output to give (hope that makes sense and is possible)

\$\endgroup\$
  • 1
    \$\begingroup\$ JSON and XML are just data formats. They don't represent the data structure used to handle what happens at runtime. In essence, they are no better or worse at solving your problem than txt or binary or any other encoding scheme. Except that they have a predefined syntax and deserializer methods. \$\endgroup\$ – Draco18s Nov 29 '18 at 20:10
  • \$\begingroup\$ But what ways are there to structure the data to have an end result as described above, purely from knowing the strings and the answer to those bools? \$\endgroup\$ – Big T Larrity Nov 29 '18 at 20:27
  • \$\begingroup\$ If it looks like a N-Way (or N-Ary) Tree and smells like a Tree, then it's probably a Tree. You even tag this with "treedata" Are you asking how to populate a tree in C#? \$\endgroup\$ – Patrick Hughes Nov 29 '18 at 22:42
  • \$\begingroup\$ Hi Patrick. Well I'm stuck in the dark with this particular part of game dev. And I can't even imagine the solution, let alone find one that works in practise @PatrickHughes \$\endgroup\$ – Big T Larrity Nov 30 '18 at 14:13
  • \$\begingroup\$ I just feel like string tags and if (bool) nests are not working for me. and I am hoping someone can show me/ or start me off , with something I have never considered or heard of before. Perhaps an intermediate level book or short tutorial would be ideal. The tutorials n books ive read so far dont really get to the point I am at which is when you want to make a game that entertains the user for longer than 10 minutes \$\endgroup\$ – Big T Larrity Nov 30 '18 at 14:16
6
\$\begingroup\$

It looks like you're trying to describe behaviour, and "tool for describing behaviour" is basically the definition of a programming language.

Data formats like XML and JSON suck for describing behaviour, because you can't have e.g. conditionals or mutable state. If you make your parser clever (such that it turns certain XML/JSON constructs into conditionals etc.), you'll soon realise you've implemented the world's worst programming language.

Use a programming language.


More specifically, perhaps you want a finite-state machine as an object for each mission, to track the player's progress in it? It could listen to appropriate events (like inventory changes), and change state accordingly. Then every NPC related to the mission could refer to that common data when deciding what they will say.

\$\endgroup\$
  • \$\begingroup\$ This is all new stuff to me !! Thanks so much, I will do my best to learn all I can about the finite-state machines and get this all working. I have been playing a game of Zelda on N64 and it made me wonder about how it is all put together (such as an Owl appears to help you at certain times/places - how did it know all about where i was at in the overall quest etc?? - these are the things I am going to try to learn for myself.) After skim-reading the wiki link, would you say Enum is a type of state-machine? I will now be looking up Finite State Machine Tutorial (LOL!) as the wiki post .... \$\endgroup\$ – Big T Larrity Dec 4 '18 at 14:51
  • \$\begingroup\$ ... has my head spinning somewhat :S \$\endgroup\$ – Big T Larrity Dec 4 '18 at 14:51
  • 1
    \$\begingroup\$ @SuperMegaBroBro Enums are a good way to store the "state" part of a state machine, but the full idea of a "state machine" also means you'll need to define the ways in which it can move from the current state to a different state. You're on the right track though. Wikipedia sometimes has an unfortunate habit of being too concise, to the point of being hard to understand. :) Finite-state machines are a common idea in programming, so you should be able to find other explanations online, like this animated explanation on YouTube. \$\endgroup\$ – Anko Dec 5 '18 at 14:50
  • \$\begingroup\$ thanks very much. I still haven't had time to tackle this new learning. But I'm definitely going to get stuck in soon. \$\endgroup\$ – Big T Larrity Dec 6 '18 at 2:51
1
\$\begingroup\$

Data belongs into files and logic belongs into code.

If you try to encode logic in form of JSON files, you are essentially inventing a JSON-encoded scripting language. Unity already has a scripting language. It's called C#. So adding a JSON layer on top of it won't be any simpler or more maintainable than just expressing your logic in C#. The attempt to avoid hard-coding logic by inventing a new language to hard-code your logic is a well-known anti-pattern called the Inner-platform effect.

But if you are going to implement complex NPC behaviours in C# in form of if/else trees, you will notice a few problems:

  • You will discover certain recurring patterns you want to reuse, like the typical fetch-quest or kill-quest loop.
  • You will want to have non-blocking logic. For example, you might have a player decision deep in an NPCs dialog tree. You can't just put that in an if-statement, because the engine needs to keep running while the player decides which option to click on.

Fortunately there is a design pattern which solves these two issues in a very elegant way: behavior trees.

  • Behavior trees can be nested, which allows you to create reusable behavior trees.
  • Behavior trees can be built at runtime, so you can have factory-methods which generate recurring behavior tree branches like the aforementioned fetch-quest branch.
  • Behavior trees can be processed asynchronously. A "let the player pick a response" node can return "running" while the player is making their decision.
\$\endgroup\$
  • \$\begingroup\$ thanks for the help from both answer givers here. I kinda felt like I was barking up the wrong tree, but haven't been able to learn about these 'Behaviour trees' and 'finite state machines' until now (not even heard of them). I again have a little time on my hands so this afternoon I am going to look at both these approaches which are new to me. Really am excited at the thought of better understanding this aspect of game programming. \$\endgroup\$ – Big T Larrity Dec 4 '18 at 14:47
  • \$\begingroup\$ Thanks for your link also. It looks exactly what I need. I haven't managed to read it thoroughly yet but this, along with the above answer, is where I will get started this evening when I try to add some of this functionality to my project \$\endgroup\$ – Big T Larrity Dec 4 '18 at 14:56
  • \$\begingroup\$ Really hard to decide which Answer to mark Correct. I'm reading Chris Sampson's Behaviour Tree article now and it seems the most helpful so far tbh. Although the 'finite-state machine' thing also seems like I need to understand that before I begin too. Both of the answers are 'Correct' for me. \$\endgroup\$ – Big T Larrity Dec 4 '18 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.