Plugin to play SE automatically after variable change.

Sarry123

Villager
Member
Joined
Jun 5, 2020
Messages
10
Reaction score
3
First Language
French
Primarily Uses
RMMV
Hi, so, I am not really good at coding.
I am actually in the middle of creating a project and am making a relationship system.

That's why I decided to make a plugin that plays a sound effect upon variable change. I tried, but nothing works in the game.

I may be able to put a sound effect every time a variable changes in the editor.
But, I was wondering if there was an easier way to do so.

Is what I am asking for possible? Or should I stick with the editor for that purpose?

Here is my test plugin :
JavaScript:
/*:
* @plugindesc A plugin letting you put sound effects for variable changes.
* @author Sarry
*/

var check_change;

if (check_change != $gameVariables.value(1)) {
        $gameSystem.saveBgm();
    check_change = $gameVariables.value(1);
   
    }
(I put $gameSystem.saveBgm(); for testing.)

EDIT : So, I was able to do this using a parallel common event and it's working perfectly. But, I'm still interested in learning how to do this via javascript scripts/plugins.

Thanks.
 
Last edited:

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,415
Reaction score
669
First Language
Portuguese - Br
Primarily Uses
RMMV
Do you want the sound effect play when changing a specific variable Id? Or any variable?
 

V_Aero

Veteran
Veteran
Joined
Apr 23, 2019
Messages
277
Reaction score
255
First Language
german
Primarily Uses
RMMV
What I recommend you to learn first is aliasing methods. This technique is similar to overriding, but instead of overriding, you extend a function. So we extend Game_Variables.set(id, value) in a way, that it plays a sound.

Code:
var alias = Game_Variables.prototype.setValue;

Game_Variables.prototype.setValue = function(id, value) {
    if (id == 1 // <-- your desired id to listen on
        && value != $gameVariables.value(1)) // <-- use this line if you want to play the SE only if the new value is different
    {
        // play sound
    }
    
    // call original function
    alias.call(this, id, value);
}
I don't know all commands by heart, so it may contain spelling errors.

Use this link to figure out how to play a SE ;)
 

Sarry123

Villager
Member
Joined
Jun 5, 2020
Messages
10
Reaction score
3
First Language
French
Primarily Uses
RMMV
Thank you for your fast answer. It was actually pretty easy. I'll see what I can do with this.
Thanks again.
 

Sarry123

Villager
Member
Joined
Jun 5, 2020
Messages
10
Reaction score
3
First Language
French
Primarily Uses
RMMV
@Eliaquim Hi and yes, I wanted to make it for at least 5 variables and some with more conditions like if the variable's value decreased, a specific se plays, if it increased, another would play, etc...

I managed to make this with a common event for two variables.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
1,698
Reaction score
1,144
First Language
EN
Primarily Uses
RMMV
Don't worry too much about it, but in this case I think I'd go a little further. Ever been playing a game and suddenly a sound plays WAY louder than it should have? :kaoback: One way to help prevent that is to have the sound check happen only once per frame and use some kind of timer/counter to limit how frequently the sound can play, e.g. (untested):
JavaScript:
/*:
* @plugindesc Hi! I'm a plugin!
*/

(function() {   // to keep seTime, TIME, and SE private to this plugin
'use strict';

  let seTime = 0;
  const TIME = 30;   // play up to once per this many frames
  const SE = { name: "Coin", volume: 20, pitch: 100, pan: 0 };

  // After normal update stuff, check if seTime is equal to TIME and play sound if so
  (function(alias) {
    Game_Map.prototype.update = function() {
      alias.apply(this, arguments);
      if (seTime > 0) {
        if (seTime === TIME) AudioManager.playSe(SE);
        --seTime;   // count down by 1
      }
    };
  })(Game_Map.prototype.update);

  // If seTime is zero when a variable's value is changed, set it equal to TIME
  (function(alias) {
    Game_Variables.prototype.setValue = function(variableId, value) {
      let preVal = this.value(variableId);
      alias.apply(this, arguments);
      if (!seTime && preVal !== this.value(variableId)) seTime = TIME;
    };
  })(Game_Variables.prototype.setValue);

})();

Similar thread, for posterity~
 

Sarry123

Villager
Member
Joined
Jun 5, 2020
Messages
10
Reaction score
3
First Language
French
Primarily Uses
RMMV
Thanks for your help.
I'll see what I can do.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

When you realize @Kupotepo is a champion among RM Web users, and it all makes sense now:
Worst nightmare this morning, tried to get 20 minutes of work done on my project before heading to work and got hit with a POWER SURGE. Restarted my computer and the project was CORRUPTED, luckily I made a back up a few days ago so I only lost 4 days of work but still
Ami
what the other name of Elixir?

many games are use that,i want name it different.
What does your project folder look like?
I was told that an iPhone can provide a personal internet hotspot...You learn something new everyday!

Forum statistics

Threads
100,461
Messages
976,198
Members
132,082
Latest member
nwr
Top