0

我的代码有问题,我似乎找不到罪魁祸首。我已经以多种方式对此进行了调试,并且它们都出现了,就好像问题不存在一样。

此代码用于我在 Roblox 中制作的用于生成矿石的采矿游戏。

--[[
an ore can have one of these different 'spawn types' that is predefined when the game starts which dictates what 'OreTypes' it can have,
and if there are multiple OreTypes under one SpawnType,
then percent values will be given controlling the odds of one OreType being chosen over the other.
]]--
OreSpawnTypes = {} 

-- all the different ore Types
OreTypes = {} 

--[[ 
creates a new ore type with its name, the ore's base value, 
and variables defining how it looks in game. 
ignore 'chance=0' that's just for defining purposes
]]--
function add_ore_type(n,ov,bc,mat,ref,tran) -- 
    OreTypes[n] = {
        BrickColor=bc,
        Material=mat,
        Reflectance=ref,
        Transparency=tran,
        Name=n,
        ore_value=ov,
        chance=0
    }
end

--[[
when used in 'add_spawn_type' to keep track of the previous ore type, 
that was just created by it, to be used in 'add_ore'
to add the different ores do the ore type
--]]
local name = ""

--[[
creates a new spawn type, and sets the name variable to the key
of the ore type listed in the OreSpawnTypes table
--]]
function add_spawn_type(n)
    OreSpawnTypes[n] = {}
    name=n
end

--[[
adds one of the defined ores from 'OreTypes' and with a number form 1-100,
adds it to the previously created spawntype
--]]
function add_ore(ch,n)
    local o = OreTypes[n]
    o.chance = ch

    OreSpawnTypes[name][#OreSpawnTypes[name]+1] = o
end

-- what I use to view the table
function show_spawn_types()

print("------------------------------------------------")

for k,v in pairs(OreSpawnTypes) do
    print(k.."=")
    for k2,v2 in pairs(v) do
        print("                "..k2.."=")
        for k3,v3 in pairs(v2) do
            print("                        "..k3.."="..v3)
        end
    end
end
end

-- this next part is where the ore types are set and the ores are set to each one.

add_ore_type("grass",2,"Earth green","Plastic",0,0)
add_ore_type("ground",4,"Earth green","Slate",0,0)
add_ore_type("rock",8,"Dark stone grey","Slate",0,0)

add_spawn_type("grass")
add_ore(100,"grass") --<NOTE ARGUMENT ONE

add_spawn_type("grass/ground")
add_ore(50,"grass") --<NOTE ARGUMENT ONE
add_ore(50,"ground") --<NOTE ARGUMENT ONE

add_spawn_type("ground")
add_ore(100,"ground") --<NOTE ARGUMENT ONE

add_spawn_type("rock")
add_ore(100,"rock") --<NOTE ARGUMENT ONE

show_spawn_types()

下面是我计划添加的矿石列表。有些在上面的代码中使用。

--[[ 
0= all_grass
1= grass/ground
2= ground 
3= rock
4= rock/stone
5= stone
6= stone/limestone/slate
7= gold deposit
8= silver deposit
9= nickel deposit
10= copper deposit
11= emerald deposit
12= saphire deposit
13= ruby deposit
14= diamond deposit
15= rare mineral deposit
16= super rare ores 1
17= super rare ores 2
18= radioactive ores
--]]

问题是在show_spawn_types()输出中,它显示:

grass=
            1=
                    Transparency=0
                    BrickColor=Earth green
                    Reflectance=0
                    chance=50 --<NOTE THESE VARIABLES
                    Material=Plastic
                    Name=grass
                    ore_value=2
rock=
            1=
                    Transparency=0
                    BrickColor=Dark stone grey
                    Reflectance=0
                    chance=100 --<NOTE THESE VARIABLES
                    Material=Slate
                    Name=rock
                    ore_value=8
ground=
            1=
                    Transparency=0
                    BrickColor=Earth green
                    Reflectance=0
                    chance=100 --<NOTE THESE VARIABLES
                    Material=Slate
                    Name=ground
                    ore_value=4
grass/ground=
            1=
                    Transparency=0
                    BrickColor=Earth green
                    Reflectance=0
                    chance=50 --<NOTE THESE VARIABLES
                    Material=Plastic
                    Name=grass
                    ore_value=2
            2=
                    Transparency=0
                    BrickColor=Earth green
                    Reflectance=0
                    chance=100 --<NOTE THESE VARIABLES
                    Material=Slate
                    Name=ground
                    ore_value=4

如果您注意到函数中的第一个参数add_ore()并将其与输出中的值进行比较,那么当我在函数中将其设置为 100 时,“草”生成类型的唯一连接矿石的生成机会为 50。此外,第一个连接的矿石有 100 的生成机会,而它应该是 50。

我已经以多种方式对此进行了检查,我将print()函数放在设置了产卵机会变量的 add ore 函数中。在那些调试中,它表明变量设置正确,但问题是......在代码中没有其他地方设置机会变量,除非这些值以某种方式“混合”,但即便如此我检查以确保将值设置为正确的生成类型。

有什么我遗漏的还是我在 lua 中发现了一个错误???

注意:我邀请您将代码复制并粘贴到 lua 命令提示符中,编辑代码并放入打印功能以调试部分代码。它应该可以正常工作。

4

1 回答 1

4

Lua 中的变量持有对表的引用,因此赋值不会创建新表。例如:

 a = {chance=100}
 b = a
 b.chance = 50
 print(a.chance) -- outputs 50

问题出现在这个函数中:

function add_ore(ch,n)
    local o = OreTypes[n]
    o.chance = ch
    OreSpawnTypes[name][#OreSpawnTypes[name]+1] = o
end

最后一行推送对表的另一个引用(不是副本),因此下次您add_ore使用相同名称调用n更改o.chance = ch会影响对OreTypes[n].

o您可以在修改之前创建表的副本。

local copy = {}
for k, v in pairs(OreTypes[n]) do copy[k] = v end
copy.chance = ch
OreSpawnTypes[name][#OreSpawnTypes[name]+1] = copy

您似乎有一个原型,并希望创建该原型的变体。复制是最简单的,但 Lua 支持带有您可能感兴趣的元表的原型。

于 2015-09-01T23:12:11.087 回答