Need help: How to check a name on audio?

I wonder how I can use this kind approach without an error:

Example:

local sfx={one} sfx.one = audio.loadSound( "sfx/mp3\_music/mysound.mp3") sfx.one.name="soundfx"

The last line with the .name is generating this error:

attempt to index field ‘one’ (a userdata value)

How can I add some info to my soundfiles, so I can later check in an if clause what file is getting called? I need some kind of ID for each sound (like the name) to check in my module what sound was called.

How can I do this?

May be something like that?

local sfx={ 

    one = { 

        sound = audio.loadSound( “coin.mp3”), 

        name  = ‘soundfx’ 

    } 

}

Thank you very much for your feast feedback!

Is this then still working when calling audio.play (sfx.one) ?

Because I need a solution I don’t have to change all the calls and just add a “fix” to the audio module wthout going to all the code!

Maybe a better way to do it would be:

[lua]

local sfx={ 

   coinSound = audio.loadSound(“coin.mp3”),

   gameOverMusic = audio.loadSound(“gameOver.mp3”)

}

local function playSound(name)

  if (sfx[name] ~= nil)

    audio.play(sfx[name])

    print ("Sound being played is: " … name)

  end

end

playSound(“coinSound”)

playSound(“gameOverMusic”)

[/lua]

No. @nick_sherman way doing this is better :slight_smile:

May be something like that?

local sfx={ 

    one = { 

        sound = audio.loadSound( “coin.mp3”), 

        name  = ‘soundfx’ 

    } 

}

Thank you very much for your feast feedback!

Is this then still working when calling audio.play (sfx.one) ?

Because I need a solution I don’t have to change all the calls and just add a “fix” to the audio module wthout going to all the code!

Maybe a better way to do it would be:

[lua]

local sfx={ 

   coinSound = audio.loadSound(“coin.mp3”),

   gameOverMusic = audio.loadSound(“gameOver.mp3”)

}

local function playSound(name)

  if (sfx[name] ~= nil)

    audio.play(sfx[name])

    print ("Sound being played is: " … name)

  end

end

playSound(“coinSound”)

playSound(“gameOverMusic”)

[/lua]

No. @nick_sherman way doing this is better :slight_smile: