index

Reference: Behaviours

asBEHAVE_CONSTRUCT
asBEHAVE_DESTRUCT
asBEHAVE_ASSIGNMENT
asBEHAVE_ADD_ASSIGN
asBEHAVE_SUB_ASSIGN
asBEHAVE_MUL_ASSIGN
asBEHAVE_DIV_ASSIGN
asBEHAVE_MOD_ASSIGN
asBEHAVE_OR_ASSIGN
asBEHAVE_AND_ASSIGN
asBEHAVE_XOR_ASSIGN
asBEHAVE_SLL_ASSIGN
asBEHAVE_SRL_ASSIGN
asBEHAVE_SRA_ASSIGN
asBEHAVE_ADD
asBEHAVE_SUBTRACT
asBEHAVE_MULTIPLY
asBEHAVE_DIVIDE
asBEHAVE_MODULO
asBEHAVE_EQUAL
asBEHAVE_NOTEQUAL
asBEHAVE_LESSTHAN
asBEHAVE_GREATERTHAN
asBEHAVE_LEQUAL
asBEHAVE_GEQUAL
asBEHAVE_LOGIC_OR
asBEHAVE_LOGIC_AND
asBEHAVE_BIT_OR
asBEHAVE_BIT_AND
asBEHAVE_BIT_XOR
asBEHAVE_BIT_SLL
asBEHAVE_BIT_SRL
asBEHAVE_BIT_SRA
asBEHAVE_INDEX
asBEHAVE_NEGATE
asBEHAVE_ADDREF
asBEHAVE_RELEASE
asBEHAVE_ALLOC
asBEHAVE_FREE

asBEHAVE_CONSTRUCT

Constructor, called when variables comes into scope.

Warning: Do not register a virtual method as a constructor. The virtual function pointer table will be initialized by the constructor function, thus the virtual method will fail.

Example:

void Constructor(Object *o)
{
  new(o) Object();
}

engine->RegisterObjectBehaviour("object", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Constructor), asCALL_DECL_OBJLAST);

asBEHAVE_DESTRUCT

Destructor, called when variable goes out of scope.

Example:

void Destructor(Object *o)
{
  o->~Object();
}

engine->RegisterObjectBehaviour("object", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(Destructor), asCALL_CDECL_OBJLAST);

asBEHAVE_ASSIGNMENT

Assignment '='.

The assignment behaviour should return a reference to itself. The operator parameter can have any type. If the parameter is a reference to an object of the same type as itself the function is used as a copy operator, which is important if the object needs to treat members especially, for example if the class holds pointers to resources.

If you pass parameters by &in, then it is a good idea to also make them const, as it can allow the compiler to make a few optimizations.

Example 1:

Object &Assign(Object *self, Object &other)
{
  *self = other;
  return *self;
}

engine->RegisterObjectBehaviour("object", asBEHAVE_ASSIGNMENT, "object &f(const object &in)", asFUNCTION(Assign), asCALL_CDECL_OBJFIRST);

Example 2:

Object &Object::operator=(int val)
{
  this->val = val;
  return *this;
}

engine->RegisterObjectBehaviour("object", asBEHAVE_ASSIGNMENT, "object &f(int)", asMETHOD(Object, operator=), asCALL_THISCALL);

asBEHAVE_ADD_ASSIGN

Add and assign '+='.

See assignment.

asBEHAVE_SUB_ASSIGN

Subtract and assign '-='.

See assignment.

asBEHAVE_MUL_ASSIGN

Multiply and assign '*='.

See assignment.

asBEHAVE_DIV_ASSIGN

Divide and assign '/='.

See assignment.

asBEHAVE_MOD_ASSIGN

Mod and assign '%='.

See assignment.

asBEHAVE_OR_ASSIGN

Bitwise or and assign '|='.

See assignment.

asBEHAVE_AND_ASSIGN

Bitwise and and assign '&='.

See assignment.

asBEHAVE_XOR_ASSIGN

Bitwise xor and assign '^='.

See assignment.

asBEHAVE_SLL_ASSIGN

Shift left and assign '<<='.

See assignment.

asBEHAVE_SRL_ASSIGN

Shift right logically and assign '>>='.

See assignment.

asBEHAVE_SRA_ASSIGN

Shift right arithmetically and assign '>>>='.

See assignment.

asBEHAVE_ADD

Add '+'.

This is a global behaviour that takes two operands and creates a new value. The function should not return a reference and shouldn't store the input values. The operands can be any type, reference or not, but one of the operands must be a registered type.

If you pass parameters by &in, then it is a good idea to also make them const, as it can allow the compiler to make a few optimizations.

Example 1:

Object Add(Object &a, Object &b)
{
  return a + b;
}

engine->RegisterGlobalBehaviour(asBEHAVE_ADD, "object f(const object &in, const object &in)", asFUNCTION(Add), asCALL_CDECL);

Example 2:

Object Add(Object &a, int i)
{
  return a + i;
}

engine->RegisterGlobalBehaviour(asBEHAVE_ADD, "object f(const object &in, int)", asFUNCTION(Add), asCALL_CDECL);

asBEHAVE_SUBTRACT

Subtract '-'.

See addition.

