Skip to content

Instantly share code, notes, and snippets.

@Choonster
Created December 4, 2012 15:06
Show Gist options
  • Select an option

  • Save Choonster/4204952 to your computer and use it in GitHub Desktop.

Select an option

Save Choonster/4204952 to your computer and use it in GitHub Desktop.

Revisions

  1. Choonster revised this gist Dec 4, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions randomSounds.lua
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,6 @@ end

    local function PlayRandomSoundAbbrev() -- This is a shorter version of the function above that does the exact same thing
    PlaySoundFile(soundFiles[random(1, numSounds)])
    -- The above line performs each task in the same order as the first function,
    -- but passes the result of task directly to the next one instead of explicitly storing the result in a local variable first
    -- The above line performs each "task" in the same order as the first function (function call, table lookup, function call),
    -- but passes the result of the "task" directly to the next one instead of explicitly storing the result in a local variable first
    end
  2. Choonster created this gist Dec 4, 2012.
    19 changes: 19 additions & 0 deletions randomSounds.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    local soundFiles = { -- These are example files only
    "Sound\\Creature\\SomeCreature\\SomeCreature_Aggro01.ogg",
    "Sound\\Creature\\OtherThing\\OtherThing_Greeting01.ogg"
    "Interface\\AddOns\\MyAddOn\\Sounds\\levelup1.ogg"
    }

    local numSounds = #soundFiles -- Get the number of entries in the soundFiles table

    local function PlayRandomSound()
    local index = random(1, numSounds) -- Generate a random number between 1 and numSounds to use as an index. random is an alias of the math.random function
    local path = soundFiles[index] -- Get the path at the generated index
    PlaySoundFile(path) -- Play the sound file
    end

    local function PlayRandomSoundAbbrev() -- This is a shorter version of the function above that does the exact same thing
    PlaySoundFile(soundFiles[random(1, numSounds)])
    -- The above line performs each task in the same order as the first function,
    -- but passes the result of task directly to the next one instead of explicitly storing the result in a local variable first
    end