| AngelScript |

Access masks and exposing different interfaces

An application may need to expose different interfaces to different types of scripts, e.g. a game entity may only have access to specific set of functions, while the GUI script can have access to a completely different set of functions.

To accomplish this the application can set a bitmask while registering the interface, and then use that bitmask when building the scripts to determine what the scripts should or should not be able to access.

void ConfigureEngine(asIScriptEngine *engine)
{
// Register the interface available to script type 1
engine->SetDefaultAccessMask(0x1);
r = engine->RegisterGlobalFunction("void func1()", asFUNCTION(func1), asCALL_CDECL); assert( r >= 0 );
// Register the interface available to script type 2
engine->SetDefaultAccessMask(0x2);
r = engine->RegisterGlobalFunction("void func2()", asFUNCTION(func2), asCALL_CDECL); assert( r >= 0 );
// Register the interface available to both script types
engine->SetDefaultAccessMask(0x3);
r = engine->RegisterGlobalFunction("void func3()", asFUNCTION(func3), asCALL_CDECL); assert( r >= 0 );
}
int CompileScript(asIScriptEngine *engine, const char *script, int type)
{
int r;
CScriptBuilder builder;
r = builder.StartNewModule(engine, script);
if( r < 0 ) return r;
// Set the access mask for the module, which will
// determine the functions from the application
// interface that can be called
asIScriptModule *mod = builder.GetModule();
mod->SetAccessMask(type);
// Add the script section and build the script
r = builder.AddSectionFromFile(script);
if( r < 0 ) return r;
return builder.BuildModule();
}

The access mask can be defined for the following entities in the application interface:

- Global functions
- Global properties
- Object types
- Individual methods of types
- Individual properties of types
- Individual object behaviours

asIScriptEngine::SetDefaultAccessMask
virtual asDWORD SetDefaultAccessMask(asDWORD defaultMask)=0
Sets the access mask that should be used for subsequent registered entities.
asIScriptEngine
The engine interface.
Definition: angelscript.h:1060
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:673
asIScriptModule::SetAccessMask
virtual asDWORD SetAccessMask(asDWORD accessMask)=0
Sets the access mask that should be used during the compilation.
asIScriptModule
The interface to the script modules.
Definition: angelscript.h:2166
asCALL_CDECL
A cdecl function.
Definition: angelscript.h:226