# How do I save which NPCs the player has talked to?

I have built a save system that saves the players name, x and y location, map and the direction they are looking at in JSON.

{
data: [
{
x: 400
y: 424
d: UP
name: Ezia
mapname: startmap
}
]
}


Now I have dialogue in my game, but I want to progress in that as well. So for example you have "Guy 1" who I just talked to, he says some stuff, afterwards he says something else, different text. How can I save it so the game knows I have already talked to "Guy 1"? I need to do this for a lot of NPCs eventually, so it would be nice if I didn't do stuff like:

Guy1TalkedTo = 1
Guy2TalkedTo = 0
Guy3TalkedTo = 1


Etc.

I am not familiar to the particular engine you are working with, but in most languages or cases you can use a 1xn matrix (i.e. a vector) where n is the total number of NPCs, that stores the 1-or-0 values, so the position within the vector is related to the NPC number.

For instance, in pseudo-code:

already_talked = {1,0,1...n}
//where n is the total number of NPCs


So the first value in the vector relates to NPC1, the second value relates to NPC2, and so on.

If you save that to a simple text file together to the other information you are saving, it would end up looking like a simple comma-separated entry inside the text file, like the following:

x = 400
y = 424
d = UP
name = Ezia
mapname = startmap


EDIT:

In reply to your further detailing given in the comments to my answer, I will add the following. If you want to save the "already talked" state for many different maps, you have three fine options.

1) just don't restart counting the NPC index every map. So, suppose in the first map you have 3 NPCs and in the second you have 4. The first NPC in the first map will be the number 1, the second will be the number 2, the third will be the number 3. Then, the first NPC in map 2 will be the number 4, the second in map two will be the number 5, the third will be the NPC number 6 and the fourth in map two will be the NPC number 7.

That way, you can still store everything only in one variable "already_talked".

2) another option, if you want to re-start the NPCs indexing every map, is the following. It might look messier but is actually simpler to maintain, is to just use one storing variable per map. So, in pseudo-code:

already_talked_map1 = {1,0,1 ... n1}
//where n1 and n2 are, respectively, the total number of NPCs in maps 1 and 2


3) a last option, also if you want to re-start the NPCs indexing every map, is to use a mxn matrix instead of a vector, so the rows indicate the map index you are referring to and the columns indicate the NPC indexing you are referring to. But in that case the way of storing the matrix in an external file together with other non-matrix information (e.g. char name, x, y, d, etc) may vary depending on the language and engine you are using.

Personal suggestion: I myself usually go for the second option if I don't have hundreds of maps. If you do have hundreds of maps, go for options one or three (the first if you want simple implementation).

• Would it be smarter to store this Vector in the map file, or in the Save File? Like I have multiple maps. Nov 29, 2015 at 20:10
• @GigaNova If I understood your setting correctly, I would save it in the save file. Because in the variable "mapname" you already store the information about which map the character is in, correct?
– MAnd
Nov 29, 2015 at 20:19
• that is correct, but I also need to save the other ones incase the player goes back to another map for example. So overwritting it may not be the smartest option if I wanna do it for every map? Nov 29, 2015 at 20:22
• @GigaNova Ah, I see what you mean. I edited the answer. Take a look.
– MAnd
Nov 29, 2015 at 20:40
• option 2 sounds like a really good one, I'll go ahead and try to implement it. Thanks alot! Nov 29, 2015 at 20:48