Jump to content
  • Advertisement
Sign in to follow this  
luasitdown

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.

If you intended to correct an error in the post then please contact us.

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 */
lua_getglobal(L, "add");

/* 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!