Building a JSON array

Perhaps I’m missing something, perhaps it’s the only way to do it.

I’m trying to figure out how JSON arrays work in NWN, so I’m playing with this small function in a trigger onEnter event.

void main()
{
    object oPC = GetEnteringObject();
    json test = JsonArray();
    test = JsonArrayInsert (test, JsonString("1"));
    test = JsonArrayInsert (test, JsonString("2"));

    string test_string =
        IntToString (JsonGetLength(test)) + "_" +
        JsonGetString(JsonArrayGet(test,Random(JsonGetLength(test))));

    FloatingTextStringOnCreature (
        test_string,
        oPC,
        TRUE
    );
}

The result are “2_1” or “2_2”… so I understand (I think) how to build up an array.

What I can’t find anywhere (nor my little knowledge javascript syntax is helping me) is if there’s any way of defining the array content without inserting its components individually. Like:

test = {"1","2"};

Any thoughts?

B.

I don’t think that will work. That is an example of doing something that really would require the compiler to understand the new type, which it doesn’t. The way the compilers deal with these new types is by creating a opaque type for it and treating it like a cookie. It’s probably some int value underneath but the compiler doesn’t actually deal with it. It just passes those opaque cookies to the engine functions which do understand the data type. Sort of like the original object type (except that one is built in). But this mechanism is used for effects, events, locations and a few others as well as the new stuff beamdog has added.

It’s actually a pretty decent way to be able to expose new engine functionality to the compiler. Another place where bioware really did a good job of designing this whole thing.

I see… Anyway its a more efficient way to store and retrieve data to/from db or objects.

Another possibility should be

json test = JsonParse("[\"1\", \"2\"]");
1 Like

That’s exactly what I was looking for.

Thx a lot,

B.