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,419
Reaction score
673
First Language
Portuguese - Br
Primarily Uses
RMMV
Do you want the sound effect play when changing a specific variable Id? Or any variable?
 

Aerosys

Veteran
Veteran
Joined
Apr 23, 2019
Messages
285
Reaction score
262
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,712
Reaction score
1,162
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 Profile Posts

Been seeing bright flashes in the sky for a while, and thought it was my eyes playing tricks on me. Nope! Lighting. Now it's raining heavily. Seeing lightning, but not hearing thunder, is weird.
Small Update for myself:
>Two Dungeon maps are done, Dungeon 3 is in progress. Dungeon 4 needs work.
>4 characters set up out of a possible 6 but those come later.
>Hub area needs work.
>Game completion..., 20-25% maybe?
Does anyone else find writing dialogue hard?
You guys want more tutorials? Well not only are we doing new video tutorials with Driftwood, we've also brought in two contributing authors for the blog to get a new tutorial every week! First one here from @Avery
https://blog.rpgmakerweb.com/tutorials/tutorial-mapping-interior/

Next week we'll get something from @hiddenone
Husband has roped me into playing one of his PSO2 characters. Productivity is now negative.

Forum statistics

Threads
100,712
Messages
978,623
Members
132,325
Latest member
ighjku
Top