| AngelScript

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(asIObjectType *ot);
static CScriptGrid *Create(asIObjectType *ot, asUINT width, asUINT height);
static CScriptGrid *Create(asIObjectType *ot, asUINT width, asUINT height, void *defaultValue);
static CScriptGrid *Create(asIObjectType *ot, void *listBuffer);
// Memory management
void AddRef() const;
void Release() const;
// Type information
asIObjectType *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;
      }

# 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;
      }