【问题标题】:Why are these values being set to the wrong variables?为什么将这些值设置为错误的变量?
【发布时间】:2015-11-27 06:00:08
【问题描述】:

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

此代码用于我在 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() 函数放在设置了产卵机会变量的添加矿石函数中。在那些调试中,它表明变量设置正确,但问题是......在代码中没有其他地方设置机会变量,除非这些值以某种方式“混淆”,但即便如此我检查以确保将值设置为正确的生成类型。

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

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

【问题讨论】:

    标签: lua lua-table roblox


    【解决方案1】:

    Variables in Lua hold references to tables,因此分配不会创建新表。例如:

     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
    

    最后一行将另一个 reference 推送到表中(不是副本),因此下次您使用相同名称调用 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 支持您可能感兴趣的 prototypes with metatables

    【讨论】:

    • 我确实觉得这很有趣,但哪一个对 cpu 的负担更小?我猜是元表
    • 元表在计算上有点费力,它们在查找失败后应用,因此它们可以执行两次查找,但它们的内存效率要高得多。然而,对于这么小的表,这些都无关紧要,干净的可维护代码具有更多更大的价值。
    猜你喜欢
    • 1970-01-01
    • 2019-10-30
    • 2018-06-05
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多