From FlightGear wiki
Revision as of 14:32, 15 April 2019 by Legoboyvdlp (Talk | contribs) (Describe how to call functions of objects stored in a vector)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

std.Vector provides an object-oriented way to use vectors in Nasal in FlightGear 3.3 or higher.

Creating a new vector

A new empty Vector can be created as follows:

var v =;

You can also wrap a standard Nasal vector in an instance of Vector by adding the Nasal vector as a parameter to new():

var v =['a', 'b', 'c']);

Manipulating vector objects

Adding items

A single item can be added using append():


To insert an item on a specific position, use insert(). The index is zero-based. The previous item on the given index will be shifted one position to the right:

var v =['a', 'c', 'd']);

v.insert(1, 'b');

To insert items to the front of the vector, use index 0. If a Vector has n items, then you can use indices 0 to n. Using index n will append the item to the Vector. It is also possible to use negative indices in order to insert items at a position relative to the end of the Vector:

var v =['a', 'b', 'd']);

v.insert(-1, 'c');

This example inserts 'c' at the old position of 'd'. 'd' will be shifted to the right. To insert an item at the front of the Vector with currently n items using a negative index, use index -n:

var v =['b', 'c', 'd']);

v.insert(-3, 'a');

Adding vectors

The Vector can be extended with a Nasal vector using extend():

v.extend(['x', 'y', 'z']);

Removing items

To remove the first occurence of an item, use remove():


If you try to remove a non-existing item, a ValueError will be raised. Use Nasal's call() function to catch this error. To remove and return an item at a given index, use pop():

var v =['a', 'b', 'd', 'c']);

var result = v.pop(2);
assert(result == 'd');

Just like with insert(), the index given to pop() can be negative. If you try to call pop() using an invalid index, then an IndexError will be raised.

To remove all items from the Vector, use clear():


Querying vector objects

To query the size of the Vector, use size():

var v =['a', 'b', 'c']);

var result = v.size();
assert(result == 3);

To query the index of the first occurence of an item, use index():

var v =['a', 'b', 'c']);

var result = v.index('c');
assert(result == 2);

contains() can be used to determine if the Vector contains the given item:

var v =['a', 'b', 'c']);

var result = v.contains('c');

Iteration using vector objects

In order to iterate over the items, you need to iterate over the wrapped Nasal vector itself:

var v =['a', 'b', 'c']);

foreach (var item; v.vector) {

Calling functions using vector objects

In order to call functions of objects stored in a vector, you need to refer to the wrapped Nasal vector itself:

var object = {
    char: '',
    new: func(char) {
        var objectChild = {parents:[object]};
        objectChild.char = char;
        return objectChild;
    outputInfo: func() {

var objects =['a')]);