Table parameter

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

-- Functions with table
-- parameters
function contact(t)
for k,v in t do
print(k .. "=" .. v)
end
end
------------------------------
contact {
name = "Game Developer",
email = "hack@ogdev.net",
url = "http://www.ogdev.net",
quote = [[
There are
10 types of people
who can understand binary.]]
}
------------------------------

lua_State* L;

int x, int y ;

int sum;

/* the function name */

/* the first argument */
lua_pushnumber(L, x);

/* the second argument */
lua_pushnumber(L, y);

/* call the function with 2
arguments, return 1 result */
lua_call(L, 2, 1);

/* get the result */
sum = (int)lua_tonumber(L, -1);
lua_pop(L, 1);


I want to call the contact in C/C++. I know call the function in C/C++ with simple function. but how to do with table as parameter? [Edited by - luasitdown on July 25, 2005 8:10:27 PM]

Share on other sites
I just want to implement a simple function with lua.

I write some data on text(.lua file)

and load the the required data and use it.

"Game Developer",
"hack@ogdev.net",
"http://www.ogdev.net",
There are
10 types of people
who can understand binary

how to do it?

Share on other sites
Well, first lua_getglobal(L, "add") should be renamed to lua_getglobal(L, "contact") in your case, because the function "add" does not exist in your code.

Second, to send the table as an argument to the function call, you should try to instead of pushing some argument to the stack, get the table with lua_getglobal(...). This should push the table to the top of the stack, where lua_call/lua_pcall expects the function's arguments...

Hope this helps,
ZMaster

Share on other sites
this help.
but could you tell me how table store in stack??

I guess every entry in table will push stack separately as a element.when said index points to table ,the index points to first entry of table in stack.

Share on other sites
If you use lua_getglobal to pop the table, no members will acutally be pushed onto the stack. Only the table itself will be ontop. As far as I can see, this is all you need in your example.

Well, as how lua handles tables in detail, I have to admit that I don't know exactly. Maybe someone with more detailed knowledge about lua should answer to this question. But: What I suppose is, that tables are internally handled as pointers that point to an array of {key, value} structures. In lua itself a table is a whole new data type and is handled as such.
But as I mentioned, wait for someone else to reply on this...

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5

• 13
• 14
• 9
• 9
• 9
• Forum Statistics

• Total Topics
632926
• Total Posts
3009239
• Who's Online (See full list)

There are no registered users currently online

×