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?

12-17-20, 10:13 AM   #2
SDPhantom
A Pyroguard Emberseer

Join Date: Jul 2006
Posts: 2,007
You could do something like this.

Code:
```local SongList={
"songA";
"songB";
"songC";
};

print(SongList[math.random(#SongList)]);```
This should print a random string grabbed from SongList. The length operator (#) returns the length of the table (number of consecutive numerical indices starting at 1). Giving only one argument to math.random() is the same as calling math.random(1,n) where n is the number you're passing it.

PS: If you're referencing sounds located in your addon folder, you need to make sure the path is formatted relative to WoW's root directory.
It should look something like this:
Code:
`"Interface\\AddOns\\YourAddOn\\PathToSound"`
__________________
 "All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools." -Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 12-17-20 at 10:20 AM.

12-17-20, 10:19 AM   #3
Krainz
A Wyrmkin Dreamwalker
Join Date: Oct 2016
Posts: 57
So if it's a list of 15 entries, then I should use

Lua Code:
`print(SongList[math.random(15SongList)]);`

?

Or am I overthinking it and #SongList is fine as it is?

12-17-20, 10:57 AM   #4
Seerah
Fishing Trainer

Join Date: Oct 2006
Posts: 10,816
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

12-17-20, 11:11 AM   #5
Krainz
A Wyrmkin Dreamwalker
Join Date: Oct 2016
Posts: 57
Thanks, I'll look into that!

12-17-20, 11:32 AM   #6
Fizzlemizz
I did that?

Join Date: Dec 2011
Posts: 1,341
If the table has a number entries and the first 5 are for one situation and the next 5 (or whatever) are for another situation then you could:

Lua Code:
`if a then    PlaySoundFile(TableofOggs[random(1, 5)])elseif b then   PlaySoundFile(TableofOggs[random(6, 12)])elseif ... end`
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

12-17-20, 12:39 PM   #7
Vrul
An Onyxian Warder

Join Date: Nov 2007
Posts: 397
I grouped up duplicates and removed the repetitive stuff ("sounds\\", ".ogg"):
Lua Code:
`local soundsif UnitExists("boss1") then    local bossHP = UnitHealth("boss1") / UnitHealthMax("boss1")    if bossHP > 0.8 then        sounds = {            "01-Rising-Dragon",            "02-Cometh",            "03-Gothams-Reckoning",            "06-the-battle-is-to-the-strong",            "06-the-battle-is-to-the-strong-2",            "07-Kakuzu",            "07-The-Fire-Rises",            "08-Prominence",            "10-Fear-Will-Find-You",            "10-Silver-for-Monsters",            "10-Silver-for-Monsters",            "13-Imagine-The-Fire",            "15-Divinity",            "16-tatakau",            "17-No-Stone-Unturned",            "17-No-Stone-Unturned-2",            "19-a-chaser",            "19-Steel-for-Humans.ogg",            "35-Hunt-Or-Be-Hunted",            "35-Hunt-Or-Be-Hunted",            "35-Hunt-Or-Be-Hunted.ogg",            "129-truth-behind-the-project",            "bloodborne-ludwig-1",            "bloodborne-ludwig-2",            "FFXV-1",            "FFXV-4",            "Twilight-of-the-Gods",            "ubw-06-souls",            "ubw-06-souls-2",            "ubw-18-fist",            "ubw-23-each",            "ubw2-05-overcome",            "ubw2-06-bluered",            "ubw2-18-excalibur",            "ubw2-23-music",             "10-Silver-for-Monsters",            "80percentplus-06-the-battle-is-to-the-strong",            "pull-Edge-of-Green.ogg",        }    elseif bossHP > 0.4 then        sounds = {            "01-Rising-Dragon",            "02-Cometh",            "03-Gothams-Reckoning",            "06-the-battle-is-to-the-strong",            "06-the-battle-is-to-the-strong-2",            "07-Kakuzu",            "07-The-Fire-Rises",            "08-Prominence",            "10-Fear-Will-Find-You",            "10-Silver-for-Monsters",            "10-Silver-for-Monsters",            "13-Imagine-The-Fire",            "15-Divinity",            "16-tatakau",            "17-No-Stone-Unturned",            "17-No-Stone-Unturned-2",            "19-a-chaser",            "19-Steel-for-Humans.ogg",            "35-Hunt-Or-Be-Hunted",            "35-Hunt-Or-Be-Hunted",            "35-Hunt-Or-Be-Hunted.ogg",            "129-truth-behind-the-project",            "bloodborne-ludwig-1",            "bloodborne-ludwig-2",            "FFXV-1",            "FFXV-4",            "Twilight-of-the-Gods",            "ubw-06-souls",            "ubw-06-souls-2",            "ubw-18-fist",            "ubw-23-each",            "ubw2-05-overcome",            "ubw2-06-bluered",            "ubw2-18-excalibur",            "ubw2-23-music",             "01-Rising-Dragon",--          "06-Raido-2",            "18-Dark-Spot",        }    else        sounds = {            "bloodborne-ludwig-1",            "bloodborne-ludwig-1",            "execute06-Raido",            "execute-09-Incantation",            "execute-17-sara-ni-tatakau-2",            "FFXV-3",            "Twilight-of-the-Gods",            "ubw2-05-overcome-execute",        }    endelseif IsInInstance() then    sounds = {        "19-a-chaser",        "FFXV-4",        "old-18-Risen-From-Darkness",    }else    sounds = {        "world-Edge-of-Green",        "world-Edge-of-Green",        "world-Edge-of-Green",    }end local originalCVar = GetCVar("Sound_EnableMusic")SetCVar("Sound_EnableMusic", 0) PlaySoundFile(("sound\\%s.ogg"):format(sounds[math.random(#sounds)]), "Master") C_Timer.After(40, function()    SetCVar("Sound_EnableMusic", originalCVar)end)`
Anything with an ".ogg" still had two to begin with (not sure if intended or not). The commented out "06-Raido-2" is because it wasn't reachable with your original code.

12-18-20, 01:49 PM   #8
Krainz
A Wyrmkin Dreamwalker
Join Date: Oct 2016
Posts: 57
Thank you so much!

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

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off