How to Input name?

Announcements, support questions, and discussion for the Dialogue System.
mac
Posts: 81
Joined: Sat Aug 22, 2020 7:54 pm

How to Input name?

Post by mac »

So Tony, I'm using the JRPG template, and my game starts with a dialogue, and it goes like this:
The protagonist name is hidden, his name in the dialogue starts as (....). The protagonist is suddenly teleported into another world while he sleeps. He is teleported in the middle of a road, where he finds a rat humanoid.

First, the rat asks his name, where the player should input his name. (As I'm using the JRPG template this should not be hard I'm assuming?). So the protagonist name in the dialogue now and forever should be this string that the player has written.

Secondly, the rat introduces himself, so before that, his dialogue name would be something like "Rat Guy", and after introducing himself, his dialogue name would now become Romulus.

How can I achieve those two things?

Thank you in advance.
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to Input name?

Post by Tony Li »

Hi,

To input the protagonist's name, use the TextInput() sequencer command to input the name and store it in a variable. In the next node, assign that variable to the player actor's Display Name. Example:
  • Dialogue Text: "What's your name?"
  • Sequence: TextInput(Text Field UI, Name:, playerName)
...
(Next node:)
  • Dialogue Text: "Nice to meet you, [var=playerName]."
    Script: Actor["Player"].Display_Name = Variable["playerName"]
To reveal a name during a conversation, see the Discover Name Example on the Dialogue System Extras page.
mac
Posts: 81
Joined: Sat Aug 22, 2020 7:54 pm

Re: How to Input name?

Post by mac »

Thank you Tony, the first node works just fine I guess, but the second doesn't play the string, throwing this error:

Code: Select all

Dialogue System: Lua code 'Actor["Player"].Display_Name = Variable["playerName"];
' threw exception 'Cannot assign to a null value. Are you trying to assign to a nonexistent table element?.'
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to Input name?

Post by Tony Li »

Hi,

That Script line makes the assumption that the player actor in your database has the Name "Player". If you've changed the Name field, use the value that you set it to. For example, if you set the player actor's Name to "Protagonist":

Actor["Protagonist"].Display_Name = Variable["playerName"]

Note that you may instead want to use the Lua function that's in the Discover Name Example I mentioned above.
mac
Posts: 81
Joined: Sat Aug 22, 2020 7:54 pm

Re: How to Input name?

Post by mac »

Hey Tony, I got the NPC Actor to say the player input name (John in the screenshot case), but how can I now change the players actor name from "...." (before inputting his name) to the chosen name? Do I also use the Discover Name Example for the player Actor?
Attachments
Screenshot_4.png
Screenshot_4.png (127.14 KiB) Viewed 1703 times
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to Input name?

Post by Tony Li »

Hi,

Yes, use the same process for any actor (player or NPC).
mac
Posts: 81
Joined: Sat Aug 22, 2020 7:54 pm

Re: How to Input name?

Post by mac »

Nice Tony, everything works now, but I imagine that the Player Actor name doesn't change forever after you input the playerName variable, is there any script or anything I can run to overwrite the Player Actor name with the variable?
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to Input name?

Post by Tony Li »

Hi,

The player actor's name is stored in the Lua variable Actor["Player"].Display_Name.

If you use the save system and a Dialogue System Saver, this Lua variable will be saved.

If you don't use the save system, you can save the Lua variables to a string using PersistentDataManager.GetSaveData() and restore them using PersistentDataManager.ApplySaveData(string).
mac
Posts: 81
Joined: Sat Aug 22, 2020 7:54 pm

Re: How to Input name?

Post by mac »

Makes sense, but how do I exactly access the playerName variable we created? We didn't use any custom Sequencer command until now so I'm not sure how to store it with the PersistentDataManager.GetSaveData();, which I assume you can only use inside a script and not in the Sequence space of the dialogue entry.

I assume I have to create a Custom Sequencer Command, I did it and tried what I thought would work:

Code: Select all

namespace PixelCrushers.DialogueSystem.SequencerCommands
{

    public class SequencerCommandSavePlayerName : SequencerCommand
    { // Rename to SequencerCommand<YourCommand>

        public void Awake()
        {
            string playerName = PersistentDataManager.GetSaveData(); //nullref here.

            SaveManager.Instance.state.playerName = playerName; // My custom save manager.

            print("Player name is: " + playerName);

            Stop();
 
        }

    }

}
Thanks in advance.
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to Input name?

Post by Tony Li »

mac wrote: Mon Apr 25, 2022 9:01 pmMakes sense, but how do I exactly access the playerName variable we created?
I don't quite follow. For what reason do you need to access the playerName variable? It should really just be used as a temporary intermediate variable to hold the result of the TextInput() sequencer command until you copy the playerName variable's value to Actor["Player"].Display_Name. If you need the player's display name to change in the middle of the conversation, which I assume you do, change it using the Lua function in the Discover Name example.

But, to answer your question, you can use the DialogueLua class to access the playerName variable in C#.

Code: Select all

string playerName = DialogueLua.GetVariable("playerName").asString;

// You can also use DialogueLua to get an actor's Display Name:
string playerName = DialogueLua.GetActorField("Player", "Display Name").asString;
Post Reply