Page 1 of 1
Switch Language with Conversation Node
Posted: Thu Apr 13, 2023 8:50 am
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
Re: Switch Language with Conversation Node
Posted: Thu Apr 13, 2023 9:19 am
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.
Re: Switch Language with Conversation Node
Posted: Thu Apr 13, 2023 10:01 pm
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
Re: Switch Language with Conversation Node
Posted: Fri Apr 14, 2023 7:42 am
by Tony Li
Thanks for sharing!