json

From NWN Lexicon
Jump to: navigation, search
Nwnee logo.jpg Note: This article documents Neverwinter Nights: Enhanced Edition new content or changes/updates/fixes to 1.69 functions. These are all listed under the category and patches pages.
The json data type is used to manipulate complicated data structures in NWN, both existing objects and new "Nuklear UI" or "NUI" which do new GUI elements. This is only available in NWN:EE.

You don't write the JSON directly as such (as a string for instance), but you can build it in NWN using the in built functions. You may also convert an object to JSON or import a JSON file built externally. You can also save and load the JSON from Sql.

You can dump the JSON you create to the log if you want to get it out of the game using WriteTimestampedLogEntry(JsonDump(jValue)). Alternatives to this are dumping in into an sqlite DB and reading it from there and printing it using SpeakString for the smallest of dumps and typing it out again.

There are a number of JSON Functions that can help manipulate, test, and use json.


Example

// Retrieves the soundset.2da line used for oCreature, or -1 on error.
int GetCreatureSoundset(object oCreature)
{
    if(GetObjectType(oCreature) != OBJECT_TYPE_CREATURE) return -1;
 
    // Convert to JSON
    json jGet = ObjectToJson(oCreature);
 
    // Retrieve the key
    jGet = JsonObjectGet(jGet, "SoundSetFile");
 
    if(JsonGetType(jGet) == JSON_TYPE_NULL)
    {
        SpeakString("JsonObjectGet error 1: " + JsonGetError(jGet));
        return -1;
    }
 
    // Retrieve the value
    jGet = JsonObjectGet(jGet, "value");
 
    if(JsonGetType(jGet) != JSON_TYPE_INTEGER)
    {
        SpeakString("JsonObjectGet error 2: " + JsonGetError(jGet));
        return -1;
    }
 
    return JsonGetInt(jGet);
}


See Also

functions:

JSON Functions