1

I want a function to return a (key-value-)table when called by a Lua-script. Therefore I have to push the table to the stack.
I know how to push an integer to the stack: state->PushInteger(10)
I also know how it works for strings and other numbers, but how would I push a table to the stack and furthermore how would I even create it from the C++ side?

This site usually explains everything pretty well: http://wwhiz.com/LuaPlus/LuaPlus.html but I have a really hard time understanding how LuaPlus works. So in this case it doesn't really help me. :(

It would be really nice if someone could help me out here, I'm literally trying to do this for 3 days now.. :/

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Forivin
  • 14,780
  • 27
  • 106
  • 199
  • There's a more uptodate [one here](https://rawgithub.com/jjensen/luaplus51-all/master/Docs/LuaPlus.html). Look for Table Creation section. – greatwolf Nov 06 '13 at 04:24
  • 3
    @greatwolf wow, that one is only 5 years old, not 9. – Bartek Banachewicz Nov 06 '13 at 08:34
  • This would be the one for the version that I'm using: https://github.com/jjensen/luaplus51-all/blob/64622510896deef01e0a9e2fc744b260d1875c2a/Docs/LuaPlus.html http://jsfiddle.net/dU4Bu/ – Forivin Nov 06 '13 at 14:12

1 Answers1

2

The Pushing a LuaObject onto the Lua Stack section of that page appears to be the answer I would think.

The cases where you would need to push a LuaObject onto the Lua stack are rare.  Nonetheless, the facility is provided through LuaObject's PushStack() function.

LuaObject tableObj(state);
tableObj.AssignNewTable();
tableObj.SetString("Key", "My String");

// It's often good practice to use a LuaAutoBlock here.
tableObj.PushStack();    // Be sure to clean it up when you're done!
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • I read that, but I didn't get it. :/ I tried this: http://pastebin.com/VyBrYzkM but the member PushStack() doesn't exist.. I guess my version of LuaPlus is too old (june 2012). I am too scared to update it because it already took me a week to set up the old version. I really hope there is another way. :( – Forivin Nov 06 '13 at 06:34
  • @Forivin try using `Push()` instead. That's the method name I found after inspecting luaplus's header. – greatwolf Nov 06 '13 at 08:59
  • I tried it but when I call the function (from the Lua-side) my program instantly crashes. http://pastebin.com/t44EjPuz The program exits with "code 3 (0x3)". Is there maybe something else that I'm doing wrong? – Forivin Nov 06 '13 at 14:03
  • I would assume you still need to return (from your C++ function) the number of elements on the lua stack that should be returned to lua. So that function should `return 1` instead of returning 0. – Etan Reisner Nov 06 '13 at 14:22
  • I tried to return 1 and I tried to return "state->GetTop() - top;".. It's still crashing with 0x3. :( – Forivin Nov 06 '13 at 14:57
  • Get a backtrace from the crash then I guess. – Etan Reisner Nov 06 '13 at 15:06
  • I actually got an error message printed into the console (all the time :oops:), but it leads to an LuaPlus header-file. http://pastebin.com/Pmwq8E0p The error says: "Assertion failed: 0, file C:\......\luafunction.h, line 41" Any idea what could be the cause? :/ – Forivin Nov 06 '13 at 17:33
  • That appears to be from calling the Test function itself. Probably because your signature doesn't match. According to your documentation jsfiddle RegisterDirect wants a normal C++ function with a normal signature. Not a lua signature function. – Etan Reisner Nov 06 '13 at 17:46
  • :O Oh, well how would I do it then? I tried to use Register() instead of RegisterDirect() (same error). Then I tried to change the datatype of the function to LuaPlus::LuaObject and return the object directly. (same error...) Then I combined these ideas, but Register() needs a lua_CFunction as second parameter... :/ But that gave me this error as already mentioned. :'( – Forivin Nov 06 '13 at 19:25
  • Well, I guess this answers my question. My problem just seems to be somewhere else. I'm starting a new more specific question on that. Thank you for your help Etan Reisner. – Forivin Nov 07 '13 at 02:18