Skip to main content
gamedev.net
🔒 Locked

Lua problem

Started by uPraTe6 Sep 14, 2010 at 5:20 PM 1 replies 800+ views
Original Post
uPraTe6
uPraTe6
Hi,

Let's say i have the following lua function:

setColor(5,{2,58,100})


How can i read all the arguments, and the values in the table in c++ ?
Also, the table is of dynamic length, how could i read the length of table in c++?

this would be my function in c++

int setColor(lua_State *L){     int b = lua_tointeger(L,1); // read first argument     // now read the table and length of table????}
rip-off
rip-off
The length of the table can be obtained using lua_objlen(). Iterating across the table values is done using lua_next, and the documentation includes an example.

If you are using Lua from C++ without any wrapping library, I highly recommend bookmarking the reference, and reading about all the functions first just to give you an idea of what the API can do. You may not understand it all to begin with - it took me a while using it before I figured out the point of upvalues and the lua registry - but its good to have a sense of what is and isn't possible.
dmail
dmail
Quote:
// now read the table and length of table????

Whilst you can use the lua_objlen function on a table, care must be taken and you should understand what the "length" of a table means.
t ={    foo=1}print(#t)

This may not print what you think with this entry. The length operator for a table returns the first none used integer index ( ie the first "hole" in the table).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.