Display player name in public string

It’s a simple problem. I’m making a rpg game with dialogues. My dialogue lines are being inserted in public string variables in a script attached to the NPC. Now, at some point, I want the NPC to say the player’s name, which is stored in a variable inside a script () in the player.

I know how to call the name, by getting component PC in the player, but how could I use pc.playerName string in the public string variable?

You could use your own Placeholder-Syntax. E.g. whenever you want to output the player’s name, you write “Hello, $playerName!” in the Inspector.

Then, in your script where you output the dialogs, always instead of just outputting the string, use C#'s String.Replace()-Method, e.g.

outputLabel.text = dialogue2[2].Replace("$playerName", pc.name);

so the label’s text will be “Hello, Rafael!” (if the player entered the name Rafael of course).

If you have more similar things to replace, you might think about a dictionary

Dictionary<string, string> replacements;

where you store all the placeholders and their replacements (as soon as the value is known - might be, that the player has to input something else later in the game?).

And then, whenever you need to output dialogue, call a method

string ReplacePlaceholders(string textFromInspector) {
    string s = textFromInspector;
    foreach (KeyValuePair<string, string> r in replacements) {
        s = s.Replace(r.Key, r.Value);
    }
    return s;
}

You should not store it this way if you want to do dynamic changes to the dialogues.

But to answer :

PNJ.Say(DialogueDictionary*[j] + Player.Name)*
You can format string with the + symbol.
But to detect your [PlayerName] inside the string use SubString a C# stirng method to cut a string
You could do this manually or using IndexOf method.
Manually:
* *int index = 43;* *string piece = myString.Substring(index);* *
Using IndexOf you can see where the fullstop is:
int index = myString.IndexOf(".") + …

I know you can format with + (but inside the script) - my problem is I need to format the string outside, in the inspector, as shown in the picture.

I think the easiest answer would be to use String.replace(), by putting some sort of tag into the string, which does not appear anywhere else in the dialogue. Then, you can just use “myString”.Replace(“Tag”, “Playername”) to replace the Tag dynamically. (Assuming that you use scripts; Something like , maybe?). Maybe that solves your problem? But if you really need to change it in the inspector, I have no idea what you could do…

So essentially youre wanting to pass through a variable from the inspector and then display it as “hello player name” in the console?

So pass the variable into the inspector. It looks like youve called it element2 so we will go with that. To access this in the script you would do something like

//code here for npc chat call
String npc = "Hello " + element2 + " how are you?"

Its hard to understand what you are doing, but if you want the npc to say the players name you will have to call the variable of the players name.