本文章将试着讲述一些好的习惯,它或许可以帮助你更快更容易的找出问题的解决方案(原文: This article will try to explain some good practices, that will help you get going faster and easier find the solution when a problem occurs.)。
注册时一定要检查的返回值,至少在调试模式下配置脚本引擎的时候,一定要检查返回值,(原文:Always check return values for registrations,When configuring the script engine you should always check the return values, at least in debug mode)。【当构建失败时,几乎所有的错误代码都可以用一个像assert( r >= 0 )简单的断言来检查返回值r(原文: All error codes are negative so a simple assert( r >= 0 ) where r is the returned value is sufficient to pinpoint where the configuration failed.)】。
如果一个函数在构建期间失败了,则构建方法总是会失败,并返回一个asINVALID_CONFIGURATION的错误码。 (原文: If a function failed during the configuration, the Build method will always fail with a return code of asINVALID_CONFIGURATION.)。错误总是不可确定,除非你总是检查所有函数调用的错误码(原文:Unless you already verified the error codes for all the configuration calls, it will not be possible to determine what the error was)。
// 用assert检查返回值 即简单又不会打乱你的代码
r = engine->RegisterGlobalFunction("void func()", asFUNCTION(func), asCALL_CDECL);
assert( r >= 0 );
在注册引擎的时候assert()可以被安全的使用,当有一个函数失败时,引擎被设置为构建无效的内部状态。(原文:
assert() can safely be used with engine registrations, since the engine will set the internal state to invalid configuration if a function fails. )。当一个脚本被组建时即使在Release模式下也会失败(原文: Even in release mode the failure is discovered when a script is built.)。
使用消息回调来接收更详细的错误信息(原文:Use the message callback to receive detailed error messages)。
注册函数的返回值,组建和编译功能都只能告诉你什么出错了而不是错误是什么(原文: The return code from the register functions, Build, and CompileFunction, can only tell you that something was wrong, not what it was.)。消息回调就是被用来确定问题之所在的(To help identify the exact problem the message callback should be used.)。脚本库将发送消息 通过普通的文字来说明错误和警告的原因(原文: The script library will then send messages explaining the error or warning in clear text.)。
关于消息回调的更多信息请参考Message callback(原文: See Message callback for more information on the message callback.)
--------- besterChen
译于 2010 年4 月10 日 星期六