Request: Plugin to play other Map's BGM

So I wanted to make managing BGM more less of a chore for a dungeon with multiple map that has same BGM music.
When changing the BGM of a dungeon, you also need to change the BGM of other map of the same dungeon.
Well, you can leave them blank so that the last map's BGM played however it will have problem if the dungeon is not linear exploration (Can access other dungeon maps from a town, for example, not just the main entrance. and it will result the Town BGM to carry over to the dungeon map).

The idea in general:
I think the simple way is to fetch note tags as a reference, then loads the data using it as reference.

fetch notetag value of the current map (<BGM_reference: mapID>)
Load Map Data Map(mapID).json using notetag as reference and then fetch BGM.
Play fetched BGM.

I tried making it on my own but i'm pretty much newbiest of newbie at JS, so I'm stuck lol though i can understand a bit.
Spoiler: The said code
JavaScript:
    Game_Map.prototype.autoplay = function() {
    var groupBGM = parseInt($dataMap.meta.bgm_reference);
    var BGMsrc = groupBGM.padZero(3);
    var BGMData;
    DataManager.loadDataFile('BGMData', 'Map'+BGMsrc+'.json')
    if ($dataMap.autoplayBgm) {
        if ($gamePlayer.isInVehicle()) {
            $gameSystem.saveWalkingBgm2();
        } else {
            AudioManager.playBgm($dataMap.bgm);
        }
    } else if (groupBGM > 0)  {
        if ($gamePlayer.isInVehicle()) {
            $gameSystem.saveWalkingBgm2();
        } else {
            console.log(BGMData);
            AudioManager.playBgm(BGMData.bgm);
        }  
    }
    if ($dataMap.autoplayBgs) {
        AudioManager.playBgs($dataMap.bgs);
    }
};

The condition does work to an extent because it returns "console.log(BGMData)" which is undefined btw; but it failed to fetch another map BGM data and play it (obviously).

If someone do make it, i will compare and study it. Hope i can get better hehe.
If someone do help me, it also help me study how to make plugin. It's fine either way.

Thanks in advance.
if the funtion exist, alias it, but the problem here as well, it doesn't have a return value.
without the return value, it probably wont work.

there might be also be an update function to replay the bgm.
there are some audio plugin though, but probably not like this one around,
it seems usefull though if you get it to work to have a bgm go continue to other maps.

you could create an event on parallel, but that way seems nicer.

another way with a richer feature (personal thoughs) is to have params with
MAP ID + BGM/BGS like

MapID + BGM: 2, 4, 5, 8, 9 "BGM file"

so those map idea's play BGM file when you enter it, or seperate the 2

Maps 1 ids: 1, 4, 6, 7, 8, 9
Maps 1 bmg: (1 bmg file here)

maps 2 ids: 2, 3, 10, 11
maps 2 bgm: (1 bgm file here)

or similair, so you have to configure it 1 time or edit from time to time
but it will play nicely when done 🙂

maybe better in the way you do it though (just feedback/suggestion)
[Edit: Disregard this post. I though about it some more after posting -_- A plugin would certainly be ideal since the bgm will keep restarting if you use a parallel event.]
Spoiler
If you don't mind using script calls, why not just get check the map id?
If player is in any of the dungeon maps, bgm plays.
Maybe you can check in a common event:
var dungeonMaps= [1,2,3,4,5,6,7,8];
var result =dungeonMaps.includes($gameMap.mapId());
if (result==true){
play bgm script call which I don't know :")
}
This didn't work for the game variables for some reason.
@ShadowDragon, Yeah, that way could also work and maybe a little easier in managing BGM for maps because it on one page that is plugin file.
It looks like it needs advanced params coding tho. Just like some Yanfly's param options.
I haven't gotten there yet

Structure List with structure like:
Code:
groupName (text)
mapIDs (array)
bgmFile (file)
volume (num)
pitch (num)
pan (num)

It could also be used to group BGS and adding more possibility altogether
well... actually I'm not use default map bgm
I use play bgm and auto run when player enter
and if I want to manage it
just make some condition with variable to select bgm


also with a bit of script call will more detail manageable
JavaScript:
var myBgm = {  name: "name of bgm",  volume: 80,  pitch: 100,  pan: 100};AudioManager.playBgm(myBgm);
you can just change the value after : to $gameVariables.value(id)
if you want to change a mapID with a different one, it can be possible,
but doing it over and over can have a slight delay if you use alot of events.

but there are methodes without plugin and with plugin, it really depends on the OP request.
smoothes = everything though.

its not always about scriptcall that can work and mostly does, but most time on a specific
reason, most bgm dont play continues and when switching to others or you have delays
in the same bmg you want to play in a new map.

controlling the map ids and bgm (maybe volume, pitch, pan) is the best way 🙂
@Hyouryuu-Na, ah yeah it is also possible that way. Though in my case I didn't use scripcalls but instead use script condition.
I also managed to make it work with map note tags, thanks for giving me ideas.

