I am having a problem with my code and I can't seem to find the culprit. I have debugged this in so many ways and they all came up as if the problem doesn't exist.
This code is used for a mining game I am making in Roblox that is used to generate the ores.
--[[
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()
below is a list of ores that I am planning to add. some are used in the code above.
--[[
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
--]]
The problem is that in the show_spawn_types()
output, it shows:
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
If you notice the first argument in the add_ore()
function and compare those to their values in the output, the 'grass' spawn type's only connected ore has a spawn chance of 50 when I set it to be 100 in the function. Also, the first connected ore has a spawn chance of 100 when it should be 50.
I've checked this in so many ways, I put print()
function inside the add ore function where the spawn chance variable is set. And in those debugs it shows that the variable is being set correctly, but the thing is... Nowhere else in the code am I setting the chance variable after that unless the values are getting 'mixed up' in some way, but even then I checked to make sure it was setting the value to the right spawn type.
Is there anything I am missing or did I find a bug in lua???
NOTE: I invite you to copy and paste the code into a lua command prompt, edit the code and put in print fuctions to debug parts of the code. It should work correctly.
答案 0 :(得分:4)
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
最后一行将另一个引用推送到表(而不是副本),因此下次使用add_ore
的{{1}}名称调用n
时,更改{{1影响o.chance = ch
的所有引用。
您可以在修改表格之前创建表格OreTypes[n]
的副本。
o
看来你有一个原型,并希望创建该原型的变体。复制是最简单的,但Lua支持您可能感兴趣的prototypes with metatables。