简体   繁体   English

如何在C中获取lua参数?

[英]How to get lua parameters in c?

I am using Visual C++ 2012 and trying to write ac extension for Lua. 我正在使用Visual C ++ 2012,并尝试为Lua编写交流扩展。 Currently I am design a function prototype: 目前,我正在设计一个函数原型:

lib.myfunc(number, {a=1,b=2,c=3},{d=4,e=5,...})

There are 3 parameters for 'myfunc' function, the 1st parameter is a number of integer, the 2nd and 3rd parameters are table type, and I need to access the values by keys(the keys are 'a','b','c'...) “ myfunc”函数有3个参数,第一个参数是整数,第二个和第三个参数是表类型,我需要通过键访问值(键是“ a”,“ b”,“ C'...)

I've read the lua manual and googled for many tutorials but I still cannot get it work. 我已经阅读了lua手册,并在Google上搜索了许多教程,但仍然无法正常工作。 I want a sample C code doing this job, thank you~ 我想要一个示例C代码来完成这项工作,谢谢〜

I don't really know luabind, so I'm not sure, if they offer any of their own facilities to do that, but in Lua you'd do that like this: 我不是很了解luabind,所以我不确定他们是否提供自己的设施,但是在Lua中,您会这样做:

int myLuaFunc(lua_State *L)
{
  int arg1 = luaL_toint(L, 1);
  luaL_checktype(L, 2, LUA_TTABLE);    //Throws an error, if it's not a table
  luaL_checktype(L, 3, LUA_TTABLE);

  //Get values for the first table and push it on the stack
  lua_getfield(L, 2, "keyname");   //Or use lua_gettable
  //Assuming it's a string, get it
  const char *tmpstr = lua_tostring(L, -1);

  //..... Similariliy for all the other keys
}

You might want to refer to Lua Reference Manual for description of the functions I used. 您可能需要参考《 Lua参考手册》以了解我使用的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM