Page 1 of 1

Get PlayerPrefs String during Dialogue

Posted: Sun Dec 04, 2022 7:58 am
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?

Re: Get PlayerPrefs String during Dialogue

Posted: Sun Dec 04, 2022 8:10 am
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.

Re: Get PlayerPrefs String during Dialogue

Posted: Sun Dec 04, 2022 9:18 am
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?

Re: Get PlayerPrefs String during Dialogue

Posted: Sun Dec 04, 2022 9:39 am
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())]."

Re: Get PlayerPrefs String during Dialogue

Posted: Sun Dec 04, 2022 9:46 am
by rrrmaynard
Omg thank you!!!! It finally worked.

Re: Get PlayerPrefs String during Dialogue

Posted: Sun Dec 04, 2022 10:08 am
by Tony Li
Happy to help!