Map2D

Inherits: Resource < Reference < Object

A two-dimensional data container.

Description

A data structure which holds an arbitrary number of elements accessed via 2D coordinates. Each cell represents a single element which stores any Variant compatible datatype. Can act as a helper class for other pixel-based classes such as Image, BitMap etc.

The map is initialized with create method:

var map = Map2D.new()
map.create(3, 4)
map.set_element(2, 3, "Goost")
print(map.get_element(2, 3)) # Prints: "Goost".

Traversing all elements can be done just like two-dimensional array (or image):

for y in map.get_height():
    for x in map.get_width():
        print(map.get_element(x, y))

Alternatively, all elements can be traversed using a simplified syntax:

for element in map:
    print(element)

Properties

Dictionary data {"data": [  ],"height": 0,"width": 0}

Methods

void clear ( )
void create ( int width, int height )
void create_from_data ( int width, int height, Array data )
void fill ( Variant with_value )
Variant get_cell ( Vector2 position )
Variant get_cell_or_null ( Vector2 position )
Variant get_element ( int x, int y )
int get_height ( ) const
Vector2 get_size ( ) const
int get_width ( ) const
bool has_cell ( Vector2 position )
bool is_empty ( ) const
void resize ( int new_width, int new_height )
void set_cell ( Vector2 position, Variant value )
void set_element ( int x, int y, Variant value )

Property Descriptions

Default {"data": [  ],"height": 0,"width": 0}

The data which represents this map. Used for storage.

Method Descriptions

  • void clear ( )

Clears all elements in this map.


  • void create ( int width, int height )

Initializes the map by allocating new data to hold width * height elements. By default, all elements of the map are initialized to null values.

Note: this clears all existing elements from the map before creation.


  • void create_from_data ( int width, int height, Array data )

Initializes the map from a continuos 1D array of data. The number of elements in data must be equal to width * height.


Fills all elements in the map with a specified value.


Similar to get_element, but accepts Vector2 as coordinates.


Returns cell’s value if the map contains an element at specified position, otherwise returns null. See also has_cell.


Returns an element at specified coordinates.


  • int get_height ( ) const

Returns the total number of rows.


Returns the map dimensions as Vector2 (width and height).


  • int get_width ( ) const

Returns the total number of elements per row.


Returns true if the map contains an element at specified position. Returns false if the position lies outside the map dimensions. See also get_cell_or_null.


  • bool is_empty ( ) const

Returns whether the map is empty (does not contain data).


  • void resize ( int new_width, int new_height )

Resizes the map to have a different number of elements. Resizing an existing map to a smaller size rearranges the elements to fit new width and height without preserving original positions (To-do: implement cropping method for this). Resizing an existing map to a larger size initializes new elements to null.


Similar to set_element, but accepts Vector2 as coordinates.


Sets any value to element at specified coordinates.