Though this plugin idea is neat if realized hahaha.

@pasunna, yeah I haven't realized that time that doing it with parallel common event is also possible. At the time i learned to extract notetag value, this plugin idea just popped up.

@ShadowDragon, yeah it will probably had slight delay because loading a bunch of event will take time.
At this time my game's common event is not that many, so it'll probably be alright haha.

I event it like this:
1593171768176.png


It works quite well.
[Edit: Disregard this post. I though about it some more after posting -_- A plugin would certainly be ideal since the bgm will keep restarting if you use a parallel event.]

Yeah plugin would be the ideal answer. Because with parallel event, the event sent the request again and again even with "erase event" added.

I've tried with the common event i posted before. It doesn't restart the BGM, even when you change map within the same BGM Group.
It would invalidate BGM changes thru event though, the only way to do the change is to disable the common event first then change the BGM.
So... basically you want something that will have the same effect as setting the desired background track on every map, only without having to go to the trouble of actually setting it on every map?

I guess it can be useful if you think you might want to change the track at some point in the future...
- -
wait... why you parallel it
sorry I'm not english speaker
What I say is
when player enter any of your map
you set one auto run event
that check bgm condition and then erase it
so it will run once every time you enter that map
So... basically you want something that will have the same effect as setting the desired background track on every map, only without having to go to the trouble of actually setting it on every map?

I guess it can be useful if you think you might want to change the track at some point in the future...
Yes, you can say some kind of utility plugin.
Usually I use RTP BGM for dungeon while making map. So the plugin is to lessen the time needed to change BGM that fits with the dungeon, making BGM managing easier.

@pasunna, no probs buddy.
Yeah I can also do that and have tried it. I just realized that common event can't erase itself. So i call it using normal parallel event that erase itself.
I just had an idea for this using the map hierarchy in the editor - basically, if a map doesn't define a background music, use the track from the parent map. I'll look into whether this will be easy to do and get back to you.
Oh using map's parent's ID? that could also work. and might be better than mine's hahaha.

I have already made a working plugin with my idea, though it has a bit of bug in it.

