lua-table

Passing table with subtable to Lua function from C++

感情迁移 提交于 2020-02-25 05:14:07
问题 I'm trying to pass table with subtable to Lua function as argument from C++. Here's my code that doesn't work but shows what I'm trying to do. class DragInfo{ public: std::vector <std::string> files; glm::vec2 position; }; //a callback that passes DragInfo to a Lua function as a table which has 2 subtables void callDragged(DragInfo &info) { lua_getglobal(L, "dragged"); if (!lua_isfunction(L, -1)) { lua_pop(L, 1); return; } lua_newtable(L); for (size_t i = 0; i < info.files.size(); ++i) { lua

How do I sort a multidimensional table in Lua?

孤街醉人 提交于 2020-01-30 05:43:05
问题 I have a table consisting basically of the following: myTable = {{1, 6.345}, {2, 3.678}, {3, 4.890}} and I'd like to sort the table by the decimal values. So I'd like to the output to be: {{2, 3.678}, {3, 4.890}, {1, 6.345}} If possible, I'd like to use the table.sort() function. Thankyou in advance for the help :-) 回答1: Given that your table is a sequence, you can use table.sort directly. This function accepts a comparison predicate as its second argument, which prescribes the comparison

How to check if two tables(objects) have the same value in Lua

江枫思渺然 提交于 2020-01-22 17:39:11
问题 I wanna check if two tables have the same value in Lua, but didn't find the way. I use the operator == , it seems just to check the same objects, but not the elements in the table. If I have two tables, a={} b={} the value of a==b is false . but if a={} b=a the value of a==b is true . I wonder know if there a way to check two tables having the same elements in Lua. Is there a built-in function like table.equals() to check? 回答1: There is no built-in function for comparing tables by contents.

Am I right to read a nested lua table as argument from C function?

蓝咒 提交于 2020-01-15 19:04:41
问题 I'm going to implement a function with C language and which will be called by Lua script. This function should receive a lua table(which even contains an array) as the argument, so I should read the fields in the table.I try to do like below, but my function is crashing when I run it. Can anyone help my find the problem? /* function findImage(options) imagePath = options.imagePath fuzzy = options.fuzzy ignoreColors = options.ignoreColor; ... end Call Example: findImage { imagePath="/var/image

Am I right to read a nested lua table as argument from C function?

ε祈祈猫儿з 提交于 2020-01-15 18:59:46
问题 I'm going to implement a function with C language and which will be called by Lua script. This function should receive a lua table(which even contains an array) as the argument, so I should read the fields in the table.I try to do like below, but my function is crashing when I run it. Can anyone help my find the problem? /* function findImage(options) imagePath = options.imagePath fuzzy = options.fuzzy ignoreColors = options.ignoreColor; ... end Call Example: findImage { imagePath="/var/image

array as parameter list lua

霸气de小男生 提交于 2020-01-14 10:15:40
问题 Today i was working some with lua,with that "oldesh" for me language,and did find what you can get arguments as array,like soo: function foo(someting,...) local arrayofargs = arg -- code here end And now,i'm intresting.Can that be in "other way"? Can you pass array,not as array,but like param list,Like so: function bar(a1,a2) print(a1+a1) end function foo(someting,...) local arrayofargs = arg bar(arg) end Yes,you cant do that.But can i somehome make someting like that? 回答1: If you're talking

array as parameter list lua

廉价感情. 提交于 2020-01-14 10:15:12
问题 Today i was working some with lua,with that "oldesh" for me language,and did find what you can get arguments as array,like soo: function foo(someting,...) local arrayofargs = arg -- code here end And now,i'm intresting.Can that be in "other way"? Can you pass array,not as array,but like param list,Like so: function bar(a1,a2) print(a1+a1) end function foo(someting,...) local arrayofargs = arg bar(arg) end Yes,you cant do that.But can i somehome make someting like that? 回答1: If you're talking

How to print Lua table using the redefined print function?

南楼画角 提交于 2020-01-14 02:48:26
问题 I learned how to redefine the Lua's print() in C++ from this post. (https://stackoverflow.com/a/4514193/5224286) Here's the redefined print function that prints variables to my host program's console. (through the functions named post.. ) int l_my_print(lua_State *L) { int nargs = lua_gettop(L); for (int i = 1; i <= nargs; ++i) { if (lua_isnil(L, i)) poststring("nil"); else if (lua_isboolean(L, i)) lua_toboolean(L, i) ? poststring("true") : poststring("false"); else if (lua_isnumber(L, i))

Find full string using partial string lua

只愿长相守 提交于 2020-01-06 04:51:08
问题 I tried to find the whole string in a table without writing the full string. Example: maintable = {"SecondString", "FirstString"} c = "First" How would I be able to use string c to find the whole name of FirstString without typing the whole string name? 回答1: You have to search the table: for k,v in pairs(maintable) do if v:match(c) then print(k,v) end end 来源: https://stackoverflow.com/questions/50952236/find-full-string-using-partial-string-lua

How to get returned table from Lua function in C++?

回眸只為那壹抹淺笑 提交于 2020-01-05 06:44:33
问题 I'm trying to figure out how to get the returned table from the Lua function in C++. My code: if (lua_pcall(L, 0, 1, 0)) { std::cout << "ERROR : " << lua_tostring(L, -1) << std::endl; } vector<float> vec; if (lua_istable(L, -1) { //how to copy table to vec? } How can I copy the returned table to vector if the table size is unknown? Thanks! 回答1: I think I found out how to do it using lua_next . lua_getglobal(L, name); if (lua_pcall(L, 0, 1, 0)) { std::cout << "ERROR : " << lua_tostring(L, -1)