Skip to content

Commit

Permalink
Merge pull request #1742 from julien-lecomte/local-object-setnamelang…
Browse files Browse the repository at this point in the history
…uage

Add Object: {Get|Set}NameByLanguage
  • Loading branch information
Daztek authored Mar 5, 2024
2 parents b9b5e6b + 9fd6bf6 commit 59b9bc2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions Plugins/Object/NWScript/nwnx_object.nss
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
43 changes: 43 additions & 0 deletions Plugins/Object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int32_t>();
const auto nGender = args.extract<int32_t>();

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<std::string>();
const auto nLanguage = args.extract<int32_t>();
const auto nGender = args.extract<int32_t>();

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 {};
}

0 comments on commit 59b9bc2

Please sign in to comment.