Import data into an Array object
Array files only provide a kind of initialization data. You need to manually import them into array objects at runtime before they can be actually used.
1. Add these objects to the object type.
- The AJAX object is used to load this file.
- The Array object is used to store this data.
2. You need to add the action like this in the eventsheet.
You can also choose to load it in this way.
When you do this, you can preview the game in debug mode. You will observe that the data has been loaded into the Array object.
Get a data in array object
You can use Array(x, y)
in an expression to get a value.
For example, if you want to get the "Apple" in the this table.
You can observe the numbers at the top and left of the editor borders, which indicate their positions. And The index is 0-based.
Then you need to use it in the expression and fill in Array(x, y)
to get it.
For a 1D array, you can use Array.At(x)
directly.
Iterate the array to get each value
Array: For each
You can use the "For each" conditional of Array object.
In the "Foreach" conditional, you can use there expressions of Array to get the value of each iteration.
Array.CurValue
Array.CurX
Array.CurY
Array.CurZ
System: For
You can also use the "For" conditional of System. the same result can be obtained by loopindex
expression, and Array.At(x, y)
.
For a 1D array, you use Array.At(loopindex)
directly.
you can specify a column by Array.At(x, loopindex)
directly.
System: Repeat
For 1D arrays, you can also use Repeat to get the same result.
But use this system "For" allows you to iterate over a certain range of indexes more freely. Instead of traversing the entire array.
For example, you can skip the first line:
Loop the 2D array
If you use two or more For loops, you need to use the loopindex("name")
to distinguish different loopindex.
Example 1: Array For each
{"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"request-project-file","objectClass":"AJAX","parameters":{"tag":"\"\"","file":"items.json"}},{"id":"wait-for-previous-actions","objectClass":"System"},{"id":"load","objectClass":"Array","parameters":{"json":"AJAX.LastData"}}],"children":[{"eventType":"variable","name":"CurIndex","type":"number","initialValue":"0","comment":"","isStatic":false,"isConstant":false},{"eventType":"block","conditions":[{"id":"for-each-element","objectClass":"Array","parameters":{"axes":"xy"}}],"actions":[{"id":"create-object","objectClass":"System","parameters":{"object-to-create":"Text","layer":"0","x":"15 + 100 * CurIndex % Array.Height","y":"20 + 30 * floor(CurIndex / Array.Height)","create-hierarchy":false,"template-name":"\"\""}},{"id":"set-text","objectClass":"Text","parameters":{"text":"Array.CurValue"}},{"id":"add-to-eventvar","objectClass":"System","parameters":{"variable":"CurIndex","value":"1"}}]}]}]}
Example 2: For: Nested loop
{"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"request-project-file","objectClass":"AJAX","parameters":{"tag":"\"\"","file":"items.json"}},{"id":"wait-for-previous-actions","objectClass":"System"},{"id":"load","objectClass":"Array","parameters":{"json":"AJAX.LastData"}}],"children":[{"eventType":"block","conditions":[{"id":"for","objectClass":"System","parameters":{"name":"\"X\"","start-index":"0","end-index":"Array.Width-1"}},{"id":"for","objectClass":"System","parameters":{"name":"\"Y\"","start-index":"0","end-index":"Array.Height-1"}}],"actions":[{"id":"create-object","objectClass":"System","parameters":{"object-to-create":"Text","layer":"0","x":"15 + 100 * loopindex(\"X\")","y":"20 + 30 * loopindex(\"Y\") ","create-hierarchy":false,"template-name":"\"\""}},{"id":"set-text","objectClass":"Text","parameters":{"text":"Array.At(loopindex(\"X\"), loopindex(\"Y\"))"}}]}]}]}
Example 3: For: Single loop
{"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"request-project-file","objectClass":"AJAX","parameters":{"tag":"\"\"","file":"items.json"}},{"id":"wait-for-previous-actions","objectClass":"System"},{"id":"load","objectClass":"Array","parameters":{"json":"AJAX.LastData"}}],"children":[{"eventType":"block","conditions":[{"id":"repeat","objectClass":"System","parameters":{"count":"Array.Width * Array.Height"}}],"actions":[],"children":[{"eventType":"variable","name":"CurX","type":"number","initialValue":"0","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"CurY","type":"number","initialValue":"0","comment":"","isStatic":false,"isConstant":false},{"eventType":"block","conditions":[],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"CurX","value":"loopindex % Array.Width"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"CurY","value":"floor(loopindex / Array.Width)"}},{"id":"create-object","objectClass":"System","parameters":{"object-to-create":"Text","layer":"0","x":"10 + (100 + 2) * CurX","y":"10 + (32 + 2) * CurY","create-hierarchy":false,"template-name":"\"\""}},{"id":"set-text","objectClass":"Text","parameters":{"text":"Array.At(CurX, CurY)"}}]}]}]}]}
Array Header Search
You Can Use Array.indexOf("")
to Search for the position of a certain name in the first row.
For example, in the table below, the Array.indexOf("Price") can get the 3.