ff_u_journal_per

Persistent journal.

The engine keeps the journal of a PC in NW_JOURNAL_* local ints, which are lost between sessions. SaveJournal() (see ff_i_journal) saves them as JSON in the persistent QUESTS string when the player exits and when the PC leaves an area. On login, they are copied back to the locals.

See https://nwn.wiki/display/NWN1/Journal+Editor.

Variables

ENABLE__FF_U_JOURNAL_PERSISTENT: (string) (opt-out) Set to "N" on module to disable the unit.

Source code

// @code

#include "ff_i_core"

const string ENABLE__FF_U_JOURNAL_PERSISTENT = "ENABLE__FF_U_JOURNAL_PERSISTENT";


// -----------------------------------------------------------------------------

//! @brief Restore the journal locals of oPC from its persistent QUESTS variable.
//! @param oPC The PC.
void RestoreJournal(object oPC);
void RestoreJournal(object oPC)
{
  if (!GetIsPC(oPC) || GetIsDM(oPC))
    return;

  json jQuests = JsonParse(NWNX_Object_GetString(oPC, "QUESTS"));
  if (JsonGetType(jQuests) != JSON_TYPE_OBJECT)
    return;

  json jNames = JsonObjectKeys(jQuests);
  int nCount = JsonGetLength(jNames);
  if (nCount <= 0)
    return;

  LogDebug(__FILE__, "Restore " + IntToString(nCount) + " quests for " + GetObjectInfo(oPC));

  int i;
  for (i = 0; i < nCount; i++)
  {
    string sQuest = JsonGetString(JsonArrayGet(jNames, i));
    json jQuest = JsonObjectGet(jQuests, sQuest);

    SetLocalInt(oPC, FF_I_JOURNAL_ENTRY + sQuest, JsonGetInt(JsonObjectGet(jQuest, "entry")));
    SetLocalInt(oPC, FF_I_JOURNAL_PREFIX + "TIME" + sQuest, JsonGetInt(JsonObjectGet(jQuest, "time")));
    SetLocalInt(oPC, FF_I_JOURNAL_PREFIX + "DATE" + sQuest, JsonGetInt(JsonObjectGet(jQuest, "date")));
  }
}


// -----------------------------------------------------------------------------

void main()
{
  string sEvent = NWNX_Events_GetCurrentEvent();
  if (sEvent == ON_REGISTER)
  {
    if (!GetModuleFlag(ENABLE__FF_U_JOURNAL_PERSISTENT, TRUE))
      return;

    SubscribeToEvent(ON_DEFAULT_CLIENT_ENTER, __FILE__);
    SubscribeToEvent(ON_DEFAULT_CLIENT_EXIT, __FILE__);
    SubscribeToEvent(ON_DEFAULT_AREA_EXIT, __FILE__);
  }
  else if (sEvent == ON_DEFAULT_CLIENT_ENTER)
    RestoreJournal(GetEnteringObject());
  else if (sEvent == ON_DEFAULT_CLIENT_EXIT)
    SaveJournal(GetExitingObject());
  else if (sEvent == ON_DEFAULT_AREA_EXIT)
    SaveJournal(GetExitingObject());
}