How to get a table from the stack in Lua?

Started by
14 comments, last by trolocsis 19 years, 7 months ago
Hello, I have a table in Lua that must be filled in the C side. for example table { a=0, b=0} Now I wnat to call C_function(table) but I don´t know how to get a table from the stack. There's no a lua_totable or something like this like I do with numbers or strings. Could anybody help?. Note: sorry for the bunch of messages I´m posting, but Lua doc isn´t as good as I would. Thanks in advance, HexDump.
Advertisement
Have the table on the stack, and have the C function use it from the stack. Most datatypes more complex than numbers and strings are like this in Lua: you don't get them into C variables, you work with them in situ, so to speak.
and how do I get the members from it? I mean, in my previous example should I suppose that the vale of a will be at index 1 and b in 2?.


thanks in advance,
HexDump
Let's assume there's a table in the Lua universe:
foo = { ["bar"] = 3 }

To get the number three, here's what you'd do:
lua_getglobal(L, "foo"); // puts the table foo on top of the stacklua_pushstring(L, "bar"); // now the string "bar" is just above the table on the stacklua_gettable(L, -2); // finds the table foo in stack position -2, pops "bar" off the stack, pushes on foo["bar"]int n = lua_tonumber(L, -1); // n should now be equal to 3

There's a pretty good example of this sort of twiddling in the lua manual. Read it over and over until you understand it; it's good conceptual practice.
ok, I understand now. Thnaks alot.

I´m seeing in the manual that I can push a table into the stack too, this is nice because I wanted to do something like:

--IN LUA

TableFromC= C_Function(Table);

Does Lua get the table without any other work?.


Thanks in advance,
HexDump.
Not quite sure what you're asking here. If you want to return a table, just have it as one of the return values (the top n values in the stack, where n is the number the c function returns).
I just want to pass a table to C and get a table from C (return value). and I was asking if the assignment in Lua

Table_returned_from_C = C_Fuction(LuaTable)

handles everything to get after the call in Lua the correct Table in Table_returned_from_C, or should I pop anything from Lua stack?. Sorry for the lot of questions :/, I am a bit confussed with LUA.

Thanks in advance,
HexDump.
Well, in that case, yes, that looks about correct. The table passed from lua ("Table") will be the only value on the stack when C_Function() starts, and the table passed back and stored in the variable "TableFromC" will be the table in the top of the stack when C_Function() returns (assuming it returns one result).
great!. Thanks a lot mate.


HexDump.
Sneftel a little more thing. I will use this system in order to get input from my engine and do anyting I need from the response I receive. Do you think it will be slow?
I mean, it has to be somthing like this:

1. Lua Pushes the player keys as a table to C
2. C reads the table from the stack and puts keys in a Struct
3. From this struct, engine checks what keys were pressed
4. C Create a new table in the stack, fills it with the response
5. Lua reads the returned Tabel.

I´m asking because I think there's a lot of lua stack reads/writes to do this.

Thanks in advance,
HexDump.

This topic is closed to new replies.

Advertisement