在 Lua 中,如何找出存储对象的键? [英] In Lua, how do you find out the key an object is stored in?

查看:39
本文介绍了在 Lua 中,如何找出存储对象的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你将如何打印()或找出一个对象的索引?

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

例如,如果我在屏幕上生成 20 个随机岩石对象到一个数组中 RockTable = {};

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

像这样RockTable[#RockTable + 1] = rock;

所有 20 块岩石都显示在屏幕上,我如何通过单击它们来找出每块岩石的键或索引?

And all 20 rocks are displayed on screen how would I find out what key or index each rock has by clicking on them?

我正在使用 Corona SDK.

I'm using Corona SDK.

任何帮助将不胜感激.

推荐答案

反转表格:

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.

我发现这个函数非常有用,以至于它进入了我的永久Lua 实用程序"库.

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

这篇关于在 Lua 中,如何找出存储对象的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