Dynamic arrays

From ZDoom Wiki
Jump to: navigation, search
Note: This feature is for ZScript only.

Dynamic arrays are resizable versions of regular arrays, with built-in functions to allow easy management.

The types available are:

  • Double
  • Int
  • String
  • Pointer (e.g. Actor)
  • Class Types (e.g. Class<Actor>)

Dynamic arrays can be defined as a class member, or inside a function:

Class example
{
    Array<type> myArray;
    
    void exampleFunction() {
        Array<type> myArray2;
    }
}

NOTE: Class types have a small syntax requirement in order to work.

// Notice the gap in between the two closing angle brackets. This is needed.
Array<Class<Actor> > myArray;

Functions

  • uint Size();
Returns the size of the array.
  • void Copy(Array<type> other);
Replaces the array's contents with the contents of the specified other array.
  • void Move(Array<type> other);
Replaces the array's contents with contents of the specified other array, but leaves that other array in unspecified state. If you constructed some data in another array and you will only need it in its new location, this should be faster than Copy.
  • uint Find(<type> item);
Searches for a particular item of the array's type and returns the index of that item. If the item is not found in the array, returns Size().
  • uint Push (<type> item);
Appends item to the end of the array and expands it by one.
  • bool Pop ();
Destroys the item at the end of the array and decreases its size by one.
  • void Delete (uint index, int deletecount = 1);
Removes a range of entries from the array.
  • void Insert (uint index, <type>item);
Inserts an item at the specified index. Implicitly performs a resize to accommodate.
  • void ShrinkToFit ();
Shrinks the total array size to the last non-empty member's location.
  • void Grow (uint amount);
Increases the size of the array by the amount specified. (Where, I'm assuming is at the end.)
  • void Resize (uint amount);
Grows or shrinks the array to the exact amount. Destroys all content within the removed containers.
  • uint Reserve (uint amount);
Allocates 'amount' new entries and returns the index of the first one.
  • uint Max ();
Returns how many entries are allocated, also counting those which are currently not in use. Caution must be used here since this can return a number greater than the array's actual size itself.
  • void Clear ();
Shrinks the array to 0 and destroys all contents.

Null Checking

All that needs to be done is a call to see if the element exists.

Array<Thinker> thinkers;

// ...some code which assigns thinkers to the array...
// Always make sure the thinker size is substantial. Otherwise it'll throw an error.

if (thinkers.Size() > 0)
{
    // There is no need for casting of any kind. Just check it directly.

    if (!thinkers[element])
        Console.Printf("Detected a null element");
}

Examples

Nuvolachalk.png Note: This article lists no examples. If you make use of this feature in your own project(s) or know of any basic examples that could be shared, please add them. This will make it easier to understand for future authors seeking assistance. Your contibutions are greatly appreciated.