Advertisement

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

Started by April 27, 2013 07:59 PM
1 comment, last by wdl7770016 11 years, 6 months ago

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

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.

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

Advertisement

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

This topic is closed to new replies.

Advertisement