如何在C 中整合Lua指令碼

2021-05-21 18:52:54 字數 2437 閱讀 7417

如何在c++中整合lua指令碼

logger()

virtual ~logger()

logger(int n)

logger(logger* logger)

int setvalue(int val)

int getvalue()

public:

int v;

};// 匯入到lua指令碼:

luaclass(state)

.create("logger") // 定義建構函式 logger::logger()

.create("logger2")   // 定義建構函式 logger::logger(int)

.create("logger3") // 定義建構函式 logger::logger(logger*)

.destroy("free")   // 定義析構函式 logger::~logger()

.destroy("__gc")   // 定義析構函式 logger::~logger()

.def("lm", &logger::logmember) // 定義成員函式 logger::logmember(const char*)

.def("setvalue", &logger::setvalue)

.def("getvalue", &logger::getvalue);

// 在lua中使用logger類(1):

state->dostring(

"l = logger();"   // 呼叫建構函式 logger::logger()

"l.lm('hello world 1');" // 呼叫成員函式 logger::logmember(const char*)

"l.free();"   // 呼叫析構函式 logger::~logger()

);// 在lua中使用logger類(2):

state->dostring(

"m = logger(10);" // 呼叫建構函式 logger::logger(int)

"m.lm('hello world 2');" // 呼叫成員函式 logger::logmember(const char*)

"n = logger(m);" // 呼叫建構函式 logger::logger(logger*)

"n.lm('hello world 3');" // 呼叫成員函式 logger::logmember(const char*)

"m.setvalue(11);"

"print(m.getvalue());"

"m,n = nil, nil;" // m,n 將由lua的垃极**來呼叫析構函式

);4. 將一組c函式歸類到lua模組

//同上面一樣,我採用luaplushelper.h來簡化:

luamodule(state, "mymodule")

.def("add", add)

.def("add2", test, add);

state->dostring(

"print(mymodule.add(3,4));"

"print(mymodule.add2(3,4));"

);5. 使用lua的table資料型別

// 在lua中建立table

luaobject table = state->getglobals().createtable("mytable");

table.setinteger("m", 10);

table.setnumber("f", 1.99);

table.setstring("s", "hello world");

table.setwstring("ch", l"你好");

table.setstring(1, "what");

// 相當於lua中的:

// mytable =

// 也可以使用table作為key和value:

state->getglobals().createtable("nexttable")

.setstring(table, "hello")

.setobject("obj", table);

// 相當於lua中的:

// nexttable =

//獲得table的內容:

luaobject t2 = state->getglobals("mytable");

int m = t2.getbyname("m").getinteger();

luaobject t3 = state->getglobals("nexttable");

std::string str = t3.getbyobject(t2).getstring();

6 遍歷table

luastateowner state;

state.dostring( "mytable = " );

如何在C 中整合Lua指令碼 LuaPlus篇

如何在c 中整合lua指令碼 luaplus篇 去年我作了乙個lua指令碼的c 包裝,有許多朋友感興趣,並嘗試使用,我感到受寵若驚。事實上,我作的包裝,學習的目的比較強,它還是有許多缺陷的。為了讓朋友們少走彎路,我推薦使用luaplus作為c 的包裝。luaplus是lua的c 增強,也就是說,lu...

如何在C 中整合Lua指令碼 LuaPlus篇

去年我作了乙個lua指令碼的c 包裝,有許多朋友感興趣,並嘗試使用,我感到受寵若驚。事實上,我作的包裝,學習的目的比較強,它還是有許多缺陷的。為了讓朋友們少走彎路,我推薦使用luaplus作為c 的包裝。我將在下面說明,如何使用luaplus,以及如何更方便的讓luaplus與c 的類合作無間。1....

在vs中整合lua開發環境

1.在vs中選擇工具 外部工具,新增乙個外部工具 命令 lua直譯器,即安裝lua目錄中的lua.exe 引數 要編譯的lua原始檔位置,格式 f test itemfilename itemext 意思就是編譯f test 下的所有lua檔案,其中 itemfilename 表示檔名,itemex...