WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Picking a random entry out of a table/list? (https://www.wowinterface.com/forums/showthread.php?t=58466)

Krainz 12-17-20 09:30 AM

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?

SDPhantom 12-17-20 10:13 AM

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"

Krainz 12-17-20 10:19 AM

So if it's a list of 15 entries, then I should use

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

?

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

Seerah 12-17-20 10:57 AM

#

http://lua-users.org/wiki/TablesTutorial

Krainz 12-17-20 11:11 AM

Thanks, I'll look into that!

Fizzlemizz 12-17-20 11:32 AM

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:
  1. if a then
  2.    PlaySoundFile(TableofOggs[random(1, 5)])
  3. elseif b then
  4.    PlaySoundFile(TableofOggs[random(6, 12)])
  5. elseif ...
  6.  
  7. end

Vrul 12-17-20 12:39 PM

I grouped up duplicates and removed the repetitive stuff ("sounds\\", ".ogg"):
Lua Code:
  1. local sounds
  2. if UnitExists("boss1") then
  3.     local bossHP = UnitHealth("boss1") / UnitHealthMax("boss1")
  4.     if bossHP > 0.8 then
  5.         sounds = {
  6.             "01-Rising-Dragon",
  7.             "02-Cometh",
  8.             "03-Gothams-Reckoning",
  9.             "06-the-battle-is-to-the-strong",
  10.             "06-the-battle-is-to-the-strong-2",
  11.             "07-Kakuzu",
  12.             "07-The-Fire-Rises",
  13.             "08-Prominence",
  14.             "10-Fear-Will-Find-You",
  15.             "10-Silver-for-Monsters",
  16.             "10-Silver-for-Monsters",
  17.             "13-Imagine-The-Fire",
  18.             "15-Divinity",
  19.             "16-tatakau",
  20.             "17-No-Stone-Unturned",
  21.             "17-No-Stone-Unturned-2",
  22.             "19-a-chaser",
  23.             "19-Steel-for-Humans.ogg",
  24.             "35-Hunt-Or-Be-Hunted",
  25.             "35-Hunt-Or-Be-Hunted",
  26.             "35-Hunt-Or-Be-Hunted.ogg",
  27.             "129-truth-behind-the-project",
  28.             "bloodborne-ludwig-1",
  29.             "bloodborne-ludwig-2",
  30.             "FFXV-1",
  31.             "FFXV-4",
  32.             "Twilight-of-the-Gods",
  33.             "ubw-06-souls",
  34.             "ubw-06-souls-2",
  35.             "ubw-18-fist",
  36.             "ubw-23-each",
  37.             "ubw2-05-overcome",
  38.             "ubw2-06-bluered",
  39.             "ubw2-18-excalibur",
  40.             "ubw2-23-music",
  41.  
  42.             "10-Silver-for-Monsters",
  43.             "80percentplus-06-the-battle-is-to-the-strong",
  44.             "pull-Edge-of-Green.ogg",
  45.         }
  46.     elseif bossHP > 0.4 then
  47.         sounds = {
  48.             "01-Rising-Dragon",
  49.             "02-Cometh",
  50.             "03-Gothams-Reckoning",
  51.             "06-the-battle-is-to-the-strong",
  52.             "06-the-battle-is-to-the-strong-2",
  53.             "07-Kakuzu",
  54.             "07-The-Fire-Rises",
  55.             "08-Prominence",
  56.             "10-Fear-Will-Find-You",
  57.             "10-Silver-for-Monsters",
  58.             "10-Silver-for-Monsters",
  59.             "13-Imagine-The-Fire",
  60.             "15-Divinity",
  61.             "16-tatakau",
  62.             "17-No-Stone-Unturned",
  63.             "17-No-Stone-Unturned-2",
  64.             "19-a-chaser",
  65.             "19-Steel-for-Humans.ogg",
  66.             "35-Hunt-Or-Be-Hunted",
  67.             "35-Hunt-Or-Be-Hunted",
  68.             "35-Hunt-Or-Be-Hunted.ogg",
  69.             "129-truth-behind-the-project",
  70.             "bloodborne-ludwig-1",
  71.             "bloodborne-ludwig-2",
  72.             "FFXV-1",
  73.             "FFXV-4",
  74.             "Twilight-of-the-Gods",
  75.             "ubw-06-souls",
  76.             "ubw-06-souls-2",
  77.             "ubw-18-fist",
  78.             "ubw-23-each",
  79.             "ubw2-05-overcome",
  80.             "ubw2-06-bluered",
  81.             "ubw2-18-excalibur",
  82.             "ubw2-23-music",
  83.  
  84.             "01-Rising-Dragon",
  85. --          "06-Raido-2",
  86.             "18-Dark-Spot",
  87.         }
  88.     else
  89.         sounds = {
  90.             "bloodborne-ludwig-1",
  91.             "bloodborne-ludwig-1",
  92.             "execute06-Raido",
  93.             "execute-09-Incantation",
  94.             "execute-17-sara-ni-tatakau-2",
  95.             "FFXV-3",
  96.             "Twilight-of-the-Gods",
  97.             "ubw2-05-overcome-execute",
  98.         }
  99.     end
  100. elseif IsInInstance() then
  101.     sounds = {
  102.         "19-a-chaser",
  103.         "FFXV-4",
  104.         "old-18-Risen-From-Darkness",
  105.     }
  106. else
  107.     sounds = {
  108.         "world-Edge-of-Green",
  109.         "world-Edge-of-Green",
  110.         "world-Edge-of-Green",
  111.     }
  112. end
  113.  
  114. local originalCVar = GetCVar("Sound_EnableMusic")
  115. SetCVar("Sound_EnableMusic", 0)
  116.  
  117. PlaySoundFile(("sound\\%s.ogg"):format(sounds[math.random(#sounds)]), "Master")
  118.  
  119. C_Timer.After(40, function()
  120.     SetCVar("Sound_EnableMusic", originalCVar)
  121. 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.

Krainz 12-18-20 01:49 PM

Thank you so much!


All times are GMT -6. The time now is 08:40 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI