Page 1 of 1

Use Player name in dialouge?

Posted: Thu Oct 12, 2023 7:59 pm
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!

Re: Use Player name in dialouge?

Posted: Thu Oct 12, 2023 9:39 pm
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.)

Re: Use Player name in dialouge?

Posted: Sun Oct 15, 2023 11:03 pm
by Hereder
I went with your first suggestion, it was easier - and it works!
Super thankful for your help!

Re: Use Player name in dialouge?

Posted: Mon Oct 16, 2023 8:31 am
by Tony Li
Glad to help!