tags:

views:

94

answers:

1

I have some C++ code that interacts with some Lua code. Basically, I want to be able to get some results (in the form of a dictionary a.k.a a collection of items) from a query message and then push them out to Lua as a table so that I can easily access all the results from Lua by using the dictionary.

Right now, I just get one specific value that I want and send that out but it would be nice to send all of them out and not have to request a specific one.

+2  A: 

Assuming you're familiar with the Lua API in general, here's the basic process:

  1. Create a new table on the stack (lua_newtable)
  2. For each item in the dictionary
    1. Push the value onto the stack
    2. Push the key onto the stack
    3. Call lua_settable
Cogwheel - Matthew Orlando
OK, this makes sense. Can you give me an example of how to easily get the values from the table in LUA code?
Brian T Hannan
First things first: http://www.lua.org/about.html#name ;) That out of the way, how is your code being triggered? Do you have an existing table in Lua and you call out to C?
Cogwheel - Matthew Orlando
Wow, that's funny that they actually explicitly say not to write it that way. I guess I thought it was an acronym until MrXexxed brought it up.
Brian T Hannan
I don't have an existing table in Lua, but I'll create one.
Brian T Hannan
In Lua code:`for k, v in pairs(MyCFunction(anyargs)) do -- k is key, v is value DoSomething(k, v)end`
DeadMG
@DeadMG ah yeah, I read his follow-up question wrong...
Cogwheel - Matthew Orlando