Passing Variables from AngelScript to C++ -
i want pass variable angelscript c++.
managed pass functions angelscript c++ , vice versa.
can pass variables c++ angelscript, can't figure out how other way round.
manual didn't me or have overseen part.
can please give me hint?
edit:
as mentioned in comment here managed do.
in test.as file:
int add(int a, int b) { print("hello world, i'm angelscript\n"); multi(5, 13); print("c " + c + "\n"); return (a + b); }
my test.cpp file:
int multi(int x, int y) { int z = x * y; cout << "x aus dem skript: " << x << endl; cout << "y aus dem skript: " << y << endl; printf("ergebnis von multi x * y: %d\n", z); return z; } void print(string &msg) { printf("%s", msg.c_str()); } int _tmain(int argc, _tchar* argv[]) { int r; int c = 42; asiscriptengine *engine = ascreatescriptengine(angelscript_version); registerstdstring(engine); // pass function print angelscript r = engine->registerglobalfunction("void print(const string &in)", asfunction(print), ascall_cdecl); assert(r >= 0); // pass function multi angelscript r = engine->registerglobalfunction("int multi(int, int)", asfunction(multi), ascall_cdecl); assert(r >= 0); // pass variable c angelscript r = engine->registerglobalproperty("int c", &c); assert(r >= 0); file *f = fopen("test.as", "rb"); fseek(f, 0, seek_end); int len = ftell(f); fseek(f, 0, seek_set); string script; script.resize(len); fread(&script[0], len, 1, f); fclose(f); mod->addscriptsection("script", &script[0], len); mod->build(); asiscriptcontext *ctx = engine->createcontext(); // function add angelscript asiscriptfunction *func = engine->getmodule("test.as")->getfunctionbydecl("int add(int, int)"); ctx->prepare(func); ctx->setargdword(0, 7); ctx->setargdword(1, 20); if (ctx->execute() == asexecution_finished) { asdword returnvalue = ctx->getreturndword(); cout << "result of + b is: " << returnvalue << endl; } ctx->release(); engine->release(); return 0; }
here output when run code posted after making few small modifications.
https://gyazo.com/cb264842efa76d153f9f5c0f962b391d
here code after adding small changes, explain added after.
int multi(int x, int y) { int z = x * y; cout << "x aus dem skript: " << x << endl; cout << "y aus dem skript: " << y << endl; printf("ergebnis von multi x * y: %d\n", z); return z; } void print(string &msg) { printf("%s", msg.c_str()); } int _tmain(int argc, _tchar* argv[]) { int r; int c = 42; asiscriptengine *engine = ascreatescriptengine(angelscript_version); registerstdstring(engine); // pass function print angelscript r = engine->registerglobalfunction("void print(const string &in)", asfunction(print), ascall_cdecl); assert(r >= 0); // pass function multi angelscript r = engine->registerglobalfunction("int multi(int, int)", asfunction(multi), ascall_cdecl); assert(r >= 0); // pass variable c angelscript r = engine->registerglobalproperty("int c", &c); assert(r >= 0); file *f = fopen("test.as", "rb"); fseek(f, 0, seek_end); int len = ftell(f); fseek(f, 0, seek_set); string script; script.resize(len); fread(&script[0], len, 1, f); fclose(f); // create our module , add our script section build! asiscriptmodule *mod = engine->getmodule(0, asgm_always_create); r = mod->addscriptsection("script", &script[0], len); assert(r >= 0); r = mod->build(); assert(r >= 0); asiscriptcontext *ctx = engine->createcontext(); // function add angelscript asiscriptfunction *func = mod->getfunctionbydecl("int add(int, int)"); ctx->prepare(func); ctx->setargdword(0, 7); ctx->setargdword(1, 20); if (ctx->execute() == asexecution_finished) { asdword returnvalue = ctx->getreturndword(); cout << "result of + b is: " << returnvalue << endl; } ctx->release(); engine->release(); return 0; }
all had change how built module since did not post how created module. if copy , paste have worked perfectly
// create our module , add our script section build! asiscriptmodule *mod = engine->getmodule(0, asgm_always_create); r = mod->addscriptsection("script", &script[0], len); assert(r >= 0); r = mod->build(); assert(r >= 0);
also had module pointer use directly when getting function declaration
// function add angelscript asiscriptfunction *func = mod->getfunctionbydecl("int add(int, int)");
the way defined multi function correct , how supposed done. i'm not sure issue guess how created module , maybe loaded wrong name. hope helps!
Comments
Post a Comment