Creating a Dynamic Dialog System

Hi all!

I believe this is the right place to post this question? I'm not entirely sure. I am using MV as that's just what I own.

The context:

I was attempting to duplicate a dynamic dialog system I saw in a game I backed on Kickstarter, World of Anterra. Their dialog system is designed to have the player enter a word when talking to an NPC, then the NPC will check a database and if there's a response for that word, display the response. This encourages the player to learn about the world on their own instead of just being explictly told things.

The way the devs describe it is that there are global dialog databases, regional dialog databases, etc, all the way down to personal NPC dialog databases. The system checks the smallest possible database first to see there's a response for the player entry, and then continually checks larger databases until it finds a response. There are several responses possible for each entry. Once the system determines how the NPC responds, that specific response is then seeded to that specific NPC so that the player can't get all possible responses by inputting the same entry with one NPC.

Spoiler: Pic for Example:
Example.png

Question 1:

I created a basic version of this system using events that worked quite well. It used common events, name input processing, and variables to determine the correct response for an NPC event. However, this only allowed for one response per entry. I got a friend involved who is more proficient in coding than I, and she managed to figure out how to store multiple responses and have the NPC event recall one response at random and display it. Which is super cool! Here's the eventing and script calls used for this.

Spoiler
Contents.png

Not shown: the "regional database" runs and then, using conditional branches, if there's not a matching response in the "regional" section it runs the "global" section which is copied and pasted underneath.

My question is, is this the most effecient way to go about this? Doing it the way I showed above requires 3 control variables to be used for each response, just for the dialog system. This would quickly add up to several hundred if not thousands of control variables.
('Apples', 'Oranges', and 'Grapes' were used as test responses because we didn't want to type out whole lines until we got it to work.)

Question 2:

Now that we're able to have an NPC event pick a stored response at random and display it, is there a way we could "seed" that specific response to the NPC? This would be to prevent the player from getting say, all possible responses to a question or topic by inputting the same word to one NPC.

We were trying to think through it but I'm not amazing with coding and have passable knowledge of the engine, and my friend is good at coding but doesn't know the game engine very well.

We were attempting to use a Self Control Switch in the NPC event, but that would require a switch for every response, right? And that would quickly get out of hand. Anyway, we couldn't figure out a way to do this because the NPC event is only setting a Regional Dialog Variable and then running a common event that starts the dialog system process. And I can't get the common event to talk to the NPC event.

Pics for system example:
Spoiler
NPC Event runs this:
Contents_2.png
Spoiler
Contents_3.png
Spoiler
Contents_4.png

Thank you all! If I need to be more clear, let me know! Happy game making! 🙂
Rather than storing your responses in game variables, I would store your databases in json files or in an actual SQL database and use node's SQL functions to retrieve data (which would prevent you from doing a web deployment of your game since that doesn't support node functions).

Then I would repurpose the variable approach to "seed" the response by storing the one retrieved in a variable, and have a conditional branch to check whether it's set. However, to save on game variables for this purpose, I would have the variable be an array with a multi-key index where the key is an array consisting of the map ID and event ID of the NPC, and the value is a key-value object where the key is the topic and the value is their response to it. That can then be retrieved as needed thereafter. Or do it like self switches where the key is [map ID, event ID, topic] and the value is the response.
My question is, is this the most effecient way to go about this? Doing it the way I showed above requires 3 control variables to be used for each response, just for the dialog system. This would quickly add up to several hundred if not thousands of control variables.
No. Aside from @Trihan's suggestion about using a proper database, I would suggest making use of a plugin that can display the results of a JavaScript eval in the text window. (Eliaquim has one, VisuStella message might be able to)

That way you're not needing to use any game variables in the process at all and you can just display the output of the database query directly.
I'm pretty sure I've seen plugins that let you set up variables like self switches, which would let you use self variables in each NPC event instead of using normal variables.

You could seed a response by setting up a conditional branch with the random response variable == 0, and inside it setting the random response to something.
that way the random response is randomized only when you talk to that event for the first time.

I'm trying to find that plugin :/

Edit: it's YEP_SelfSwVar. from Yanfly.
I'm pretty sure I've seen plugins that let you set up variables like self switches, which would let you use self variables in each NPC event instead of using normal variables.

You could seed a response by setting up a conditional branch with the random response variable == 0, and inside it setting the random response to something.
that way the random response is randomized only when you talk to that event for the first time.

I'm trying to find that plugin :/

