Using SetArgObject

Started by
2 comments, last by jedjed 14 years, 1 month ago
I'm having a hard time passing an object to a script function. Here is my test code: class TestObject { public: int blah; }; r = mEngine->RegisterObjectType("Test", sizeof(TestObject), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS ); int funID=mModule->GetFunctionIdByDecl("void testFun(Test)"); TestObject test; mContext->Prepare(funID); mContext->SetArgObject(0, &test); int r = mContext->Execute(); in script: void testFun(Test obj) { } Execute() always returns -1. What am I doing wrong?
Advertisement
Is the function id valid?

Also I'm not sure about that but when building code maybe you should already have registered your object type?
This works for me:
#include "angelscript.h"#include "scriptstdstring.h"#include <iostream>#include <cassert>#include <cstdio>struct Test {  int data;};void MessageCallback(const asSMessageInfo *msg, void *param){	const char *type = "ERR ";	if( msg->type == asMSGTYPE_WARNING ) 		type = "WARN";	else if( msg->type == asMSGTYPE_INFORMATION ) 		type = "INFO";	printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);}void print(std::string & str) {	std::cout << str;}int main(int, char **) {  asIScriptEngine * engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);  engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);  RegisterStdString(engine);    int r;    r = engine->RegisterObjectType("Test", sizeof(Test), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CDA); assert(r >= 0);  r = engine->RegisterObjectProperty("Test", "int data", offsetof(Test,data)); assert( r >= 0 );  r = engine->RegisterGlobalFunction("void print(string & in)", asFUNCTION(print), asCALL_CDECL); assert( r >= 0 );    const char script[] =    "void foo(Test t) {\n"    "  print(t.data + \"\\n\");\n"    "}\n"  ;      asIScriptModule * mod = engine->GetModule(0, asGM_ALWAYS_CREATE);  r = mod->AddScriptSection("script", script, sizeof(script) - 1); assert(r >= 0);  r = mod->Build(); assert(r >= 0);  int func_id = mod->GetFunctionIdByDecl("void foo(Test)"); assert(func_id >= 0);  asIScriptContext * ctx = engine->CreateContext();  Test test = { 5 };  r = ctx->Prepare(func_id); assert(r >= 0);  r = ctx->SetArgObject(0, &test); assert(r >= 0);  r = ctx->Execute(); assert(r >= 0);  test.data = 128;  r = ctx->Prepare(func_id); assert(r >= 0);  r = ctx->SetArgObject(0, &test); assert(r >= 0);  r = ctx->Execute(); assert(r >= 0);  ctx->Release();  engine->Release();}
ah thank you. it was a silly typo on my part but knowing the working example let me stop trying all the permutations of flags and whatnot.

This topic is closed to new replies.

Advertisement