Picking a random entry out of a table/list? - WoWInterface
12-17-20, 09:30 AM   #1
Krainz
A Wyrmkin Dreamwalker
Join Date: Oct 2016
Posts: 57
Picking a random entry out of a table/list?

I have this code in a weakaura that plays music during combat when bloodlust is cast:

Lua Code:
`local G = 0G = GetCVar("Sound_EnableMusic")SetCVar("Sound_EnableMusic", 0)  if UnitExists("boss1") then        local bossHP = UnitHealth("boss1")/UnitHealthMax("boss1")        if bossHP > 0.8 then        local R = 0        R=math.random(1,38)                        if (R == 1) then PlaySoundFile("sound\\01-Rising-Dragon.ogg", "Master") elseif (R == 2) then PlaySoundFile("sound\\02-Cometh.ogg", "Master") elseif (R == 3) then PlaySoundFile("sound\\03-Gothams-Reckoning.ogg", "Master") elseif (R == 4) then PlaySoundFile("sound\\35-Hunt-Or-Be-Hunted.ogg", "Master") elseif (R == 5) then PlaySoundFile("sound\\06-the-battle-is-to-the-strong-2.ogg", "Ambience") elseif(R == 6) then PlaySoundFile("sound\\06-the-battle-is-to-the-strong.ogg", "Master") elseif (R == 7) then PlaySoundFile("sound\\FFXV-1.ogg", "Master") elseif (R == 8) then PlaySoundFile("sound\\07-Kakuzu.ogg", "Master") elseif (R == 9) then PlaySoundFile("sound\\07-The-Fire-Rises.ogg", "Master") elseif (R == 10) then PlaySoundFile("sound\\08-Prominence.ogg", "Master") elseif (R == 11) then PlaySoundFile("sound\\19-Steel-for-Humans.ogg.ogg", "Master") elseif (R == 12) then PlaySoundFile("sound\\10-Fear-Will-Find-You.ogg", "Master") elseif (R == 13) then PlaySoundFile("sound\\10-Silver-for-Monsters.ogg", "Master") elseif (R == 14) then PlaySoundFile("sound\\13-Imagine-The-Fire.ogg", "Master") elseif (R == 15) then PlaySoundFile("sound\\15-Divinity.ogg", "Master") elseif (R == 16) then PlaySoundFile("sound\\129-truth-behind-the-project.ogg", "Master") elseif (R == 17) then PlaySoundFile("sound\\16-tatakau.ogg", "Master") elseif (R == 18) then PlaySoundFile("sound\\17-No-Stone-Unturned-2.ogg", "Master") elseif (R == 19) then PlaySoundFile("sound\\17-No-Stone-Unturned.ogg", "Master") elseif (R == 20) then PlaySoundFile("sound\\35-Hunt-Or-Be-Hunted.ogg", "Master") elseif (R == 21) then PlaySoundFile("sound\\19-a-chaser.ogg", "Master") elseif (R == 22) then PlaySoundFile("sound\\FFXV-4.ogg", "Master") elseif (R == 23) then PlaySoundFile("sound\\35-Hunt-Or-Be-Hunted.ogg.ogg", "Master") elseif (R == 24) then PlaySoundFile("sound\\10-Silver-for-Monsters.ogg", "Master") elseif (R == 25) then PlaySoundFile("sound\\10-Silver-for-Monsters.ogg", "Master") elseif (R == 26) then PlaySoundFile("sound\\80percentplus-06-the-battle-is-to-the-strong.ogg", "Master") elseif (R == 27) then PlaySoundFile("sound\\ubw-06-souls-2.ogg", "Master") elseif (R == 28) then PlaySoundFile("sound\\ubw-18-fist.ogg", "Master") elseif (R == 29) then PlaySoundFile("sound\\ubw-23-each.ogg", "Master") elseif (R == 30) then PlaySoundFile("sound\\ubw2-05-overcome.ogg", "Master") elseif (R == 31) then PlaySoundFile("sound\\ubw2-06-bluered.ogg", "Master") elseif (R == 32) then PlaySoundFile("sound\\ubw2-18-excalibur.ogg", "Master") elseif (R == 33) then PlaySoundFile("sound\\ubw2-23-music.ogg", "Master") elseif (R == 34) then PlaySoundFile("sound\\ubw-06-souls.ogg", "Master")         elseif (R == 35) then PlaySoundFile("sound\\bloodborne-ludwig-1.ogg", "Master")                    elseif (R == 36) then PlaySoundFile("sound\\bloodborne-ludwig-2.ogg", "Master")                    elseif (R == 37) then PlaySoundFile("sound\\pull-Edge-of-Green.ogg.ogg", "Master")                    elseif (R == 38) then PlaySoundFile("sound\\Twilight-of-the-Gods.ogg", "Master")                    end                    elseif bossHP > 0.4 then                local R = 0        R=math.random(1,37)                if (R == 1) then PlaySoundFile("sound\\01-Rising-Dragon.ogg", "Master") elseif (R == 2) then PlaySoundFile("sound\\02-Cometh.ogg", "Master") elseif (R == 3) then PlaySoundFile("sound\\03-Gothams-Reckoning.ogg", "Master") elseif (R == 4) then PlaySoundFile("sound\\35-Hunt-Or-Be-Hunted.ogg", "Master") elseif (R == 5) then PlaySoundFile("sound\\06-the-battle-is-to-the-strong-2.ogg", "Master") elseif(R == 6) then PlaySoundFile("sound\\06-the-battle-is-to-the-strong.ogg", "Master") elseif (R == 7) then PlaySoundFile("sound\\FFXV-1.ogg", "Master") elseif (R == 8) then PlaySoundFile("sound\\07-Kakuzu.ogg", "Master") elseif (R == 9) then PlaySoundFile("sound\\07-The-Fire-Rises.ogg", "Master") elseif (R == 10) then PlaySoundFile("sound\\08-Prominence.ogg", "Master") elseif (R == 11) then PlaySoundFile("sound\\19-Steel-for-Humans.ogg.ogg", "Master") elseif (R == 12) then PlaySoundFile("sound\\10-Fear-Will-Find-You.ogg", "Master") elseif (R == 13) then PlaySoundFile("sound\\10-Silver-for-Monsters.ogg", "Master") elseif (R == 14) then PlaySoundFile("sound\\13-Imagine-The-Fire.ogg", "Master") elseif (R == 15) then PlaySoundFile("sound\\15-Divinity.ogg", "Master") elseif (R == 16) then PlaySoundFile("sound\\129-truth-behind-the-project.ogg", "Master") elseif (R == 17) then PlaySoundFile("sound\\16-tatakau.ogg", "Master") elseif (R == 18) then PlaySoundFile("sound\\17-No-Stone-Unturned-2.ogg", "Master") elseif (R == 19) then PlaySoundFile("sound\\17-No-Stone-Unturned.ogg", "Master") elseif (R == 20) then PlaySoundFile("sound\\35-Hunt-Or-Be-Hunted.ogg", "Master") elseif (R == 21) then PlaySoundFile("sound\\18-Dark-Spot.ogg", "Master") elseif (R == 22) then PlaySoundFile("sound\\19-a-chaser.ogg", "Master") elseif (R == 23) then PlaySoundFile("sound\\FFXV-4.ogg", "Master") elseif (R == 24) then PlaySoundFile("sound\\35-Hunt-Or-Be-Hunted.ogg.ogg", "Master") elseif (R == 25) then PlaySoundFile("sound\\10-Silver-for-Monsters.ogg", "Master") elseif (R == 26) then PlaySoundFile("sound\\ubw-06-souls.ogg", "Master") elseif (R == 27) then PlaySoundFile("sound\\ubw-06-souls-2.ogg", "Master") elseif (R == 28) then PlaySoundFile("sound\\ubw-18-fist.ogg", "Master") elseif (R == 29) then PlaySoundFile("sound\\ubw-23-each.ogg", "Master") elseif (R == 30) then PlaySoundFile("sound\\ubw2-05-overcome.ogg", "Master") elseif (R == 31) then PlaySoundFile("sound\\ubw2-06-bluered.ogg", "Master") elseif (R == 32) then PlaySoundFile("sound\\ubw2-18-excalibur.ogg", "Master") elseif (R == 33) then PlaySoundFile("sound\\ubw2-23-music.ogg", "Master") elseif (R == 34) then PlaySoundFile("sound\\01-Rising-Dragon.ogg", "Master")                    elseif (R == 35) then PlaySoundFile("sound\\bloodborne-ludwig-1.ogg", "Master")                    elseif (R == 36) then PlaySoundFile("sound\\bloodborne-ludwig-2.ogg", "Master")                    elseif (R == 37) then PlaySoundFile("sound\\Twilight-of-the-Gods.ogg", "Master")                    elseif (R == 38) then PlaySoundFile("sound\\06-Raido-2.ogg", "Master")                    end            else        local R = 0        R=math.random(1,8)                if (R == 1) then PlaySoundFile("sound\\execute-09-Incantation.ogg", "Master") elseif (R == 2) then PlaySoundFile("sound\\execute06-Raido.ogg", "Master") elseif (R == 3) then PlaySoundFile("sound\\execute-17-sara-ni-tatakau-2.ogg", "Master") elseif (R == 4) then PlaySoundFile("sound\\Twilight-of-the-Gods.ogg", "Master") elseif (R == 5) then PlaySoundFile("sound\\FFXV-3.ogg", "Master") elseif (R == 6) then PlaySoundFile("sound\\ubw2-05-overcome-execute.ogg", "Master") elseif (R == 7) then PlaySoundFile("sound\\bloodborne-ludwig-1.ogg", "Master") elseif (R == 8) then PlaySoundFile("sound\\bloodborne-ludwig-1.ogg", "Master") end    end    elseif IsInInstance() then        local R = 0    R=math.random(1,3)        if (R==1) then PlaySoundFile("sound\\FFXV-4.ogg", "Master") elseif (R==2) then PlaySoundFile("sound\\19-a-chaser.ogg", "Master") elseif (R==3) then PlaySoundFile("sound\\old-18-Risen-From-Darkness.ogg", "Master") end        else        local R = 0    R=math.random(1,3)    --print("noinstance")            if (R==1) then PlaySoundFile("sound\\world-Edge-of-Green.ogg", "Master")    elseif (R==2) then PlaySoundFile("sound\\world-Edge-of-Green.ogg", "Master")    elseif (R==3) then PlaySoundFile("sound\\world-Edge-of-Green.ogg", "Master") end        end  C_Timer.After(40, function()        SetCVar("Sound_EnableMusic", G)end)`

So as I was studying other addons to learn how they solve some problems, I came upon this

(from GW2 UI)

Lua Code:
`local MAP_FRAMES_HIDE = {}MAP_FRAMES_HIDE[1] = MiniMapMailFrameMAP_FRAMES_HIDE[2] = MiniMapVoiceChatFrameMAP_FRAMES_HIDE[3] = GameTimeFrameMAP_FRAMES_HIDE[4] = MiniMapTrackingButtonMAP_FRAMES_HIDE[5] = MiniMapTracking  local function hideMiniMapIcons()    for k, v in pairs(MAP_FRAMES_HIDE) do        if v then            v:Hide()            v:SetScript(                "OnShow",                function(self)                    self:Hide()                end            )        end    endend`

I didn't know that was possible. Is there a way for me to turn that long sequence of ifs and elses into a neat list/table? I suppose it's a more efficient code that makes it even easier to update later on?

 WoWInterface » Picking a random entry out of a table/list?