Edit: it's YEP_SelfSwVar. from Yanfly.
Yeah, that's basically a plugin version of what I suggested.
I'm pretty sure I've seen plugins that let you set up variables like self switches, which would let you use self variables in each NPC event instead of using normal variables.

You could seed a response by setting up a conditional branch with the random response variable == 0, and inside it setting the random response to something.
that way the random response is randomized only when you talk to that event for the first time.

I'm trying to find that plugin :/

Edit: it's YEP_SelfSwVar. from Yanfly.
If it was designed this way, the events and scripting would need to be run inside of the NPC event, not as a common event, correct? But in theory, once you have one event done, you could just copy/paste the event and change the sprite.

To make a change to it, you could just use Yanfly's Event Copier plugin to make sure any changes get updated across every duplicate event.

EDIT: Actually, Yanfly's Event Copier copies everything, including the sprite. Unless I'm mistaken, I don't think there's a way to turn that part off. So I'd need to make several different versions? Or am I over/underthinking this.

I'm not sure how to double quote here so, Trihan said:
Rather than storing your responses in game variables, I would store your databases in json files or in an actual SQL database and use node's SQL functions to retrieve data (which would prevent you from doing a web deployment of your game since that doesn't support node functions).

Then I would repurpose the variable approach to "seed" the response by storing the one retrieved in a variable, and have a conditional branch to check whether it's set. However, to save on game variables for this purpose, I would have the variable be an array with a multi-key index where the key is an array consisting of the map ID and event ID of the NPC, and the value is a key-value object where the key is the topic and the value is their response to it. That can then be retrieved as needed thereafter. Or do it like self switches where the key is [map ID, event ID, topic] and the value is the response.
I'll see if my coding friend understands what this means. As I'm guessing that this might be a cleaner way, in the long-run, to do this? (As opposed to doing with with plugins.)
I'm not sure how to double quote here
Just do whatever you did to quote the first time. If you press Reply on another post, it'll post the quote wherever your cursor is.

You can also use the +Quote buttons ahead of time to quote multiple posts and arrange which order they get pasted in. Or you can highlight a portion of text with your mouse and the Reply button for just that bit will show up.

this might be a cleaner way, in the long-run, to do this? (As opposed to doing with with plugins.)
Ehn? Plugins, generally, have nothing to do with it. I personally think Trihan's centralized method is a bit cleaner than using self-variables/switches on individual events that you have to keep track of someplace. But either way could absolutely work.
If it was designed this way, the events and scripting would need to be run inside of the NPC event, not as a common event, correct? But in theory, once you have one event done, you could just copy/paste the event and change the sprite.
I think you could make each NPC event have it's own self variables, then make said NPC event set the global variables that the common event uses to equal it's own self variables before calling the common event.

that way the common event can reference the NPC's self variables, and you can use a common event. if I'm understanding this right.

also, I would warn against building systems you can't clearly understand like using an SQL database without knowledge of SQL.

I've heard ugly code you understand is better than clean code you aren't sure of as long as it works.

but that's just my opinion.
Ok, upon looking into Yanfly's self variable plugin, the self variables are still managed in the normal variable window, which doesn't immediately solve the issue.

