首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环获取动态变量值的Lua泛型

循环获取动态变量值的Lua泛型
EN

Stack Overflow用户
提问于 2022-01-17 22:29:38
回答 1查看 96关注 0票数 0

我有一个具有动态键和值的表,我想用一个泛型的for循环来循环它,但是,由于表值是动态的,我如何获得动态变量的值?我希望这是有意义的。让我用代码演示一下,以便更好地解释。

代码语言:javascript
复制
Local weapons = {
    [GetHashKey('WEAPON_PISTOL')] = { [Strings['supp_name']] = GetHashKey('component_at_pi_supp_02'), [Strings['flash_name']] = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_PISTOL_VARMOD_LUXE') },
    [GetHashKey('WEAPON_PISTOL50')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_PISTOL50_VARMOD_LUXE') },
    [GetHashKey('WEAPON_COMBATPISTOL')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_PI_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = nil },
    [GetHashKey('WEAPON_APPISTOL')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_PI_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_APPISTOL_VARMOD_LUXE') },
    [GetHashKey('WEAPON_HEAVYPISTOL')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_PI_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_HEAVYPISTOL_VARMOD_LUXE') },
    [GetHashKey('WEAPON_VINTAGEPISTOL')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_PI_SUPP'), [Strings['flash_name']]  = nil, [Strings['grip_name']] = nil, skin = nil },
    [GetHashKey('WEAPON_SMG')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_PI_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_SMG_VARMOD_LUXE') },
    [GetHashKey('WEAPON_MICROSMG')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_MICROSMG_VARMOD_LUXE') },
    [GetHashKey('WEAPON_ASSAULTSMG')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = nil, skin = nil },
    [GetHashKey('WEAPON_ASSAULTRIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = GetHashKey('COMPONENT_ASSAULTRIFLE_VARMOD_LUXE') },
    [GetHashKey('WEAPON_CARBINERIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = GetHashKey('COMPONENT_CARBINERIFLE_VARMOD_LUXE') },
    [GetHashKey('WEAPON_ADVANCEDRIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_ADVANCEDRIFLE_VARMOD_LUXE') },
    [GetHashKey('WEAPON_SPECIALCARBINE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_BULLPUPRIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_ASSAULTSHOTGUN')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_HEAVYSHOTGUN')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_BULLPUPSHOTGUN')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_PUMPSHOTGUN')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_SR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = nil, skin = nil },
    [GetHashKey('WEAPON_MARKSMANRIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_SNIPERRIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = nil, [Strings['grip_name']] = nil, skin = nil },
    [GetHashKey('WEAPON_COMBATPDW')] = { [Strings['supp_name']] = nil, [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil }
}

local Strings = {
    ['removed_supp'] = 'You removed your suppressor',
    ['removed_grip'] = 'You removed your weapon grip',
    ['removed_flash'] = 'You removed your weapon flashlight',
    ['no_fit'] = '%s does not fit your current weapon',
    ['removed'] = 'You have removed your %s',
    ['used'] = 'You have used your %s',
    ['webhookMSG'] = '**Spiller Navn:** %s \n**ID:** %s \n**Tried to spawn:** %s',
    ['supp_name'] = 'suppressor',
    ['supp_Rname'] = 'Suppressor',
    ['supp_desc'] = 'Can be used to make your weapon silent when shooting.',
    ['flash_name'] = 'flashlight',
    ['flash_Rname'] = 'Flashlight',
    ['flash_desc'] = 'Can be attached to a weapon to give it a flashlight.',
    ['grip_name'] = 'grip',
    ['grip_Rname'] = 'Grip',
    ['grip_desc'] = 'Can be attached on a weapon to get a better grip.',
    ['choice_use'] = 'Use'
}

正如您在表中看到的,这些值依赖于Strings表中的值。所以,如果我想做一个for循环,并得到正确的值,我将如何做呢?

代码语言:javascript
复制
for k,v in pairs(weapons) do
   if k == GetHashKey('WEAPON_PISTOL') then
      print(v.<what here>) -- how do i get the v.Strings['supp_name']?
   end
end

我希望这是有意义的,如果没有,请在评论中询问。

谢谢你,奥斯西

EN

回答 1

Stack Overflow用户

发布于 2022-01-18 08:34:41

只需使用v[Strings['supp_name']]

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70748245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档