Sign in to follow this  
jedjed

Using SetArgObject

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this