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

            聚星亭

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

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

            // 創建一個腳本引擎

            asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

             

            // 設置一個消息回調來接收可讀的錯誤信息

            // 建議在創建一個引擎對象后這樣做一下, 因為如果注冊失敗,本引擎可以發送一些有效信息到消息流中

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

             

            //由于標準的C++中沒有明確的字符串類型,因此,AngelScript也沒有內置的字符串類型.

            //故每個開發人員可以自由注冊它自己的字符串類型

            //但是本引擎SDK為注冊一個字符串類型提供了一個標準的附加項,因此如果你不想注冊,就沒有必要進行注冊  

            RegisterStdString(engine);

             

            // 注冊一個腳本中的函數

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

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

             

                   配置好引擎之后,下一步便是編譯要執行的腳本了(原文:After the engine has been configured, the next step is to compile the scripts that should be executed.)。接下來是一個注冊print函數將“Hello world”寫在標準輸出流中。例如,下面test.as文件中存儲的代碼(原文: 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類是一個裝在文件的附加物

            //如果有必要的話,通過執行一個預處理, 然后告訴引擎組建一個腳本模塊

            CScriptBuilder builder;

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

            if( r < 0 )

            {

              // 產生一個錯誤. 把它列在標準輸出流中以通知腳本作者修正腳本錯誤

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

              return;

            }

                   最后一步是關聯要被調用的函數,并建立上下文來執行它(原文:The last step is to identify the function that is to be called, and set up a context for executing it. )

             

            // 定位到被調用的函數

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

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

            if( funcId < 0 )

            {

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

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

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

              return;

            }

             

            // 創建一個引擎環境, 配置一下, 然后執行

            asIScriptContext *ctx = engine->CreateContext();

            ctx->Prepare(funcId);

            r = ctx->Execute()

            if( r != asEXECUTION_FINISHED )

            {

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

              if( r == asEXECUTION_EXCEPTION )

              {

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

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

              }

            }

             

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

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

            // 清理

            ctx->Release();

            engine->Release();

             

                   Helper functions

                   print 函數是對printf函數很簡單封裝(The print function is implemented as a very simple wrapper on the printf function.)。

            // 打印腳本字符串到標準輸出流

            void print(string &msg)

            {

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

            }

            另見:

                   Message callback, Script builder helper, and string object (STL)
            久久国产精品99国产精| 婷婷伊人久久大香线蕉AV | 日韩人妻无码精品久久久不卡| 久久婷婷色综合一区二区| 精产国品久久一二三产区区别 | 久久久无码精品亚洲日韩蜜臀浪潮 | 欧美大战日韩91综合一区婷婷久久青草 | 综合久久精品色| 国产精品美女久久久m| 91精品国产综合久久四虎久久无码一级| 国产精品成人无码久久久久久 | 中文国产成人精品久久亚洲精品AⅤ无码精品 | 国产高清国内精品福利99久久| 久久丝袜精品中文字幕| 精品久久久久久亚洲精品| 久久精品夜色噜噜亚洲A∨| 国产A三级久久精品| 狠狠色丁香婷婷综合久久来来去 | 国产一级持黄大片99久久| 内射无码专区久久亚洲| 亚洲精品高清久久| 久久午夜羞羞影院免费观看| 亚洲欧美另类日本久久国产真实乱对白| 久久精品国产亚洲AV电影| 色天使久久综合网天天| 欧美激情精品久久久久| 亚洲欧美伊人久久综合一区二区| 四虎国产精品免费久久久 | 国产亚洲色婷婷久久99精品91| 一本色道久久HEZYO无码| 亚洲精品WWW久久久久久| 国产成人精品久久亚洲| 国产精品美女久久久| 亚洲AV日韩AV永久无码久久| 久久人人添人人爽添人人片牛牛| 九九久久精品国产| 26uuu久久五月天| 精品久久久无码中文字幕| 久久久婷婷五月亚洲97号色| 色综合久久久久综合体桃花网| 中文字幕日本人妻久久久免费|