Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

An array of group sizes...

 

Syntax

Code Block
themeRDark
languagejavascript
linenumberstrue
// size of a group
var size = groupSizes[id]; // will throw error if element missing
 
// next available group ID
var numGroups = groupSizes.length;

Properties

PropertyTypeMandatoryDescriptionGame version
idNumber(error)

Specify the group ID that you want to query the size of (see "Notes" below for important information).

3.2
lengthNumber(error)

Returns the next available group ID, which can be used to create a completely new group.

3.2

Return values

ValueTypeNotesGame version
<size>

Number

If the group id has been used, it's current size is returned as an integer that is ≥ 0.3.2
undefinedUndefinedThe array element exists, but that group has never been used.3.2
<error>ReferenceErrorThe array element does not exist and therefore it's value cannot be referenced.3.2

Notes

The groupSizes[] global is a "sparse" array – there might be gaps in the numbering of it's elements, which means that groupSizes.length is not representative of the number of groups you've actually used.

All elements up to groupSizes.length - 1 will be defined, but only groups that have been used will have a numeric value (the others will have a value of undefined).

It's recommended to use the forEach() method when iterating the array (see example below).

Note also that groups defined in labels.ini will have negative indices in the array (which I'm not sure if .forEach() will process?).

Example

The .forEach() method simplifies iteration of sparse arrays – it will skip any undefined elements...

Code Block
themeRDark
languagejavascript
titleIterating through the groupSizes array...
linenumberstrue
groupSizes.forEach( function get(groupSize, groupID) {
	// any unused groups will be skipped, so this
	// groupID has been used, and groupSize will be ≥ 0
} );

When you need a new group, you can either use newGroup() or simply add stuff to a group that doesn't yet exist:

Code Block
themeRDark
languagejavascript
titleFast way to find next free group...
linenumberstrue
// add a structure to a new group
var newGroup = groupSizes.length;
 
groupSizes[newGroup]; // ReferenceError - no such element
 
groupAdd(newGroup, someStructure);
 
groupSizes[newGroup]; // 1
 
groupSizes.length; // == newGroup + 1

If you want to find the next free group that's already defined, you can do something like this (not recommended):

Code Block
themeRDark
languagejavascript
titleSlower way to find next free group...
linenumberstrue
var
	i = 0,
	nextUnused = groupSizes.length;
 
while (i < nextUnused) {
	(typeof groupSizes[i] != "number") ? nextUnused = i : ++i;
}
 
// 'nextUnused' now contains the numeric ID of the next unused group
 
groupAdd(nextUnused, someObject); // create group by adding someObject to it
Div
classbox
Availability
Status
colourYellow
title3.2+

Requires:

  • Warzone 3.2 and above.

Earlier versions:

Div
classbox
Contents

Jump to:

Table of Contents
maxLevel5

Include Page
.jslinks-groups
.jslinks-groups