Questions about table.insert()

Alright, I have a few questions regarding table.insert().

index = table.insert( exampletable, exampleobject )

First Question: Will the value of the variable index be the index that exampleobject is placed at in example table? If not, showing me how to do this would be appreciated.
Second Question: If I do this, will it only return the index, or will it also insert exampleobject into exampletable?
Third Question: Can I do this if exampleobject is an entity?

Thanks for all the help, ahead of time.


For your questions…

  1. When you use table.insert, it uses its own generated numerical keys starting from 1 ( other languages start at 0; also some elements within GLua do start at 0 too, but table.insert starts at 1 ).

For each insert, the key is incremented by 1.

  1. If you ran the code, index would be the key assigned ( 1 for the first, 2 for second, etc… ), exampletable is the table it inserts to, exampleobject is the value for the key.

So, if you did: exampletable[ index ] on the next line, it would be exampleobject.

  1. Lua is a language built from tables. Basically, everything can be stored in tables. Tables can be extended to be called like a function, so if you want to use exampletable( ) like that, you’d turn it into a metatable and add a __call function to it. See: for metatable information.

Thank you very much for all the help.