- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 ComputerCraft 中为一只乌龟创建一个程序。该程序将使 turtle control 成为我在游戏中的元素的存储仓库。它会检查我放入的元素,然后它会找出箱子的位置,去那里,然后把它倒进去。我把每个箱子的位置存储在一个表中。例如:
cobblestone = {2,0,1}
这告诉乌龟鹅卵石箱存储在 x=2 y=0 和 z=1 的位置。为了让乌龟知道它需要存储什么,它会:
itemDetails = turtle.getItemDetail()
name = string.gsub(itemDetails.name, "minecraft:", "")
这让乌龟获得元素的详细信息。然后它将一个变量设置为项目名称减去 minecraft: 在它的开头(我不能有一个名为“minecraft:cobblestone”的变量)。我不知道如何使用这个变量来调用表并找到它的位置让乌龟去它。如果有人可以提供帮助,我将不胜感激。提前致谢!另外,请注意,代码仍然是为调试和测试目的而设置的。设置是一只乌龟,前面是输入箱,右边是燃料箱,后面是仓库。
我试过:
cobblestone = {2,0,1}
--Putting a piece of cobblestone in--
itemDetails = turtle.getItemDetail()
--Returning name of "minecraft:cobblestone"--
name = string.gsub(itemDetails.name, "minecraft:", "")
print name[1]
到目前为止,这还没有奏效。
pos = {0,0,0}
looking = 0
cobblestone = {2,0,1}
function fuel()
if turtle.getFuelLevel() < 20 then
turtle.select(16)
turtle.refuel(1)
end
end
function left()
turtle.turnLeft()
looking = looking - 1
if looking < 0 then
looking = 3
end
print(looking)
end
function right()
turtle.turnRight()
looking = looking + 1
if looking > 3 then
looking = 0
end
print(looking)
end
function forward()
fuel()
if turtle.forward() then
if looking == 0 then
pos[1] = pos[1] - 1
elseif looking == 1 then
pos[3] = pos[3] - 1
elseif looking == 2 then
pos[1] = pos[1] + 1
elseif looking == 3 then
pos[3] = pos[3] + 1
else
print("wot")
end
end
end
function up()
fuel()
turtle.up()
pos[2] = pos[2] + 1
end
function down()
fuel()
turtle.down()
pos[2] = pos[2] - 1
end
function goHome()
while pos[3] > 0 do
while looking > 1 do
left()
end
forward()
end
while pos[2] > 0 do
down()
end
while pos[1] > 0 do
while looking > 0 do
left()
end
forward()
end
end
while true do
turtle.select(1)
while not turtle.suck() do
sleep(1)
end
itemDetails = turtle.getItemDetail()
name = string.gsub(itemDetails.name, "minecraft:", "")
print(name)
while looking < 2 or looking > 2 do
left()
end
for i = pos[1],name[1]-1 do
forward()
end
while looking > 3 or looking < 3 do
right()
end
for i = pos[3],name[3]-1 do
forward()
end
for i = pos[2],name[2]-1 do
up()
end
while looking < 2 or looking > 2 do
left()
end
turtle.select(1)
turtle.drop()
goHome()
end
我希望能够做到:
cobblestone = {2,0,1}
--Putting a piece of cobblestone in--
itemDetails = turtle.getItemDetail()
--Returning name of "minecraft:cobblestone"--
name = string.gsub(itemDetails.name, "minecraft:", "")
print name[1]
然后让乌龟打印同样的东西 鹅卵石[1]这将是 2。但是,它返回 nil。
最佳答案
目前,您的cobblestone
是全局的,这对这个例子来说有什么不好。首先,像这样存储所有元素:
local myelems = {
cobblestone = {2,0,1};
grass = {3,1,2};
}
等等。尽可能使用 local
变量,这是编程中的好习惯。
那么让我们看看您的代码:
-- Returning name of "minecraft:cobblestone"
local name = string.gsub(itemDetails.name, "minecraft:", "")
print(name[1])
它有什么作用? (我稍微改了一下)
起初,string.gsub
在这里看起来有点矫枉过正,改用 string.match
:
local name = itemDetails.name:match("^minecraft:(.+)$")
此处 itemDetails.name:match
与使用 itemDetails.name
作为第一个参数调用 string.match
相同。它可用于所有 string
函数。
所以这里的name
是string变量。不同语言的索引字符串可能有不同的结果。在 lua 中,实际上提供了对所有 string
函数的访问,以便更轻松地调用它们(如上所述)。没有string[1]
这样的函数,所以你得到nil
。但是,现在我们有了表 myelems
,其中的键是字符串,值是您的表。
完整代码:
-- Table for all elements
local myelems = {
cobblestone = {2,0,1};
grass = {3,1,2};
}
-- Putting a piece of cobblestone in
local itemDetails = turtle.getItemDetail()
--Returning name of "minecraft:cobblestone"
local name = itemDetails.name:match("^minecraft:(.+)$")
-- Get our table
local elem = myelems[name]
if elem then
print(elem[1], elem[2], elem[3])
end
关于lua - 如何从 Lua 中的变量调用表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53901165/
我是一名优秀的程序员,十分优秀!