In Lua, how do you find out the key an object is stored in?

后端 未结 5 778
失恋的感觉
失恋的感觉 2020-12-21 07:59

How would you print() out or find out the index of an object?

For example, if I spawned 20 random rock objects on screen into an array RockTable = {};<

5条回答
  •  温柔的废话
    2020-12-21 08:54

    Invert the table:

    function table_invert(t)
      local u = { }
      for k, v in pairs(t) do u[v] = k end
      return u
    end
    

    You can then use the inverted table to find the index.

    I find this function so useful that it goes into my permanent "Lua utilities" libraries.

提交回复
热议问题