From 9fd6bf6470045bc27af55a5110f41a862e4ccd60 Mon Sep 17 00:00:00 2001 From: Julien Lecomte Date: Fri, 1 Mar 2024 15:14:22 +0100 Subject: [PATCH] Add Object: {Get|Set}NameByLanguage --- CHANGELOG.md | 1 + Plugins/Object/NWScript/nwnx_object.nss | 39 ++++++++++++++++++++++ Plugins/Object/Object.cpp | 43 +++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5872d14814d..3890c352fb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ https://github.com/nwnxee/unified/compare/build8193.36.12...HEAD ##### New NWScript Functions - Util: GetModuleTlkFile() +- Object: {Set|Get}LocalizedName() ### Changed - N/A diff --git a/Plugins/Object/NWScript/nwnx_object.nss b/Plugins/Object/NWScript/nwnx_object.nss index 03a99e4ecbb..acab7d82d53 100644 --- a/Plugins/Object/NWScript/nwnx_object.nss +++ b/Plugins/Object/NWScript/nwnx_object.nss @@ -418,6 +418,21 @@ int NWNX_Object_GetLastSpellInstant(); /// @param oCreator The new creator of the trap. Any non-creature creator will assign OBJECT_INVALID (similar to toolset-laid traps) void NWNX_Object_SetTrapCreator(object oObject, object oCreator); +/// @brief Return the name of the object for nLanguage. +/// @param oObject an object +/// @param nLanguage A PLAYER_LANGUAGE constant. +/// @param nGender Gender to use, 0 or 1. +/// @return The localized string. +string NWNX_Object_GetLocalizedName(object oObject, int nLanguage, int nGender = 0); + +/// @brief Set the name of the object as set in the toolset for nLanguage. +/// @note You may have to SetName(oObject, "") for the translated string to show. +/// @param oObject an object +/// @param sName New value to set +/// @param nLanguage A PLAYER_LANGUAGE constant. +/// @param nGender Gender to use, 0 or 1. +void NWNX_Object_SetLocalizedName(object oObject, string sName, int nLanguage, int nGender = 0); + /// @} int NWNX_Object_GetLocalVariableCount(object obj) @@ -1036,3 +1051,27 @@ void NWNX_Object_SetTrapCreator(object oObject, object oCreator) NWNX_PushArgumentObject(oObject); NWNX_CallFunction(NWNX_Object, sFunc); } + +string NWNX_Object_GetLocalizedName(object oObject, int nLanguage, int nGender = 0) +{ + string sFunc = "GetLocalizedName"; + + NWNX_PushArgumentInt(nGender); + NWNX_PushArgumentInt(nLanguage); + NWNX_PushArgumentObject(oObject); + + NWNX_CallFunction(NWNX_Object, sFunc); + return NWNX_GetReturnValueString(); +} + +void NWNX_Object_SetLocalizedName(object oObject, string sName, int nLanguage, int nGender = 0) +{ + string sFunc = "SetLocalizedName"; + + NWNX_PushArgumentInt(nGender); + NWNX_PushArgumentInt(nLanguage); + NWNX_PushArgumentString(sName); + NWNX_PushArgumentObject(oObject); + + NWNX_CallFunction(NWNX_Object, sFunc); +} diff --git a/Plugins/Object/Object.cpp b/Plugins/Object/Object.cpp index ed109d9476a..04dd10d0bb1 100644 --- a/Plugins/Object/Object.cpp +++ b/Plugins/Object/Object.cpp @@ -1285,3 +1285,46 @@ NWNX_EXPORT ArgumentStack SetTrapCreator(ArgumentStack&& args) } return {}; } + +NWNX_EXPORT ArgumentStack GetLocalizedName(ArgumentStack&& args) +{ + if (auto *pGameObject = Utils::PopGameObject(args)) + { + const auto nLanguage = args.extract(); + const auto nGender = args.extract(); + + CExoString myString; + + if (auto *pArea = Utils::AsNWSArea(pGameObject)) + pArea->m_lsName.GetString(nLanguage, &myString, nGender); + else if (auto *pObject = Utils::AsNWSObject(pGameObject)) + pObject->GetFirstName().GetString(nLanguage, &myString, nGender); + + return myString; + } + return {}; +} + +NWNX_EXPORT ArgumentStack SetLocalizedName(ArgumentStack&& args) +{ + if (auto *pGameObject = Utils::PopGameObject(args)) + { + const auto sName = args.extract(); + const auto nLanguage = args.extract(); + const auto nGender = args.extract(); + + CExoString myString(sName); + if (auto *pArea = Utils::AsNWSArea(pGameObject)) + { + pArea->m_lsName.RemoveString(nLanguage, nGender); + pArea->m_lsName.AddString(nLanguage, myString, nGender); + } + else if (auto *pObject = Utils::AsNWSObject(pGameObject)) + { + pObject->GetFirstName().RemoveString(nLanguage, nGender); + pObject->GetFirstName().AddString(nLanguage, myString, nGender); + } + } + + return {}; +}