Sign in to follow this  
wdl7770016

why I can't call a script member function successfully?

Recommended Posts

wdl7770016    104

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

Share this post


Link to post
Share on other sites
WitchLord    4677

The problem is that the object you created is destroyed before you manage to call the class method. 

 

After you get the object pointer from the context with GetAddressOfReturnValue() you must increase the ref counter with a call to asIScriptObject::AddRef() because the context will release its reference when the context is reused with the next call to Prepare().

 

Don't forget to call Release() on the object again when you will no longer use the object. Otherwise you'll get memory leaks.

Share this post


Link to post
Share on other sites
wdl7770016    104

The problem is that the object you created is destroyed before you manage to call the class method. 

 

After you get the object pointer from the context with GetAddressOfReturnValue() you must increase the ref counter with a call to asIScriptObject::AddRef() because the context will release its reference when the context is reused with the next call to Prepare().

 

Don't forget to call Release() on the object again when you will no longer use the object. Otherwise you'll get memory leaks.

 


thanks  I have resolve it now

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