Jump to content
  • Advertisement
Sign in to follow this  
wdl7770016

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

This topic is 1970 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 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
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.

Share this post


Link to post
Share on other sites

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