Use Player name in dialouge?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Hereder
Posts: 76
Joined: Mon Dec 21, 2020 2:53 am

Use Player name in dialouge?

Post by Hereder »

Hi!
I have a simple script where I can name my player "Rune" for example, this is saved in my PlayerData sriptable object.
Can I somehow fetch the name of my player and show it in my dialogue?

Instead of the NPC saying " Hello there man, what's up?"
He can say "Hello there Rune, what's up?"

So, goes without saying, other players will have other names and their names will be shown in the Dialouge instead of Rune.

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Data/Player Data")]
public class PlayerData : ScriptableObject
{
    public string characterName; //Can I fetch this somehow?
    public Gender playerCharacterGender;
    public int saveSlotId;
}
Thanks!
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: Use Player name in dialouge?

Post by Tony Li »

Hi,

There are two ways to do that:

1. Set the Dialogue System's Player actor's Display Name to the same value:

Code: Select all

public PlayerData playerData; // Assume this variable points to your ScriptableObject.
...
DialogueLua.SetActorField("Player", "Display Name", playerData.characterName);
Then, assuming your Player actor is the conversation's Actor, use [var=Actor] in your Dialogue Text:
  • Dialogue Text: Hello there [var=Actor], what's up?"

2. Or write a C# method that returns the character's name, and register it with Lua. Example:

Code: Select all

public string GetPlayerName() { return playerData.characterName; }
...
void OnEnable()
{
    Lua.RegisterFunction(nameof(GetPlayerName), this, SymbolExtensions.GetMethodInfo(() => GetPlayerName()));
Then use [lua(GetPlayerName())] in your Dialogue Text:
  • Dialogue Text: Hello there [lua(GetPlayerName())], what's up?"

([var] and [lua] are markup tags.)
Hereder
Posts: 76
Joined: Mon Dec 21, 2020 2:53 am

Re: Use Player name in dialouge?

Post by Hereder »

I went with your first suggestion, it was easier - and it works!
Super thankful for your help!
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: Use Player name in dialouge?

Post by Tony Li »

Glad to help!
Post Reply