A Way to Change Music File Name By Variable

When I made TripleJump in GDevelop I had a lot of different music and I did what other here suggested and just made a ton of conditions for each music file… but I know better now, and the javascript is pretty simple to do here.

Save this text as soundbyname.json and import it as an extension:

{
  "author": "",
  "category": "Audio",
  "description": "",
  "extensionNamespace": "",
  "fullName": "SoundsByName",
  "helpPath": "",
  "iconUrl": "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAyMy4wLjMsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iSWNvbnMiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4Ig0KCSB2aWV3Qm94PSIwIDAgMzIgMzIiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDMyIDMyOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+DQo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPg0KCS5zdDB7ZmlsbDpub25lO3N0cm9rZTojMDAwMDAwO3N0cm9rZS13aWR0aDoyO3N0cm9rZS1saW5lam9pbjpyb3VuZDtzdHJva2UtbWl0ZXJsaW1pdDoxMDt9DQoJLnN0MXtmaWxsOm5vbmU7c3Ryb2tlOiMwMDAwMDA7c3Ryb2tlLXdpZHRoOjI7c3Ryb2tlLWxpbmVjYXA6cm91bmQ7c3Ryb2tlLWxpbmVqb2luOnJvdW5kO3N0cm9rZS1taXRlcmxpbWl0OjEwO30NCjwvc3R5bGU+DQo8cmVjdCB4PSI2IiB5PSIzIiBjbGFzcz0ic3QxIiB3aWR0aD0iMjAiIGhlaWdodD0iMjYiLz4NCjxjaXJjbGUgY2xhc3M9InN0MSIgY3g9IjE2IiBjeT0iMTkiIHI9IjciLz4NCjxjaXJjbGUgY2xhc3M9InN0MSIgY3g9IjE2IiBjeT0iMTkiIHI9IjIiLz4NCjxjaXJjbGUgY2xhc3M9InN0MSIgY3g9IjIxIiBjeT0iOCIgcj0iMiIvPg0KPC9zdmc+DQo=",
  "name": "SoundsByName",
  "previewIconUrl": "https://resources.gdevelop-app.com/assets/Icons/Line Hero Pack/Master/SVG/Music/Music_speaker_audio_sound.svg",
  "shortDescription": "Example Extension to allow playing sounds by name",
  "version": "0.0.1",
  "tags": [],
  "authorIds": [
    "Khhq7a4Lu8bt91GLLhG0QiGLpfx2"
  ],
  "dependencies": [],
  "eventsFunctions": [
    {
      "description": "Play Music on a Channel (By Name)",
      "fullName": "Play Music on a Channel By Name",
      "functionType": "Action",
      "group": "",
      "name": "PlayMusicOnChannelByName",
      "private": false,
      "sentence": "Plays _PARAM1_ on channel _PARAM2_ (Loop = _PARAM3_, Volume = _PARAM4_, Pitch = _PARAM5_)",
      "events": [
        {
          "type": "BuiltinCommonInstructions::JsCode",
          "inlineCode": "const soundManager = runtimeScene._runtimeGame._soundManager;\neventsFunctionContext.getArgument(\"version\")\nsoundManager.playMusicOnChannel(\n    eventsFunctionContext.getArgument(\"sound\"),\n    eventsFunctionContext.getArgument(\"channel\"),\n    eventsFunctionContext.getArgument(\"loop\"),\n    eventsFunctionContext.getArgument(\"volume\"),\n    eventsFunctionContext.getArgument(\"pitch\"));\n",
          "parameterObjects": "",
          "useStrict": true,
          "eventsSheetExpanded": true
        }
      ],
      "parameters": [
        {
          "codeOnly": false,
          "defaultValue": "",
          "description": "Sound Resource Name",
          "longDescription": "",
          "name": "sound",
          "optional": false,
          "supplementaryInformation": "",
          "type": "string"
        },
        {
          "codeOnly": false,
          "defaultValue": "",
          "description": "Channel Number",
          "longDescription": "",
          "name": "channel",
          "optional": false,
          "supplementaryInformation": "",
          "type": "expression"
        },
        {
          "codeOnly": false,
          "defaultValue": "no",
          "description": "Loop when done?",
          "longDescription": "",
          "name": "loop",
          "optional": true,
          "supplementaryInformation": "",
          "type": "yesorno"
        },
        {
          "codeOnly": false,
          "defaultValue": "",
          "description": "Volume (0-100)",
          "longDescription": "",
          "name": "volume",
          "optional": false,
          "supplementaryInformation": "",
          "type": "expression"
        },
        {
          "codeOnly": false,
          "defaultValue": "",
          "description": "Pitch (typically 1)",
          "longDescription": "",
          "name": "pitch",
          "optional": false,
          "supplementaryInformation": "",
          "type": "expression"
        }
      ],
      "objectGroups": []
    }
  ],
  "eventsBasedBehaviors": []
}

Example Usage:

Using what I provide there you can easily expand it to support all of the methods here:

1 Like