Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reference 使用io.read()引用表。Lua 5.1_Reference_Lua_Lua Table - Fatal编程技术网

Reference 使用io.read()引用表。Lua 5.1

Reference 使用io.read()引用表。Lua 5.1,reference,lua,lua-table,Reference,Lua,Lua Table,好吧,这就是我要做的,我几乎可以肯定我不知道用什么短语来找到我要找的,所以我会尽我所能在有限的术语知识下尽可能清楚 我正在使用lua(或至少尝试)为D&D游戏生成赛道/赛段 这就是我所做的,但我不知道如何让一张表引用另一张表。无论我多么努力地研究它或是玩弄它,它都是行不通的 表转储: --Track Tables local raceClass = { 'SS', 'S', 'A', 'B', 'C', 'D', 'N' } local trackLength = { 50, 30, 25,

好吧,这就是我要做的,我几乎可以肯定我不知道用什么短语来找到我要找的,所以我会尽我所能在有限的术语知识下尽可能清楚

我正在使用lua(或至少尝试)为D&D游戏生成赛道/赛段

这就是我所做的,但我不知道如何让一张表引用另一张表。无论我多么努力地研究它或是玩弄它,它都是行不通的

表转储:

--Track Tables
local raceClass =   { 'SS', 'S', 'A', 'B', 'C', 'D', 'N' }
local trackLength = { 50, 30, 25, 20, 15, 10, 5 }
local trackDifficulty = { 3, 3, 3, 2, 2, 1, 1 }
local trackTypes = { 'Straightaway', 'Curve', 'Hill', 'Water', 'Jump' }
所以,这里只是解释一下。首先,我们是比赛中的一流选手。N代表新手,SS代表最难的。接下来,我们有结果轨迹的长度。SS是一条50段的轨道。N为5段轨道。每个竞赛班在赛道的每个部分都有一个难度上限。SS、S和A的上限均为3。D和N的上限为1。然后,轨道的每一部分都被进一步细分为它的类型。这些都是使用这个代码板生成的

--Track Generation
math.randomseed(os.time())
for i = 1, trackLength do
    local trackTypeIndex = math.random(1, #trackTypes)
    local SP = math.random(1, trackDifficulty)  --SP is Stamina Cost for that segment.
    print(tracktypes[trackTypeIndex]..' of SP '..SP)
end
io.read() --So it doesn't just close the window but waits for some user input.
现在它进入了我开始放松自己的部分。我希望DM能够输入选定的比赛类别,并获得生成的赛道列表

--DM Input
print('Race Class? N, D, C, B, A, S, SS")
io.flush()
local classChoice = io.read()
因此,DM放入类选择,让我们使用N。我找不到的是一段代码,它将获取
classChoice
的值,并将其与
raceClass
配对。然后使用该位置选择
轨迹长度
轨迹难度
中的位置,最后运行代码段的剩余部分
轨迹生成
外推适当的变量,并打印结果,获得以下结果:

Straightaway of SP 1
Curve of SP 1
Water of SP 1
Water of SP 1
Jump of SP 1

对于低端新手比赛,只有5段长,最大难度为1。但是随着等级的提高,仍然会产生更长、更难的音轨。我试图尽可能地具体,以尽量减少我在代码方面的经验不足可能造成的任何混乱

我认为您最好使用不同的表格结构:

local raceClass = {
    SS = {50, 3},
    S = {30, 3},
    A = {25, 3},
    B = 20, 2},
    C = {15, 2},
    D = {10, 1},
    N = {5, 1},
}
现在,您可以轻松访问
raceClass
的所有数据。代码如下所示:

print "Race Class? N, D, C, B, A, S, SS"
io.flush()
local classChoice = (io.read "*line"):upper()    -- To convert the input to upper case characters
if not raceClass[classChoice] then
    -- Wrong input was given
end
local SP, Length = raceClass[classChoice][2], raceClass[classChoice][1]