I use this in my work:
asIScriptModule *mod = Stage::getInstance()->getScriptModule()->getBuilder()->GetModule();
asIObjectType *type = Stage::getInstance()->getScriptModule()->getEngine()->GetObjectTypeById(mod->GetTypeIdByDecl(behaviourClassName.c_str()));
//???????????????
if (type == 0 ) {
return 0;
}
//????IBehaviour???
IBehaviour *iBehaviour = Stage::getInstance()->getScriptModule()->getIBehaviour();
//???????????IBehaviour
if (!iBehaviour->isImplementBy(type)) {
return 0;
}
//?????????
string factoryDecl = behaviourClassName + " @" + behaviourClassName + "()";
asIScriptFunction *factoryFunc = type->GetFactoryByDecl("XXXBehaviour @XXXBehaviour()");
if (factoryFunc == 0) {
return 0;
}
asIScriptContext *context = Stage::getInstance()->getScriptModule()->getAvailableContext();
context->Prepare(factoryFunc);
context->Execute();
asIScriptObject *obj = *(asIScriptObject **)context->GetAddressOfReturnValue();
if (obj == 0) {
return 0;
}
//?????????Id(?????????????????????????IBehaviour???
asIScriptFunction *func_getType = type->GetMethodByDecl("uint getType()");
context->Prepare(func_getType);
context->SetObject(obj);
context->Execute();
BEHAVIOUR_TYPE bType = context->GetReturnDWord();
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << bType << endl;
return 0;
I register a IBehaviour interface to the engine
_engine->RegisterInterface("IBehaviour");
_engine->RegisterInterfaceMethod("IBehaviour", "uint getType()");
I instance the XXXBehaviour and want to call it's getType method which is in IBehaviour
this is script code:
class XXXBehaviour : IBehaviour {
XXXBehaviour() {}
uint getType() {
return 110;
}
int getInt() {
print("kfc");
return 120;
}
}
but it will work only when I comment the "context->SetObject(obj);"
and through it work but the bType is still be 0, because in fact it may not work
please help me thanks