Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 wdl7770016   Members   -  Reputation: 104

Like
0Likes
Like

Posted 27 April 2013 - 01:59 PM

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



Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3361

Like
0Likes
Like

Posted 28 April 2013 - 10:12 AM

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

#3 wdl7770016   Members   -  Reputation: 104

Like
0Likes
Like

Posted 29 April 2013 - 02:48 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS