Brian T Hannan
Brian T Hannan

Reputation: 4005

How do I write a C++ dictionary to a Lua table?

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.

Upvotes: 0

Views: 711

Answers (1)

Cogwheel
Cogwheel

Reputation: 23217

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

Upvotes: 2

Related Questions