• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            聚星亭

            吾笨笨且懶散兮 急須改之而奮進(jìn)
            posts - 74, comments - 166, trackbacks - 0, articles - 0
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

                   AngelScript作為一個(gè)嵌入式的腳本庫(kù)不支持獨(dú)立運(yùn)作(原文: Being an embedded scripting library there isn't much that AngelScript allows the scripts to do by themselves,)       因此,應(yīng)用程序必須先注冊(cè)一個(gè)接口讓腳本與應(yīng)用程序交互(原文:so the first thing the application must do is to register the interface that the script will have to interact with the application.)。該接口可以由函數(shù)、變量、甚至是一個(gè)完整的類組成(原文:The interface may consist of functions, variables, and even complete classes.)。

            // 創(chuàng)建一個(gè)腳本引擎

            asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

             

            // 設(shè)置一個(gè)消息回調(diào)來(lái)接收可讀的錯(cuò)誤信息

            // 建議在創(chuàng)建一個(gè)引擎對(duì)象后這樣做一下, 因?yàn)槿绻?cè)失敗,本引擎可以發(fā)送一些有效信息到消息流中

            r = engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL); assert( r >= 0 );

             

            //由于標(biāo)準(zhǔn)的C++中沒有明確的字符串類型,因此,AngelScript也沒有內(nèi)置的字符串類型.

            //故每個(gè)開發(fā)人員可以自由注冊(cè)它自己的字符串類型

            //但是本引擎SDK為注冊(cè)一個(gè)字符串類型提供了一個(gè)標(biāo)準(zhǔn)的附加項(xiàng),因此如果你不想注冊(cè),就沒有必要進(jìn)行注冊(cè)  

            RegisterStdString(engine);

             

            // 注冊(cè)一個(gè)腳本中的函數(shù)

            r = engine->RegisterGlobalFunction("void print(const string &in)", \

                                            asFUNCTION(print), asCALL_CDECL); assert( r >= 0 );

             

                   配置好引擎之后,下一步便是編譯要執(zhí)行的腳本了(原文:After the engine has been configured, the next step is to compile the scripts that should be executed.)。接下來(lái)是一個(gè)注冊(cè)print函數(shù)將“Hello world”寫在標(biāo)準(zhǔn)輸出流中。例如,下面test.as文件中存儲(chǔ)的代碼(原文: The following is our script that will call the registered print function to write Hello world on the standard output stream. Let's say it's stored in the file test.as.:

            void main()

            {

              print("Hello world\n");

            }

             

                   下面的代碼是加載腳本文件并編譯(Here's the code for loading the script file and compiling it. ):

            // CScriptBuilder類是一個(gè)裝在文件的附加物

            //如果有必要的話,通過(guò)執(zhí)行一個(gè)預(yù)處理, 然后告訴引擎組建一個(gè)腳本模塊

            CScriptBuilder builder;

            r = builder.BuildScriptFromFile(engine, "MyModule", "test.as");

            if( r < 0 )

            {

              // 產(chǎn)生一個(gè)錯(cuò)誤. 把它列在標(biāo)準(zhǔn)輸出流中以通知腳本作者修正腳本錯(cuò)誤

              printf("Please correct the errors in the script and try again.\n");

              return;

            }

                   最后一步是關(guān)聯(lián)要被調(diào)用的函數(shù),并建立上下文來(lái)執(zhí)行它(原文:The last step is to identify the function that is to be called, and set up a context for executing it. )

             

            // 定位到被調(diào)用的函數(shù)

            asIScriptModule *mod = engine->GetModule("MyModule");

            int funcId = mod->GetFunctionIdByDecl("void main()");

            if( funcId < 0 )

            {

              // 找不到這個(gè)函數(shù)(The function couldn't be found.)

              // 通知腳本作者在腳本中包含指定的函數(shù).

              printf("The script must have the function 'void main()'. Please add it and try again.\n");

              return;

            }

             

            // 創(chuàng)建一個(gè)引擎環(huán)境, 配置一下, 然后執(zhí)行

            asIScriptContext *ctx = engine->CreateContext();

            ctx->Prepare(funcId);

            r = ctx->Execute()

            if( r != asEXECUTION_FINISHED )

            {

              // The execution didn't complete as expected. 確定發(fā)生了什么.

              if( r == asEXECUTION_EXCEPTION )

              {

                // 發(fā)生一個(gè)異常, 讓腳本作者知道發(fā)生了什么,怎樣才可以修正它.

                printf("An exception '%s' occurred. Please correct the code and try again.\n", ctx->GetExceptionString());

              }

            }

             

                   上面試最基本的異常處理(原文:The exception handling above is very basic.)。如果需要,該應(yīng)用程序也可以獲得關(guān)于行號(hào)、函數(shù)、調(diào)用堆棧信息、以及本地和全局變量(原文:The application may also obtain information about line number, function, call stack, and even values of local and global variables if wanted. )。

                   在虛擬機(jī)執(zhí)行完之后,不要忘了清理一下(原文:Don't forget to clean up after you're done with the engine.

            // 清理

            ctx->Release();

            engine->Release();

             

                   Helper functions

                   print 函數(shù)是對(duì)printf函數(shù)很簡(jiǎn)單封裝(The print function is implemented as a very simple wrapper on the printf function.)。

            // 打印腳本字符串到標(biāo)準(zhǔn)輸出流

            void print(string &msg)

            {

              printf("%s", msg.c_str());

            }

            另見:

                   Message callback, Script builder helper, and string object (STL)
            亚洲精品乱码久久久久久久久久久久 | 97久久国产亚洲精品超碰热| 久久香蕉一级毛片| 久久这里只有精品首页| 国产综合免费精品久久久| 色偷偷88888欧美精品久久久| 人妻丰满?V无码久久不卡| 久久午夜电影网| 狠狠色丁香久久婷婷综| 无码人妻久久一区二区三区 | 国产精品九九久久精品女同亚洲欧美日韩综合区| 久久这里有精品视频| 亚洲伊人久久大香线蕉苏妲己| 嫩草伊人久久精品少妇AV| 亚洲精品成人久久久| 久久亚洲国产成人精品无码区| 日本精品久久久久中文字幕8| 久久97精品久久久久久久不卡| 国产亚洲精久久久久久无码| 久久久国产乱子伦精品作者| 久久国产热精品波多野结衣AV| 日韩人妻无码精品久久免费一| 无码伊人66久久大杳蕉网站谷歌 | 久久最新免费视频| 欧美日韩精品久久久久| 欧美激情精品久久久久久久| 一本久久综合亚洲鲁鲁五月天| 欧美久久一级内射wwwwww.| 日本久久中文字幕| 97精品伊人久久久大香线蕉| 伊人久久综合无码成人网| 久久久免费精品re6| 久久国产乱子精品免费女| 国产ww久久久久久久久久| 久久99精品国产麻豆不卡| 亚洲人成无码网站久久99热国产| 久久亚洲国产成人影院| 久久大香香蕉国产| 久久久受www免费人成| 伊人久久大香线焦AV综合影院| 国产麻豆精品久久一二三|