【发布时间】:2022-01-17 22:29:38
【问题描述】:
我有一个带有动态键和值的表,我想用一个通用的 for 循环遍历它,但是,由于表值是动态的,我怎样才能获得动态变量的值?我希望这有任何意义。让我用代码来展示它以更好地解释。
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'
}
正如您在表中看到的,这些值取决于字符串表中的值。因此,如果我想做一个 for 循环,并获得正确的值,我该怎么做呢?
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
我希望这有任何意义,如果没有,请在 cmets 中询问。
谢谢,奥西
【问题讨论】:
-
只有当表键是有效的 Lua 标识符时才可以使用点进行索引。如果您想使用其他任何内容,请使用方括号。您还可以将变量用作表键。请阅读 Lua 手册