根据另一个随机值从表格中选择一个随机值。

我正在将我朋友的武器属性列表转换成生成器,但是我遇到了一些问题。比如,我不想要一把次要自动步枪。是否有一种方法可以根据其他输出值来排除某些值?

local myclasses = {'主武器', '次武器', '重武器'}
local myprimaries = {'自动步枪', '侦察步枪', '脉冲步枪', '狙击步枪', '手枪'}
local mysecondaries = {'霰弹枪', '手枪', '冲锋枪/短管霰弹枪'}
print(myclasses[math.random(#myclasses)])
if '主武器' then
  print(myprimaries[math.random(#myprimaries)])
elseif '次武器' then
  print(mysecondaries[math.random(#mysecondaries)])
end
点赞
用户1009479
用户1009479

问题出在这个条件语句上:

if 'Primary' then

它总会被判断为真,因为任何非 falsenil 的值都会被判断为真。

你需要这样做:

local rand_class = myclasses[math.random(#myclasses)]
print(rand_class)
if rand_class == 'Primary' then
  print( myprimaries[math.random(#myprimaries)] )
elseif rand_class == 'Secondary' then
  print( mysecondaries[math.random(#mysecondaries)] )
end

别忘了要 seed 随机数生成器。

2015-07-13 01:22:26