Calling method of globally declared variable

Started by
6 comments, last by MrK45 3 months ago

Hello! Thanks to creator for really great project!

Can anyone explain how to call method of globally declared variable?

For example there is script:

class MyClass
{
   int b;
   int myMethod(int a) 
   { 
       return a + b;
   }
};
MyClass asObj1;

I want to call asObj1.myMethod from c++ side.

Let the power of C++ be with you.

Advertisement

This task can be solved by getting pointer to asIScriptObject, but i can't find solution.

Further steps are quite simple:

asIScriptObject* obj = ... ? //How to do this?

asITypeInfo* typeInfo = obj->GetObjectType();
asIScriptFunction* func = typeInfo->GetMethodByDecl("int myMethod(int)");

context->Prepare(func);
context->SetArgDWord(0, 10);
context->Execute();
int result = context->GetReturnDWord();

Let the power of C++ be with you.

I've solved that.

int index = module->GetGlobalVarIndexByName("asObj1");

if (index >= asSUCCESS)
{
   asIScriptObject* obj = (asIScriptObject*)module->GetAddressOfGlobalVar(index);
   
   asITypeInfo* typeInfo = obj->GetObjectType();
   asIScriptFunction* func = typeInfo->GetMethodByDecl("int myMethod(int)");

   context->Prepare(func);
   context->SetArgDWord(0, 10);
   context->Execute();
   
   int result = context->GetReturnDWord();
}

Let the power of C++ be with you.

It seems that my solution is wrong.

Can anyone help?

Let the power of C++ be with you.

The mistake is that I need to set object after preparing context:

context->Prepare(func);
context->SetObject(obj);

Let the power of C++ be with you.

Hi MrK45,

I'm sorry for leaving you talking to yourself here, but it looks like you were able to solve your initial question on your own. 🙂

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks anyway for commenting. I'll be glad if my conversations with myself will be useful for someone else.

Let the power of C++ be with you.

This topic is closed to new replies.

Advertisement