| AngelScript |

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grid template object

Path: /sdk/add_on/scriptgrid/

The `grid` type is a template object that allow the scripts to declare 2D grids of any type. In many ways it is similar to the array template object, but it is specialized for use with areas.

The type is registered with `RegisterScriptGrid(asIScriptEngine *engine)`.

# Public C++ interface

class CScriptGrid
{
public:
// Set the memory functions that should be used by all CScriptGrids
static void SetMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc);
// Factory functions
static CScriptGrid *Create(asITypeInfo *gridType);
static CScriptGrid *Create(asITypeInfo *gridType, asUINT width, asUINT height);
static CScriptGrid *Create(asITypeInfo *gridType, asUINT width, asUINT height, void *defaultValue);
static CScriptGrid *Create(asITypeInfo *gridType, void *listBuffer);
// Memory management
void AddRef() const;
void Release() const;
// Type information
asITypeInfo *GetGridObjectType() const;
int GetGridTypeId() const;
int GetElementTypeId() const;
// Size
asUINT GetWidth() const;
asUINT GetHeight() const;
void Resize(asUINT width, asUINT height);
// Get a pointer to an element. Returns 0 if out of bounds
void *At(asUINT x, asUINT y);
const void *At(asUINT x, asUINT y) const;
// Set value of an element.
// The value arg should be a pointer to the value that will be copied to the element.
// Remember, if the grid holds handles the value parameter should be the
// address of the handle. The refCount of the object will also be incremented
void SetValue(asUINT x, asUINT y, void *value);
};

# Public script interface

      class grid<T>
      {
        grid();
        grid(uint width, uint height);
        grid(uint width, uint height, const T &in fillValue);

        uint width() const;
        uint height() const;
        void resize(uint w, uint h);

        T &opIndex(uint, uint);
        const T &opIndex(uint, uint) const;
      }

grid()
grid(uint width, uint height)
grid(uint width, height, const T &in fillValue)

The constructors initializes the grid object. The default constructor will create an zero sized grid.

uint width() const
uint height() const

Returns the width and height of the grid.

void resize(uint w, uint h)

Resizes the grid to the new dimension. The elements that still fit in the grid will keep their values.

T &opIndex(uint x, uint y)
const T &opIndex(uint x, uint y) const

The index operator returns a reference to one of the elements. If the index is out of bounds a script exception will be raised.

# Example usage in script

      // Initialize a 5x5 map
      grid<int> map = {{1,0,1,1,1},
                       {0,0,1,0,0},
                       {0,1,1,0,1},
                       {0,1,1,0,1},
                       {0,0,0,0,1}};

      // A function to verify if the next area is walkable
      bool canWalk(uint x, uint y)
      {
        // If the map in the destination is
        // clear, it is possible to wark there
        return map[x,y] == 0;
      }