AngelScript
Using namespaces

Namespaces can be used to group related functions and other entities together. Doing so avoids potential conflicts with other entities that happen to use the same name, but is otherwise unrelated.

Namespaces can be used in the application registered interface, as well as in the scripts.

Registering the interface with namespaces

To register a function, or other entity in a specific namespace, the application should first call the method SetDefaultNamespace to define the desired namespace. After that the registration follows the normal procedure as described in the chapter on registering the interface.

void RegisterInNamespace(asIScriptEngine *engine)
{
int r;
// Register the type and function in the namespace
r = engine->SetDefaultNamespace("myspace"); assert( r >= 0 );
r = engine->RegisterObjectType("mytype", 0, asOBJ_REF); assert( r >= 0 );
r = engine->RegisterGlobalFunction("void myfunc()", asFUNCTION(myfunc), asCALL_CDECL); assert( r >= 0 );
}

If desired nested namespaces can also be used by separating them with the scoping token, ::, e.g. SetDefaultNamespace("outer::inner");

Finding entities in namespaces

As namespaces allow multiple declarations with the same signature, it is necessary to specify in which namespace a search for an entity is to be done. This is also done with the SetDefaultNamespace method. This applies to both the engine and the module interfaces.

void FindFuncInNamespace(asIScriptModule *module)
{
int r;
// Look for the function in the namespace, i.e. myspace::myfunc
r = module->SetDefaultNamespace("myspace"); assert( r >= 0 );
asIScriptFunction *func1 = module->GetFunctionByName("myfunc");
// When searching for a matching declaration the default namespace is also
// used unless an explicit namespace is given in the declaration itself.
asIScriptFunction *funcA = module->GetFunctionByDecl("void myfunc()");
asIScriptFunction *funcB = module->GetFunctionByDecl("void myspace::myfunc()");
assert( funcA == funcB );
}
asIScriptModule::GetFunctionByDecl
virtual asIScriptFunction * GetFunctionByDecl(const char *decl) const =0
Returns the function by its declaration.
asIScriptEngine::RegisterObjectType
virtual int RegisterObjectType(const char *obj, int byteSize, asDWORD flags)=0
Registers a new object type.
asOBJ_REF
@ asOBJ_REF
A reference type.
Definition: angelscript.h:250
asIScriptEngine::SetDefaultNamespace
virtual int SetDefaultNamespace(const char *nameSpace)=0
Sets the current default namespace for registrations and searches.
asIScriptEngine
The engine interface.
Definition: angelscript.h:1092
asIScriptModule::GetFunctionByName
virtual asIScriptFunction * GetFunctionByName(const char *name) const =0
Returns the function by its name.
asIScriptEngine::RegisterGlobalFunction
virtual int RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv, void *auxiliary=0)=0
Registers a global function.
asFUNCTION
#define asFUNCTION(f)
Returns an asSFuncPtr representing the function specified by the name.
Definition: angelscript.h:675
asIScriptFunction
The interface for a script function description.
Definition: angelscript.h:3823
asIScriptModule
The interface to the script modules.
Definition: angelscript.h:2218
asCALL_CDECL
@ asCALL_CDECL
A cdecl function.
Definition: angelscript.h:226
asIScriptModule::SetDefaultNamespace
virtual int SetDefaultNamespace(const char *nameSpace)=0
Sets the default namespace that should be used in the following calls.