asBEHAVE_MULTIPLY

Multiply '*'.

See addition.

asBEHAVE_DIVIDE

Divide '/'.

See addition.

asBEHAVE_MODULO

Modulo '%'.

See addition.

asBEHAVE_EQUAL

Equal '=='.

Example 1:

bool Equals(Object &a, Object &b)
{
  return a == b;
}

engine->RegisterGlobalBehaviour(asBEHAVE_EQUAL, "bool f(const object &in, const object &in)", asFUNCTION(Equals), asCALL_CDECL);

Example 2:

bool Equals(Object &a, int i)
{
  return a == i;
}

engine->RegisterGlobalBehaviour(asBEHAVE_EQUAL, "bool f(const object &in, int)", asFUNCTION(Equals), asCALL_CDECL);

See also addition.

asBEHAVE_NOTEQUAL

Not equal '!='.

See equal.

asBEHAVE_LESSTHAN

Less than '<'.

See equal.

asBEHAVE_GREATERTHAN

Greater than '>'.

See equal.

asBEHAVE_LEQUAL

Less than or equal '<='.

See equal.

asBEHAVE_GEQUAL

Greater than or equal '>='.

See equal.

asBEHAVE_LOGIC_OR

Logical or 'or'/'||'.

See addition.

asBEHAVE_LOGIC_AND

Logical and 'and'/'&&'.

See addition.

asBEHAVE_BIT_OR

Bitwise or '|'.

See addition.

asBEHAVE_BIT_AND

Bitwise and '&'.

See addition.

asBEHAVE_BIT_XOR

Bitwise exclusive or '^'.

See addition.

asBEHAVE_BIT_SLL

Bitwise shift left logically '<<'.

See addition.

asBEHAVE_BIT_SRL

Bitwise shift right logically '>>', ie. clear left most bits.

See addition.

asBEHAVE_BIT_SRA

Bitwise shift right arithmetically '>>>', ie. set left most bits to the sign bit.

See addition.

asBEHAVE_INDEX

Indexing operator []. Must be registered as a object behaviour.

This behaviour is normally used to access an element in a collection based on an index, wether it be a numerical index or some other form. The behaviour normally returns a reference to the element so that it can be altered by the script, but it is allowed to return the element by value.

You'll normally want to register two versions of this behaviour, one that allow the element to be altered, and one that return a read-only value. This is useful when having constant objects.

Example:

int *Object::operator[](uint idx)
{
  if( idx >= collection.size() )
  {
    asIScriptContext *ctx = asGetActiveContext();
    if( ctx ) ctx->SetException("Out of range");
    return 0;
  }
  return &collection[int];
}

engine->RegisterObjectBehaviour("object", asBEHAVE_INDEX, "int &f(uint)", asMETHOD(Object, operator[]), asCALL_THISCALL);
engine->RegisterObjectBehaviour("object", asBEHAVE_INDEX, "const int &f(uint) const", asMETHOD(Object, operator[]), asCALL_THISCALL);

asBEHAVE_NEGATE

Unary negate operator -. Must be registered as a object behaviour.

This behaviour should return a new object with the effect of the operation applied to it.

Example:

Object Negate(Object *self)
{
  Object obj;
  obj.val = -self->val;
  return obj;
}

engine->RegisterObjectBehaviour("object", asBEHAVE_NEGATE, "object f()", asFUNCTION(Negate), asCALL_CDECL_OBJLAST);

asBEHAVE_ADDREF

Necessary for object handles to work. Allows the library to increase the reference counter for the object.

Example:

void Object::AddRef()
{
  refCount++;
}

engine->RegisterObjectBehaviour("object", asBEHAVE_ADDREF, "void f()", asMETHOD(Object, AddRef), asCALL_THISCALL);

asBEHAVE_RELEASE

Necessary for object handles to work. Allows the library to decrease the reference counter for the object. When the reference counter reaches zero the function should release any resources held by the object and free its memory.

Example:

void Object::Release()
{
  if( --refCount == 0 )
    delete this;
}

engine->RegisterObjectBehaviour("object", asBEHAVE_RELEASE, "void f()", asMETHOD(Object, Release), asCALL_THISCALL);

asBEHAVE_ALLOC

If you want to use custom memory management for an object type, you should register this behaviour. Some examples where this might be needed is if the object type is allocated from a memory pool, or if it is dynamically allocated from a DLL which doesn't share the same heap as the application.

This behaviour must be registered as an object behaviour despite the fact that it is a global function with the same signature as standard C's malloc() function, i.e:

void *my_alloc(asUINT size);

engine->RegisterObjectBehaviour("object", asBEHAVE_ALLOC, "object &f(uint)", asFUNCTION(my_alloc), asCALL_CDECL);

The function declaration should return a reference to the object type.

asBEHAVE_FREE

If the object type doesn't register the free behaviour then the library uses the standard C's free() function. Note that if the object type has the ADDREF and RELEASE behaviours registered then the library never frees the memory itself, since it expects that the application to free the memory when the reference count reaches zero.

void my_free(void *mem);

engine->RegisterObjectBehaviour("object", asBEHAVE_FREE, "void f(object &in)", asFUNCTION(my_free), asCALL_CDECL);

top