Get PlayerPrefs String during Dialogue

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
rrrmaynard
Posts: 3
Joined: Sun Dec 04, 2022 7:25 am

Get PlayerPrefs String during Dialogue

Post by rrrmaynard »

Hi,

I want to be able to, in the "Dialogue Text" of one of my conversations, have an actor mention a name saved in player prefs under a string.

For example:

"Oh hi (PlayerPrefs.GetString("playername") nice to see you."

It's based on player input, so how can I access whatever the player inputs in the dialogue system conversation node?
User avatar
Tony Li
Posts: 22142
Joined: Thu Jul 18, 2013 1:27 pm

Re: Get PlayerPrefs String during Dialogue

Post by Tony Li »

Hi,

There are two ways to do this:

1. Use the TextInput() sequencer command to read player input during a conversation and store it in a Dialogue System variable (more info). If you set up the save system, the variable value will be saved in saved games. Use the [var=variable] markup tag in your dialogue text to show the variable value. See here for example scene: How To: Read Input From Player During Conversations

2. Or register the PlayerPrefs.GetString() with Lua (info, tutorial). Then use the [lua(code)] markup tag to call PlayerPrefs.GetString("playername") in your text.
rrrmaynard
Posts: 3
Joined: Sun Dec 04, 2022 7:25 am

Re: Get PlayerPrefs String during Dialogue

Post by rrrmaynard »

Thank you so much for the help! I registered a function in Lua called "playername" like this:

Code: Select all

Lua.RegisterFunction("player", this, SymbolExtensions.GetMethodInfo(() => PlayerName(string.Empty)));
And then I created a function called PlayerName like this:

Code: Select all

    public void PlayerName(string message)
    {
        message = PlayerPrefs.GetString("playername");
    }
but when I type in

Code: Select all

[var=player]
in the dialogue text, it comes out as nil. Any reason why this would be?
User avatar
Tony Li
Posts: 22142
Joined: Thu Jul 18, 2013 1:27 pm

Re: Get PlayerPrefs String during Dialogue

Post by Tony Li »

Hi,

Change your C# method to:

Code: Select all

public string PlayerName()
{
    return PlayerPrefs.GetString("playername");
}
and register it with Lua like this:

Code: Select all

Lua.RegisterFunction("player", this, SymbolExtensions.GetMethodInfo(() => PlayerName()));
Use it in your Dialogue Text like this:
  • Dialogue Text: "Hello, [lua(player())]."
rrrmaynard
Posts: 3
Joined: Sun Dec 04, 2022 7:25 am

Re: Get PlayerPrefs String during Dialogue

Post by rrrmaynard »

Omg thank you!!!! It finally worked.
User avatar
Tony Li
Posts: 22142
Joined: Thu Jul 18, 2013 1:27 pm

Re: Get PlayerPrefs String during Dialogue

Post by Tony Li »

Happy to help!
Post Reply