Jump to content
  • Advertisement
Sign in to follow this  
luasitdown

Some real question about lua

This topic is 4886 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

1. How to put c value as table into to stack? there are lua_pushstring()...but no lua_pushtabel(). maybe I can create new table and add new key/value into it.but this is 2nd question. 2. How to add key/value to a existed table in a stack? I can get value from table in a stack.and set value .but add? 3.Why I need call a lua function by C API/lua_call()... I could call a lua function by lua_dofile in .lua file or by lua_dostring() in string. and put the return value to global variable and get it. why I do exact step geting parameter and function body and putting them in top of stack. call lua_call() to execute such a function.

Share this post


Link to post
Share on other sites
Advertisement
1) The is no C-language type for Lua tables. You can only create new tables.

2) With the table on the stack: push the key, push the value, lua_settable();

3) lua_dofile() internally reads text from your hard drive then calls the compiler to make bytecode then parses the bytecode to make a function and finally calls the function. That is a lot of work and it is very, very slow. dostring() doesn't hit the drive, but it does call the compiler. If you are going to call the function more than once then it is tremendously faster to make the function once and put it in a variable. That way you can call the function over and over without calling the compiler every time!

Share this post


Link to post
Share on other sites
That helps ,especially 3.

but 2 question:
Quote:
With the table on the stack: push the key, push the value, lua_settable();


if do this way, I think the value will replace the value the key points on stack

so I think if the key do not exist, lua_settable() will fail. lua_settable() only replace value and never add new key / value to stack.


Am I right?

Share this post


Link to post
Share on other sites
Quote:
Original post by luasitdown
Am I right?

No. Think of a Lua table as, by default, containing ALL keys, and all values are nil. Then when you set a value, you replace the nil with that value for the given key. To get rid of a value, just set it back to nil.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by luasitdown
Am I right?

No. Think of a Lua table as, by default, containing ALL keys, and all values are nil. Then when you set a value, you replace the nil with that value for the given key. To get rid of a value, just set it back to nil.



Great. but these knowledge can not be found in lua manual.

So how to get these knowledge?

Share this post


Link to post
Share on other sites
Hm? It is in the manual (well, not the all-keys-are-nil stuff, but that's a conceptual thing, not an actual feature of the language). From the manual: "To store a value into a table that resides somewhere in the stack, you push the key and then the value onto the stack, and call void lua_settable (lua_State *L, int index);". That doesn't say anything about the function failing if the key isn't already in the table.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!