Quantcast
Picking a random entry out of a table/list? - WoWInterface
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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:
  1. local G = 0
  2. G = GetCVar("Sound_EnableMusic")
  3. SetCVar("Sound_EnableMusic", 0)
  4.  
  5.  
  6. if UnitExists("boss1") then
  7.    
  8.     local bossHP = UnitHealth("boss1")/UnitHealthMax("boss1")
  9.    
  10.     if bossHP > 0.8 then
  11.         local R = 0
  12.         R=math.random(1,38)
  13.        
  14.        
  15.         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")
  16.         elseif (R == 35) then PlaySoundFile("sound\\bloodborne-ludwig-1.ogg", "Master")
  17.            
  18.         elseif (R == 36) then PlaySoundFile("sound\\bloodborne-ludwig-2.ogg", "Master")
  19.            
  20.         elseif (R == 37) then PlaySoundFile("sound\\pull-Edge-of-Green.ogg.ogg", "Master")
  21.            
  22.         elseif (R == 38) then PlaySoundFile("sound\\Twilight-of-the-Gods.ogg", "Master")
  23.            
  24.         end
  25.        
  26.        
  27.     elseif bossHP > 0.4 then
  28.        
  29.         local R = 0
  30.         R=math.random(1,37)
  31.        
  32.         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")
  33.            
  34.         elseif (R == 35) then PlaySoundFile("sound\\bloodborne-ludwig-1.ogg", "Master")
  35.            
  36.         elseif (R == 36) then PlaySoundFile("sound\\bloodborne-ludwig-2.ogg", "Master")
  37.            
  38.         elseif (R == 37) then PlaySoundFile("sound\\Twilight-of-the-Gods.ogg", "Master")
  39.            
  40.         elseif (R == 38) then PlaySoundFile("sound\\06-Raido-2.ogg", "Master")
  41.            
  42.         end
  43.        
  44.     else
  45.         local R = 0
  46.         R=math.random(1,8)
  47.        
  48.         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
  49.     end
  50.    
  51. elseif IsInInstance() then
  52.    
  53.     local R = 0
  54.     R=math.random(1,3)
  55.    
  56.     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
  57.    
  58.    
  59. else
  60.    
  61.     local R = 0
  62.     R=math.random(1,3)
  63.     --print("noinstance")
  64.    
  65.    
  66.     if (R==1) then PlaySoundFile("sound\\world-Edge-of-Green.ogg", "Master")
  67.     elseif (R==2) then PlaySoundFile("sound\\world-Edge-of-Green.ogg", "Master")
  68.     elseif (R==3) then PlaySoundFile("sound\\world-Edge-of-Green.ogg", "Master") end
  69.    
  70.    
  71. end
  72.  
  73.  
  74. C_Timer.After(40, function()
  75.         SetCVar("Sound_EnableMusic", G)
  76. end)


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

(from GW2 UI)

Lua Code:
  1. local MAP_FRAMES_HIDE = {}
  2. MAP_FRAMES_HIDE[1] = MiniMapMailFrame
  3. MAP_FRAMES_HIDE[2] = MiniMapVoiceChatFrame
  4. MAP_FRAMES_HIDE[3] = GameTimeFrame
  5. MAP_FRAMES_HIDE[4] = MiniMapTrackingButton
  6. MAP_FRAMES_HIDE[5] = MiniMapTracking
  7.  
  8.  
  9. local function hideMiniMapIcons()
  10.     for k, v in pairs(MAP_FRAMES_HIDE) do
  11.         if v then
  12.             v:Hide()
  13.             v:SetScript(
  14.                 "OnShow",
  15.                 function(self)
  16.                     self:Hide()
  17.                 end
  18.             )
  19.         end
  20.     end
  21. end


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?
  Reply With Quote
 

WoWInterface » Developer Discussions » Lua/XML Help » Picking a random entry out of a table/list?

Thread Tools
Display Modes

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