C++調用Lua,調用lua

來源:互聯網
上載者:User

C++調用Lua,調用lua

轉載請註明出處:http://blog.csdn.net/zhy_cheng/article/details/39756423


我使用的cocos2d-x版本是2.3.3,先在一個C++工程中配置好lua的環境。


首先匯入lua項目

1.liblua工程是cocos2d-x-2.2.3\scripting\lua\proj.win32\liblua.vcxproj這個檔案,匯入VS2010工程中

2.包含目錄:在工程的屬性-配置屬性-C/C++-常規-附加元件封裝含目錄中加入$(ProjectDir)..\..\..\scripting\lua\tolua,$(ProjectDir)..\..\..\scripting\lua\lua,

$(ProjectDir)..\..\..\scripting\lua\lua

3.在屬性-配置屬性-連結器-輸入-附加依賴項加入liblua.lib和lua51.lib

好了,lua環境就配置好了

在HelloWorld.cpp中加入標頭檔引用#include "CCLuaEngine.h",#include "script_support\CCScriptSupport.h"

下面開始寫lua了,lua代碼如下

pageName="equip"a={student="zhangsan",age=23}function main()print("leoooooooooooooo")endfunction printSomething(name,age)print("-----------------name is "..name)print("-----------------age is "..age)endfunction returnSomething(name,age)return name,ageend

這個寫的lua代碼,下面看C++部分,首先在AppDelegate.cpp中加入

CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
來設定lua的引擎

然後在HelloWorld.cpp的點擊事件中將lua代碼載入

CCLuaEngine *pEngine = CCLuaEngine::defaultEngine();pEngine->executeString("require \"lua/hello.lua\"");

下面主要說3個知識點

1.調用lua的函數

//調用無參數無傳回值的函數lua_State* L=pEngine->getLuaStack()->getLuaState();//獲得棧頂,並且儲存值int top=lua_gettop(L);lua_getglobal(L,"main");//看看在不在那裡if(!lua_isfunction(L,-1)){CCLog("------------return");}//第一個參數是棧的狀態的指標,第二個參數是參數個數,第三個參數是傳回值的個數,第四個參數是出現錯誤的回呼函數的地址lua_pcall(L,0,0,0);//還原棧lua_settop(L,top);//調用有參數,無傳回值的函數top=lua_gettop(L);lua_getglobal(L,"printSomething");//看看在不在那裡if(!lua_isfunction(L,-1)){CCLog("------------return");}//一一對應啊lua_pushstring(L,"zhycheng");lua_pushnumber(L,24);lua_pcall(L,2,0,0);lua_settop(L,top);//調用有參數,有兩個傳回值的函數top=lua_gettop(L);lua_getglobal(L,"returnSomething");if(!lua_isfunction(L,-1)){CCLog("------------return");}lua_pushstring(L,"new");lua_pushnumber(L,22);lua_pcall(L,2,2,0);if(!lua_isnumber(L,-1)||!lua_isstring(L,-2)){CCLog("return error");}//name在下面int age =(int)lua_tonumber(L,-1);const char* name=lua_tostring(L,-2);CCLog("age is %d",age);CCLog("name %s",name);lua_settop(L,top);

2.獲得lua的一個全域變數的值

//讀取lua的全域變數top=lua_gettop(L);lua_getglobal(L,"pageName");if(!lua_isstring(L,-1)){CCLog("return error");}const char* equipname=lua_tostring(L,-1);CCLog("name is %s",equipname);lua_settop(L,top);

3.訪問全域table的某一個值

//擷取lua表中的某個key的valuetop=lua_gettop(L);lua_getglobal(L,"a");if(!lua_istable(L,-1)){CCLog("error----------------");}lua_pushstring(L,"student");//lua_gettable是一個函數,它首先讓索引值出站,擷取相應表元素的值,然後把這個值入棧//此時student在-2的位子,然這個key出棧,讓他的值入棧lua_gettable(L,-2);if(!lua_isstring(L,-1)){CCLog("error --------------");}const char* studentName = lua_tostring(L, -1);CCLog("studentName is %s",studentName);lua_settop(L,top);//再擷取的時候一定要注意棧的問題//擷取年齡top=lua_gettop(L);lua_getglobal(L,"a");if(!lua_istable(L,-1)){CCLog("error----------------");}lua_pushstring(L,"age");lua_gettable(L,-2);if(!lua_isnumber(L,-1)){CCLog("error-----------------");}int aage=lua_tonumber(L,-1);CCLog("aage is %d",aage);lua_settop(L,top);

好了,就說這麼多,對於棧的操作,可以從-1開始,也可以從1開始,從-1開始的話,就是從棧頂往下,從1開始就是從棧底向上。

下載資源:http://download.csdn.net/detail/zhy_cheng/7999945


C調用lua檔案中函數

你使用的是哪個lua 版本????
我用的是 lua 5.2 ,在 codeblock 下編譯的。
你的描述和原始碼不符合。

printf("%d \n",(int)lua_tonumber(m_pState,1));

應該顯示 0 。
lua堆棧中
index 1 => add函數
index 2 => 1
index 3 => 2
所以那句代碼試圖把lua函數轉換成為數字。應該得到0 。
後面一句應該得到3 。
你說你的結果是 2 0 。應該不會。除非你貼出來的代碼,和實際編譯的代碼不同。
 
C與lua 函數調用

能不能貼出你的關鍵代碼?否則無法知道是你的使用方法錯了還是一些小的細節問題導致功能沒實現。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.