It invalidates autorun event with BGM change (if it isn't delayed). It happens when the plugin detected a change in group id (notetags). It wouldn't invalidate BGM change if the group ID are the same.

Spoiler: Code
JavaScript:
(function($) {

  var currentGroup;
 
  const BGM_Group_Autoplay = Game_Map.prototype.autoplay;
  Game_Map.prototype.autoplay = function() {
  BGM_Group_Autoplay.call(this);
    var BGMGroup = parseInt($dataMap.meta.bgm_reference);
    var mapRef = 'Map%1.json'.format(BGMGroup.padZero(3));
    loadReference = function(src) {
      var xhr = new XMLHttpRequest();
      var url = 'data/' + src;
      xhr.open('GET', url);
      xhr.overrideMimeType('application/json');
      xhr.onload = function() {
        if (xhr.status < 400) {
          dataMapRef = JSON.parse(xhr.responseText);
          DataManager.extractMetadata(dataMapRef);
          if (BGMGroup != currentGroup) {
            currentGroup = BGMGroup;
            AudioManager.playBgm(dataMapRef.bgm); 
          }
        }
      };
      xhr.onerror = ResourceHandler.createLoader(url, loadReference.bind($, src));
      dataMapRef = null;
      xhr.send();
    };
    if (BGMGroup > 0 && !$dataMap.autoplayBgm)  {
      if ($gamePlayer.isInVehicle()) {
          $gameSystem.saveWalkingBgm2();
      } else {
          loadReference(mapRef);
      }   
    }
  };

})(eqD.groupedBGM);

If someone can help identifying it, I'd be very grateful
I think what you'd want to do is also override DataManager.isMapLoaded() so that it doesn't return true until your XHR request completes. So something like this:

JavaScript:
// You could alias this properly, I didn't bother here because the default just returns !!$dataMap
DataManager.isMapLoaded = function() {
    return !!$dataMap && this._bgmLoaded;
};
const load = DataManager.loadMapData;
DataManager.loadMapData = function(id) {
    this._bgmLoaded = false;
    load.call(this, id);
};

And then add DataManager._bgmLoaded = true at the top of your onload callback.
I think what you'd want to do is also override DataManager.isMapLoaded() so that it doesn't return true until your XHR request completes. So something like this:
Spoiler
JavaScript:
// You could alias this properly, I didn't bother here because the default just returns !!$dataMap
DataManager.isMapLoaded = function() {
    return !!$dataMap && this._bgmLoaded;
};
const load = DataManager.loadMapData;
DataManager.loadMapData = function(id) {
    this._bgmLoaded = false;
    load.call(this, id);
};

And then add DataManager._bgmLoaded = true at the top of your onload callback.
Added it like what you have said, but now it can't load the map completely.
probably because the map isn't loaded and the plugin can't fetch map notetag to call the xhr request which in turn can't turn DataManager._bgmLoaded = true.

Tried to call the script thru console, and now it can load it.

Spoiler: the code
JavaScript:
(function($) {

  var currentGroup;
  var BGMGroupIsEnabled;

//DATA MANAGER
  const eqd_BGMGroup_loadMapData = DataManager.loadMapData;
  DataManager.loadMapData = function(id) {
    this._bgmLoaded = false;
    eqd_BGMGroup_loadMapData.call(this, id);
};

  const eqd_BGMGroup_isMaploaded = DataManager.isMapLoaded;
  DataManager.isMapLoaded = function() {
    return !!$dataMap && this._bgmLoaded;
    eqd_BGMGroup_isMaploaded.call(this);
};

//GAME MAP
  const BGM_Group_Autoplay = Game_Map.prototype.autoplay;
  Game_Map.prototype.autoplay = function() {
  BGM_Group_Autoplay.call(this);
    var BGMGroup = parseInt($dataMap.meta.bgm_reference);
    var mapRef = 'Map%1.json'.format(BGMGroup.padZero(3));
    loadReference = function(src) {
      var xhr = new XMLHttpRequest();
      var url = 'data/' + src;
      xhr.open('GET', url);
      xhr.overrideMimeType('application/json');
      xhr.onload = function() {
        if (xhr.status < 400) {
          dataMapRef = JSON.parse(xhr.responseText);
          DataManager.extractMetadata(dataMapRef);
          if (BGMGroup != currentGroup) {
            currentGroup = BGMGroup;
            AudioManager.playBgm(dataMapRef.bgm);
            DataManager._bgmLoaded = true;
            console.log("This is group : "+currentGroup); //fetch other map bgm first.
            console.log("Current song is : "+AudioManager._currentBgm.name); 
          } else {
            DataManager._bgmLoaded = true;
          }
        }
      };
      xhr.onerror = ResourceHandler.createLoader(url, loadReference.bind($, src));
      dataMapRef = null;
      xhr.send();
    };
    if (BGMGroup > 0 && !$dataMap.autoplayBgm)  {
      if ($gamePlayer.isInVehicle()) {
          $gameSystem.saveWalkingBgm2();
          DataManager._bgmLoaded = true;
      } else {
          loadReference(mapRef);
      } 
    } else {
      DataManager._bgmLoaded = true;
    }
  };

  const eqD_groupedBGM_PluginCommands = Game_Interpreter.prototype.pluginCommand;
  Game_Interpreter.prototype.pluginCommand = function (command, args) {
  eqD_groupedBGM_PluginCommands.call(this, command, args);
    if (command.toLowerCase() === "groupedbgm") {
      const groupedBGMEnabled = parseInt(args[0]);
      if (groupedBGMEnabled.toLowerCase() == "true") {
          var BGMGroupIsEnabled = "true";
      } else if (groupedBGMEnabled.toLowerCase() == "false") {
          var BGMGroupIsEnabled = "false";
      } else {
          console.log("eqD_groupedBGM: BGM, wrong argument!");
      }
   }
};

})(eqD.groupedBGM);

I put DataManager._bgmLoaded = true in the conditional IF so that when it return false it still load the map data.
I put DataManager._bgmLoaded = true in the conditional IF so that when it return false it still load the map data.
I was imagining setting the flag even if the load failed, so that DataManager._bgmLoaded = true is the first line of your onload, just before if(xhr.status < 400)[/code]. I'm not quite sure if that's the best solution, mind you, as I think the engine might call your onload more than once. That said, my suggestion probably does mix up the ordering of things. You're overriding [icode]autoplay, which obviously can't be called until the map is actually loaded.

My version sets the override to run as soon as the map has finished loading, and substitutes the new background track into $dataMap where autoplay will then see it and play it.

With an autoplay override, I think the best way to avoid the delay requirement would be to add a check in your onload - if the current background track is not equal to $dataMap.bgm (also check $dataMap.autoplayBgm, then don't play the track from the other map. I'm not sure how to do this off the top of my head.

JavaScript:
const eqd_BGMGroup_isMaploaded = DataManager.isMapLoaded;
DataManager.isMapLoaded = function() {
  return !!$dataMap && this._bgmLoaded;
  eqd_BGMGroup_isMaploaded.call(this);
};
Side note: the version I wrote assumed no aliasing of isMapLoaded. With aliasing it should look like this:
JavaScript:
const eqd_BGMGroup_isMaploaded = DataManager.isMapLoaded;
DataManager.isMapLoaded = function() {
  return eqd_BGMGroup_isMaploaded.call(this) && this._bgmLoaded;
};
The way you wrote it is sort of "false" aliasing, in that it looks like you're aliasing it but it's actually just overwritten. The line eqd_BGMGroup_isMaploaded.call(this) is never executed in your version because the line above it returns from the function.

Original thread: https://forums.rpgmakerweb.com/threads/request-plugin-to-play-other-maps-bgm.123315/