Jump to content
  • Advertisement
Sign in to follow this  
Guy Meh

Removing element from Lua array in C(++)

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

I need a way to remove the element of a Lua array (table) at a certain index from C. I've read about how to access and set array elements (lua_rawgeti and lua_rawseti) but I can't tell how to use these to remove an element from an array completely. What do I do?

Share this post


Link to post
Share on other sites
Advertisement
Well, I tried this:

lua_pushnil(L);
lua_rawseti(L, 1, n);

Where 1 is the position of my table on the stack. However, not only does this remove the element at n, it also removes every single element that goes after n. Also, the table still reports its original size when I use #array.

Please tell me I don't have to manually reposition every array element.

Share this post


Link to post
Share on other sites
Quote:
Original post by Guy Meh
Where 1 is the position of my table on the stack. However, not only does this remove the element at n, it also removes every single element that goes after n.
No, it doesn't. It does, however, leave a hole in otherwise consecutive integer keys, and that will make the rest of the table appear to go away if you're iterating using ipairs (see docs). If you are interested in not leaving a hole, the easiest method (as with arrays in every other language) is to move the last element to the removed location.

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!