However, I dug a little further into large arrays. It seems that you could store each NPC's responses in their own, single self-variable as a very long array. Each entry in the array could be labeled as a number. Then using Yanfly's Message Eval plugin (only to have less events and write the script and text display at the same time), you could write a script that uses the Find function to look at a certain index of the array (we'll just use 50), and see if it's labeled 50. If it is, it'll pick a random response from another array, diplay it, then replace index 50 from the original with the random statement. This would also mean that if there was a statement in index 50, it would just display the statement. Right?

The article I was reading was saying that the Find function was finding a result in about 1ms up to about 10,000 indexes, so I dont think having many possible topics would cause lag or anything. I also think this might be the cleanest way to store all of the data.

Unfortunately I think this still means that the dialog event will need to run inside of the npc event, because if the dialog event was a common event, as I was doing before, there's still no way for the common event to know which NPC event the player is talking to. Right? Is there a non-messy way to make the common event reference which npc it's talking to?
Unfortunately I think this still means that the dialog event will need to run inside of the npc event, because if the dialog event was a common event, as I was doing before, there's still no way for the common event to know which NPC event the player is talking to. Right? Is there a non-messy way to make the common event reference which npc it's talking to?
I don't know to what specifics you're using, as I don't use MV, but you should be able to use 1 variable, that is only ever filled with a select bit of information. The same variable can be used by all your events, since it only ever passes key information to be used by the find() function.

For instance, if you set the variable to have the id of the event, the map it's on, and any other relevant information, when you then call the common event to have it display the message, it can check that variable for the id and/or any needed information, and then do what's needed.

Also, this may or may not be useful for you: Ink Intergration
Lots of suggestions here about how to store the NPC responses so that they don't change, but the thing is... to achieve what you're describing you don't actually need to store them. You want the response to a given word to be both random and always the same for each NPC. You can do that with seeded random numbers.

You might be familiar with the concept if you've played Minecraft or a similar game that generates something random but with an option to specify a seed. Basically it's a way of generating random numbers that will always be the same if you specify the same seed (n.b. the default JavaScript random numbers are also that, but the seed usually comes from nanoseconds on your system clock).

Sadly it is a bit harder in JavaScript that in other languages, since it doesn't support them by default, so you'd have to use a library for that, e.g. seedrandom. I suggest you show that to your friend, she should be able to use this to make a function that randomly selects an element from an array using a seed.

For the seed itself, I'd recommend using the name of the NPC + the word used. As long as the names are unique it should work differently for every NPC and word.
Lots of suggestions here about how to store the NPC responses so that they don't change, but the thing is... to achieve what you're describing you don't actually need to store them. You want the response to a given word to be both random and always the same for each NPC. You can do that with seeded random numbers.

You might be familiar with the concept if you've played Minecraft or a similar game that generates something random but with an option to specify a seed. Basically it's a way of generating random numbers that will always be the same if you specify the same seed (n.b. the default JavaScript random numbers are also that, but the seed usually comes from nanoseconds on your system clock).

Sadly it is a bit harder in JavaScript that in other languages, since it doesn't support them by default, so you'd have to use a library for that, e.g. seedrandom. I suggest you show that to your friend, she should be able to use this to make a function that randomly selects an element from an array using a seed.

For the seed itself, I'd recommend using the name of the NPC + the word used. As long as the names are unique it should work differently for every NPC and word.
This is quite interesting and obviously not something I had heard of before. I'll definitely show this to my friend, thank you!

So the logic behind this is actually just to create a function that picks a response at random, but then always pick same response, yes? How would one store the library? Just create a JS file, keep it in the javascript folder and have the function reference it? Does that work with save game data?

I'm assuming that this function would need to be inside of each NPC event. Is that correct? As opposed to a being in a common event? That's something else I'm hung up on. Because I can't think of a way to pass information to the common event that would tell it which NPC initiated the dialog. Not without another 100 conditional branches anyway.

Sorry, I'm just running into things I hadn't thought of before! I'm curious and want to learn! 🙂

---------

For everyone who has responded, thank you so much for helping me figure this out! I very much appreciate all of you! 🙂
So the logic behind this is actually just to create a function that picks a response at random, but then always pick same response, yes? How would one store the library? Just create a JS file, keep it in the javascript folder and have the function reference it? Does that work with save game data?

As to storing the library, just add it in the same way you'd add a plugin: it won't show a description or anything, but it should work perfectly fine.
Then my suggestion would be to create a function (probably a task for your friend) that will take a seed and an array as arguments, and return a randomly chosen element of that array using the seed. That function would also exist in a JavaScript file, since it's basically a small plugin (and should be added lower in the plugin manager than the library). That function can then be called using a script inside an event.
I'm not sure about your question about how it works with save game data - the whole point is that it never needs to save anything; your NPCs will say the same things across different saves or even copies of the game.

As for passing an argument to a common event, the simplest way is to dedicate one variable that's used for passing an argument to that specific event, and then set that variable before calling it.
I'm not sure about your question about how it works with save game data - the whole point is that it never needs to save anything; your NPCs will say the same things across different saves or even copies of the game.
Ok, I wasn't sure. I'm just not familiar with that function and didn't know how that worked. But I can most likely get my friend to explain that to me. She's good at translating in a way that I can understand.

I'll get back to you if I have anymore questions/issues/breakthroughs. Thank you! 🙂
Hi all!

Sorry for not getting back for a while, I've been pretty busy with work/life. My coding friend was also too busy to look at this, so I think if I do end up going this direction, I think I'll use variables and Yanfly's message eval plugin to run the script and display a message.

Thanks to everyone who took the time to respond and look at this!

Original thread: https://forums.rpgmakerweb.com/threads/creating-a-dynamic-dialog-system.154375/