Jump to content
  • Advertisement
Sign in to follow this  
jedjed

Using SetArgObject

This topic is 3168 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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?

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
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!