如何在Lua中进行程序化迭代相似的变量?
2014-9-1 10:51:43
收藏:0
阅读:81
评论:1
我有很多变量似乎更容易引用,但我不知道该怎么做;变量名与坐标系条目相关,它们写入的位置也是基于坐标的。我尝试搜索谷歌,阅读了Lua文档,但由于我不确定在寻找什么,所以我认为这妨碍了我的搜索。这是代码节选中变量的外观:
-- 银行1
local Object111 = sItem.getTargetDetails("-4,3,-4")
local Object112 = sItem.getTargetDetails("-5,3,-4")
local Object113 = sItem.getTargetDetails("-6,3,-4")
local Object114 = sItem.getTargetDetails("-7,3,-4")
local Object115 = sItem.getTargetDetails("-8,3,-4")
local Object121 = sItem.getTargetDetails("-4,4,-4")
local Object122 = sItem.getTargetDetails("-5,4,-4")
local Object123 = sItem.getTargetDetails("-6,4,-4")
local Object124 = sItem.getTargetDetails("-7,4,-4")
local Object125 = sItem.getTargetDetails("-8,4,-4")
local Object131 = sItem.getTargetDetails("-4,5,-4")
local Object132 = sItem.getTargetDetails("-5,5,-4")
local Object133 = sItem.getTargetDetails("-6,5,-4")
local Object134 = sItem.getTargetDetails("-7,5,-4")
local Object135 = sItem.getTargetDetails("-8,5,-4")
-- 银行2
local Object211 = sItem.getTargetDetails("-4,3,1")
local Object212 = sItem.getTargetDetails("-5,3,1")
local Object213 = sItem.getTargetDetails("-6,3,1")
local Object214 = sItem.getTargetDetails("-7,3,1")
local Object215 = sItem.getTargetDetails("-8,3,1")
local Object221 = sItem.getTargetDetails("-4,4,1")
local Object222 = sItem.getTargetDetails("-5,4,1")
local Object223 = sItem.getTargetDetails("-6,4,1")
local Object224 = sItem.getTargetDetails("-7,4,1")
local Object225 = sItem.getTargetDetails("-8,4,1")
local Object231 = sItem.getTargetDetails("-4,5,1")
local Object232 = sItem.getTargetDetails("-5,5,1")
local Object233 = sItem.getTargetDetails("-6,5,1")
local Object234 = sItem.getTargetDetails("-7,5,1")
local Object235 = sItem.getTargetDetails("-8,5,1")
然后,我会调用这些变量中的每一个在一个函数中,其中屏幕上的位置是名称中的数字的一个函数,前两个数字与x坐标相关,最后一个数字与y坐标相关:
mon.setCursorPos(4,4)
mon.write(Object111.StoredPercentage)
mon.setCursorPos(10,4)
mon.write(Object112.StoredPercentage)
mon.setCursorPos(16,4)
mon.write(Object113.StoredPercentage)
...
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
...
mon.setCursorPos(36,4)
mon.write(Object211.StoredPercentage)
mon.setCursorPos(42,4)
mon.write(Object212.StoredPercentage)
mon.setCursorPos(48,4)
mon.write(Object213.StoredPercentage)
等等....
我可以看到这应该能够动态生成,但我不知道从哪里开始而不手动调用;我希望我的代码更干净。在这个阶段,我真的只需要学会钓鱼;如果有人能把我指向解释我正在尝试的东西的文档,我会非常感激。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
以下是我为那些努力实现相同目标的人提供的最终解决方案:
local Banks = 2 local Rows = 3 local Columns = 5 function Objectstatus(bank,row,column) Id = bank .. row .. column worldx = "-" .. column+3 worldy = row+2 if bank == 1 then worldz = -4; end if bank == 2 then worldz = 1; end Object = {} Object[Id] = s.getTargetDetails(worldx..","..worldy..","..worldz) xcursor = (column*7)+3 if bank == 1 then ycursor = (row*4)+8 end if bank == 2 then ycursor = (row*4)+24 end mon.setCursorPos(xcursor,ycursor) powertext(Object[Id].StoredPercentage) -- 基于结果设置文本设置的函数 mon.write(Object[Id].StoredPercentage) -- 实际写入结果 end for BankTemp = 1, Banks do for ColumnTemp = 1, Columns do for RowTemp = 1, Rows do Objectstatus(BankTemp,RowTemp,ColumnTemp) end end end