Arrays in squiffy

I seem to recall this discussed earlier but there are no arrays in squiffy. Are there any in JS that survive going from section to section? Or am I just SOL...

Thanks in advance.


Hello.

I would bet you can use JSON.

    var arr1 = [1,2,3];
    squiffy.set("arr1", JSON.stringify(arr1))

To get it back:

    var arr1 = JSON.parse(squiffy.get("arr1"))

EXAMPLE

@title Array

[[test one]]

[[test one]]:
    var arr1 = [1,2,3];
    squiffy.set("arr1", JSON.stringify(arr1));

Done.

[[test two]]

[[test two]]:
    var arr1 = JSON.parse(squiffy.get("arr1"));
    arr1.push(4);
    squiffy.set("arr1",JSON.stringify(arr1));
    
arr1: {arr1}

That seems to work when I test it online.


This looks interesting. I'll give it a try tonight or tomorrow.

What does the push command do?


What does the push command do?

Oh, I was just adding (pushing) a 4 to the array, to make sure it worked as expected.


arr1 begins as [ 1, 2, 3 ].

Then, I push 4 to it, making it [ 1, 2, 3, 4 ].


Okay, a couple of questions:

How do I read a specific element (say, the second value)?

How to I write to a specfic element (again, we'll say the second value)?


How do I read a specific element (say, the second value)?

How to I write to a specfic element (again, we'll say the second value)?

Bracket notation.

myarray[0] = 6;
squiffy.sections['output section'].text = "The 3rd array element is " + myarray[2];

You can use array[index] like any other variable. I think in most cases you'll end up extracting an element and putting it into a single Squiffy attribute so you can display it:

[[somesection]]:
    var arr1 = JSON.parse(squiffy.get("arr1"));
    squiffy.set("fifthelement", arr1[4]);

The fifth element of arr1 is {fifthelement}.

You can also use pop to get the last element of the array, and shift to get the first. These functions both return the specified element, and remove it from the array, so they're good if you want to process the next item in an NPCs "to do list" or something like that.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums