intArray
intArray(size) makes a fixed-size array of whole numbers that lives for one
function’s run. Ints<T> is its type; Strings is the type of a string array
a server call hands you, which nothing in the language can make. Reference
only: the collections a panel draws from are
listOf and mapOf, and a per-row value is a component’s own cell.
intArray<T = Int>(size: Int, initial?: T[]): Ints<T>
interface Ints<T> { [index: number]: T; readonly length: Int; map(fn: (value: T, index: Int) => Component): Component[] }interface Strings { readonly [index: number]: String; readonly length: Int; map(fn: (value: String, index: Int) => Component): Component[] }Reference
Section titled “Reference”intArray(size, initial?)
Section titled “intArray(size, initial?)” const counts = intArray(3); const prizes = intArray<Item>(3, [Items.RUNE_SCIMITAR, Items.AMULET_OF_POWER, Items.LOBSTER]);Parameters
Section titled “Parameters”size: AnIntliteral. How many slots.initial: The values to fill it with, in order. Optional; the values need not be constants. The type parameter says what the array holds —intArray<Item>(3)holds items, and the default holds plainInt.
Returns
Section titled “Returns”An Ints<T>: index it to read or write, .length for how many it holds, and
.map(fn) for one component per element.
Strings
Section titled “Strings”An array of strings handed to a function the server calls. Index it and read
.length; a store into one is refused (TS2E253).
Caveats
Section titled “Caveats”- The array lives in one function’s run. It is not shared with anything the function calls and does not survive the function returning. Nothing in it is state.
.map()only over an array declared here (TS2E254). Walk a parameter withwhile (i < ids.length).lengthis what the array holds, read at run time.- At most 5000 slots.
const Rows = (ids: Ints): Component => ( <layer w="fill" h="fill"> {ids.map((id, i) => ( // how many rows?Counting into buckets
Section titled “Counting into buckets” const bag = useInventory(Inventories.INVENTORY); const counts = intArray(3);
let slot: Int = 0; while (slot < bag.size) { if (bag[slot] === -1) { counts[EMPTY] = counts[EMPTY] + 1; } else if (bag.count(slot) > 1) { counts[STACK] = counts[STACK] + 1; } else { counts[SINGLE] = counts[SINGLE] + 1; } slot = slot + 1; }Mapping an array into components
Section titled “Mapping an array into components” const prizes = intArray<Item>(3, [Items.RUNE_SCIMITAR, Items.AMULET_OF_POWER, Items.LOBSTER]);
return ( <layer w="fill" h={40}> {prizes.map((item, i) => ( <sprite x={i * 44} y="center" w={36} h={32} item={item} itemCountMode="never" /> ))} </layer> );Troubleshooting
Section titled “Troubleshooting”This array arrived from somewhere else
Section titled “This array arrived from somewhere else”You mapped a parameter. Walk it with while and .length, or copy it into an
array declared here first.