Skip to content

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[] }
src/reference-arrays.ts2
const counts = intArray(3);
src/reference-arrays.ts2
const prizes = intArray<Item>(3, [Items.RUNE_SCIMITAR, Items.AMULET_OF_POWER, Items.LOBSTER]);
  • size: An Int literal. 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 plain Int.

An Ints<T>: index it to read or write, .length for how many it holds, and .map(fn) for one component per element.

An array of strings handed to a function the server calls. Index it and read .length; a store into one is refused (TS2E253).

  • 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 with while (i < ids.length).
  • length is what the array holds, read at run time.
  • At most 5000 slots.
refused/int-array.ts2
const Rows = (ids: Ints): Component => (
<layer w="fill" h="fill">
{ids.map((id, i) => ( // how many rows?
src/reference-arrays.ts2
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;
}
src/reference-arrays.ts2
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>
);

You mapped a parameter. Walk it with while and .length, or copy it into an array declared here first.