Advertisement
eliasdaler

Using Lua tables as parametrs for C++ functions

Nov 1st, 2014
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. // script.lua
  2. printChoices("Would you like to...", {"YES", "NO", "MAYBE"})
  3.  
  4. // main.cpp
  5. #include <LuaBridge.h>
  6. #include <iostream>
  7. extern "C" {
  8. # include "lua.h"
  9. # include "lauxlib.h"
  10. # include "lualib.h"
  11. }
  12.  
  13. using namespace luabridge;
  14.  
  15. void printChoices(const std::string& s, LuaRef table) {
  16.     std::cout << s << std::endl;
  17.     for (int i = 1; i < table.length() + 1; ++i) {
  18.         LuaRef choice = table[i];
  19.         std::cout << "* " << choice.cast<std::string>() << std::endl;
  20.     }
  21. }
  22.  
  23. int main() {
  24.     lua_State* L = luaL_newstate();
  25.     luaL_openlibs(L);
  26.     getGlobalNamespace(L).addFunction("printChoices", printChoices);
  27.     luaL_dofile(L, "script.lua");
  28.     lua_pcall(L, 0, 0, 0);
  29.     system("pause");
  30. }
  31.  
  32. // output:
  33. // Would you like to...
  34. // * YES
  35. // * NO
  36. // * MAYBE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement