What is array?

Thanks to mici1234 for the explanation *applause*

What is Array? Arrays are simply list of "things".

What is a "thing"? It can be a number, a text, literally anything.

What matters is that array holds more than 1 "thing".

Think of list of numbers, or list of texts, or list of both numbers and texts.


Why are array actions required?

Before array actions were a thing, the "equivalent" array was using variables, more specifically variable holding a text SEPARATED by some character. One such example is blacklist, which is separated with /, so for example /mici1234/lisa/, then if you wanted to check whether an item is in that variable, you'd simply wrap the variable with / and check if it is included in the main "list".

This would work but what if you wanted to "index"? Basically, pick item at specific number? Or get length of the "list"? Because your "list" is simply texting a text with specific rule. At this point, you get arrays, which are actual proper lists, more specifically array actions are simply actions to interface with actual ActionScript3 arrays. ActionScript3 as you may know is language of PB2.

This allows you to do same things as text-list, but much more efficient and much easier. It is easy to get length, it is easy to index.

While it's not exactly convenient (There is no remove item action for example), it is definitely more convenient than having variable pretend to be a list.


Examples of using an array

First Case: Basic Way to create the array.

Paste this one into any trigger you desire and remember to add the USE region.

uid: #array_loop
enabled: true
maxcalls: -1

VarSetValue( "~ArrayString", "hello/world/forest/city" );
ShowText( "~ArrayString", "0" );
op349( "~ArrayString", "/" );
ShowText( "~ArrayString", "1" );
op350( "~ArrayString", "0" );
ShowText( "Counter: ~counter, Element: ~ArrayString", "2" );
VarAddValue( "~counter", "1" );

Result:

Task: How to print "forest" instead?


Second Case, Loop the element through the array.

For looping element, we had to prepare 2 triggers, first one is initiate the counter, the second one remains the same, just replace the index to the counter variable.

uid: #counter init
enabled: true
maxcalls: 1

VarSetValue( "~counter", "0" );
uid: #array_loop
enabled: true
maxcalls: -1

VarSetValue( "~ArrayString", "hello/world/forest/city" );
op349( "~ArrayString", "/" );
op350( "~ArrayString", "~counter" );
ShowText( "Counter: ~counter, Element: ~ArrayString", "2" );
VarAddValue( "~counter", "1" );

Result:

Task: what happens if the player pressed the button again?


Third Case, empty array, add elements, loop the element.

In the first 2 cases, the array is created by simply separating the string with a symbol. In this case, we are going to build a fresh, empty array and we are going to insert the element one by one.

Here's the code for your startup:

uid: #counter init
enabled: true
maxcalls: 1

VarSetValue( "~counter", "0" );
uid: #array_loop
enabled: true
maxcalls: -1

op354( "~ArrayString", "0" );
op352( "~ArrayString", "hello" );
op352( "~ArrayString", "world" );
op352( "~ArrayString", "forest" );
op352( "~ArrayString", "city" );
op350( "~ArrayString", "~counter" );
ShowText( "Counter: ~counter, Element: ~ArrayString", "2" );
VarAddValue( "~counter", "1" );

Result:

Task: How to insert, delete a specific elements in an array?


Fourth Case, Joining 2 arrays.

There's one more trigger action we haven't used, whhich is join the arrays.

Here's the code for you to start up:

uid: #array_loop
enabled: true
maxcalls: -1

op354( "~ArrayString", "0" );
VarSetValue( "~combined", "alpha/beta/gamma/delta" );
op349( "~combined", "/" );
op352( "~ArrayString", "hello" );
op352( "~ArrayString", "world" );
op352( "~ArrayString", "forest" );
op405( "~combined", "~ArrayString" );
ShowText( "Combined Array: ~combined", "2" );

Result:

Task: Given ~ArrayString and ~combined, both arrays, modify the code to show the output as:

alpha, beta, gamma, delta, hello, world, forest

Last updated