MCPcopy Create free account
hub / github.com/anjo76/angelscript / AddFunction

Function AddFunction

sdk/samples/console/source/main.cpp:244–273  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

242}
243
244void AddFunction(asIScriptEngine *engine, string &arg)
245{
246 asIScriptModule *mod = engine->GetModule("console", asGM_CREATE_IF_NOT_EXISTS);
247
248 asIScriptFunction *func = 0;
249 int r = mod->CompileFunction("addfunc", arg.c_str(), 0, asCOMP_ADD_TO_MODULE, &func);
250 if( r < 0 )
251 {
252 // TODO: Add better description of error (invalid declaration, name conflict, etc)
253 cout << "Failed to add function. " << endl;
254 }
255 else
256 {
257 // The script engine supports function overloads, but to simplify the
258 // console we'll disallow multiple functions with the same name.
259 // We know the function was added, so if GetFunctionByName() fails it is
260 // because there already was another function with the same name.
261 if( mod->GetFunctionByName(func->GetName()) == 0 )
262 {
263 mod->RemoveFunction(func);
264 cout << "Another function with that name already exists." << endl;
265 }
266 else
267 cout << "Function added. " << endl;
268 }
269
270 // We must release the function object
271 if( func )
272 func->Release();
273}
274
275void DeleteFunction(asIScriptEngine *engine, string &arg)
276{

Callers 1

mainFunction · 0.85

Calls 6

GetFunctionByNameMethod · 0.80
RemoveFunctionMethod · 0.80
GetModuleMethod · 0.45
CompileFunctionMethod · 0.45
GetNameMethod · 0.45
ReleaseMethod · 0.45

Tested by

no test coverage detected