Switch Language with Conversation Node

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
snoopyuj
Posts: 2
Joined: Thu Apr 13, 2023 8:35 am

Switch Language with Conversation Node

Post by snoopyuj »

Hello,

I was wondering if I can switch languages using Sequence or Lua script in a conversation node?
My requirement is to change the language while responding.

Thank you for your kind assistance :)
User avatar
Tony Li
Posts: 22131
Joined: Thu Jul 18, 2013 1:27 pm

Re: Switch Language with Conversation Node

Post by Tony Li »

Hi,

You could register a C# method with Lua and use it in the Script field. Example:

Code: Select all

private void Start()
{
    Lua.RegisterFunction("SetLanguage", DialogueManager.instance, SymbolExtensions.GetMethodInfo(() => DialogueManager.instance.SetLanguage("")));
}
Add a script with this method to the Dialogue Manager. Then use SetLanguage("your-language") in a dialogue entry's Script field.
snoopyuj
Posts: 2
Joined: Thu Apr 13, 2023 8:35 am

Re: Switch Language with Conversation Node

Post by snoopyuj »

It works flawlessly! Thank you very much!

Here is my code:

Code: Select all

public class LuaRegister : MonoBehaviour
{
    /// <summary>
    /// Unity Start
    /// </summary>
    private void Start()
    {
        Lua.RegisterFunction(nameof(SetLanguage), this, SymbolExtensions.GetMethodInfo(() => SetLanguage(string.Empty)));
    }

    /// <summary>
    /// Switch Language
    /// </summary>
    /// <param name="_newLanguage"> New Language </param>
    private void SetLanguage(string _newLanguage)
    {
        // Update Language
        DialogueManager.instance.SetLanguage(_newLanguage);

        // Update Response
        DialogueManager.UpdateResponses();

        // Change actors' name
        var conversation = DialogueManager.masterDatabase.GetConversation(DialogueManager.currentConversationState.subtitle.dialogueEntry.conversationID);
        var actorIDs = new HashSet<int>();

        conversation.dialogueEntries.ForEach(entry => actorIDs.Add(entry.ActorID));

        foreach (var actorID in actorIDs)
        {
            var characterInfo = DialogueManager.conversationModel.GetCharacterInfo(actorID);
            characterInfo.Name = PixelCrushers.DialogueSystem.CharacterInfo.GetLocalizedDisplayNameInDatabase(characterInfo.nameInDatabase);
        }
    }
}
References:
Hope it helps somebody else :)
User avatar
Tony Li
Posts: 22131
Joined: Thu Jul 18, 2013 1:27 pm

Re: Switch Language with Conversation Node

Post by Tony Li »

Thanks for sharing!
Post Reply