Triggering a Common Event whenever there is a change in a certain variable

KazukiT

Veteran
Veteran
Joined
Dec 7, 2014
Messages
306
Reaction score
396
First Language
English
Primarily Uses
RMMV
I was wondering if there was a plugin or script call that allows you to trigger a common event whenever there is a change with a certain variable. For example you gain 5 points for Variable 1 is there a way to trigger a common event whenever Variable 1 's value changes?
 

fizzly

Veteran
Veteran
Joined
Mar 23, 2012
Messages
802
Reaction score
516
First Language
Polish
Primarily Uses
RMMV
You can do this in every RPG Maker without any script or plugin.
Just make a common event called and set trigger as prallel (assigned switch must be ON), then make conditional branch with variable = 5, and it's done.

If you need to trigger common event every time the value changes, just make common event called "add value" and put value change operation and trigger common event XYZ (which is the one you want to trigger every time). Now, besides change variable option just use the common event you created to trigger variable change and an common event.
 

KazukiT

Veteran
Veteran
Joined
Dec 7, 2014
Messages
306
Reaction score
396
First Language
English
Primarily Uses
RMMV
You can do this in every RPG Maker without any script or plugin.
Just make a common event called and set trigger as prallel (assigned switch must be ON), then make conditional branch with variable = 5, and it's done.

If you need to trigger common event every time the value changes, just make common event called "add value" and put value change operation and trigger common event XYZ (which is the one you want to trigger every time). Now, besides change variable option just use the common event you created to trigger variable change and an common event.

I am not sure I quite understand, could you show a screenshot on how to set it up the common event any time a variable value changes.
 

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
954
Reaction score
526
First Language
Javascript
Primarily Uses
RMMZ
I was wondering if there was a plugin or script call that allows you to trigger a common event whenever there is a change with a certain variable.
There is now ;) See below :)
You can do this in every RPG Maker without any script or plugin.
True but a plugin would reduce extra processing and could be a bit easier ;)

...

Sooooooo anyways I made one for you. Tada! :)


Plugin Download:
CTB_OnChangeVariable.js: Click Here to Download

How to use:
Quite simple really. Just set up the list of Variable & Common Event Pairings :)
ddkqzj0-87b72b89-15e0-4130-985d-5d2a3b6a8c9c.png

Please let me know how it goes :)
Happy Game Making!
 
Last edited:

KazukiT

Veteran
Veteran
Joined
Dec 7, 2014
Messages
306
Reaction score
396
First Language
English
Primarily Uses
RMMV
There is now ;) See below :)
True but a plugin would reduce extra processing and could be a bit easier ;)

...

Sooooooo anyways I made one for you. Tada! :)


Plugin Download:
CTB_OnChangeVariable.js: Click Here to Download

How to use:
Quite simple really. Just set up the list of Variable & Common Event Pairings :)
ddkqzj0-87b72b89-15e0-4130-985d-5d2a3b6a8c9c.png

Please let me know how it goes :)
Happy Game Making!

This plugin makes the common event trigger when the variable value changes. However, when I go to check if the value changed the game says the value is 0 even though in the Event I added to that variable.
 

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
954
Reaction score
526
First Language
Javascript
Primarily Uses
RMMZ
Last edited:

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
41,143
Reaction score
14,205
First Language
English
Primarily Uses
RMMV
I would not use a parallel common event for this. I wouldn't even use a plugin.

Unless you have some plugin that changes the variable, you already know exactly when you need to call the common event, as you know when the variable is being changed, because there are only a few event commands that let you change a variable's value. So after you use those event commands, call the common event directly.
 

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
954
Reaction score
526
First Language
Javascript
Primarily Uses
RMMZ
Hi @Shaz! :cutesmile:

I wouldn't even use a plugin.
Just curious though... Why not a plugin?
My plugin does the same thing but reduces the extra step of making sure to call the common event every time the variable is changed.
(Aliased the function too so no need to worry about compatibility either)
Also as you mentioned also the added bonus of including the plugins that could be used for the changing of game variables.

Plus another perk of the plugin is if you want to change to a different common event ID in the future you wouldn't have to go to each place the game variable was changed to change it, you would only have to change the parameter in the plugin manager.

Again just curious why not a plugin, promise I'm not trying to be rude or anything. :popcorn: I fully respect your view on the matter. :)

Edit:
v1.1 Now Released (Features Script Call, and uses "forEach" insead of "for" to increase performance a bit ;))
 
Last edited:

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
41,143
Reaction score
14,205
First Language
English
Primarily Uses
RMMV
Don't get me wrong - I'm not saying a plugin is bad. It's certainly better than a parallel process solution. It's just unnecessary if the variable is only being changed by an event command.

If the variable only gets changed by a Control Variables command, then you know exactly when the variable will change, so you can do a Common Event command immediately after the Control Variable. You don't have to have something running all the time in the background keeping an eye out to see when a change has happened.

I can't view your plugin while I'm at work, but I'm guessing you've aliased a function somewhere that changes a variable, and in your aliased function, you call the original, then you check to see if the variable being changed is the one of interest, and then you trigger the common event. It's a lot better than checking X times every second, because it's only running the extra code when a variable is changed. But for every single variable that gets changed during the entire duration of the game, that plugin is checking to see "is THIS the one I need to do something special with?" If the variable is only going to be changed 5 times in the first 15 minutes of the game, and the game goes for 10 hours, that's also a lot of unnecessary checking that's happening. But it won't be a CPU drain like a parallel process would be, because you're being more specific about when / how often you're doing the check.

Note though, even though you have aliased and called the aliased function, it doesn't guarantee there'll be no compatibility issues. What if you had another plugin that also changes the same function but it didn't alias the function? It's not only your plugin that has to be set up just the right way, but every other plugin as well. And if one isn't, most people probably can't go in and fix that themselves.

In addition, a plugin requires the user to download and save it correctly, and to set up the parameters correctly. We've seen numerous instances where people have struggled with that. If your plugin uses parameters, either it has to be written so the file name doesn't matter, or it has to be saved with the exact, correct filename. And if they don't set up the parameters (which an awful lot of people don't) or don't set them up correctly, there are still issues.

I'm just a fan of not using parallel processes or plugins if there's not a real, valid reason to do so.

Now, in the case where the variable isn't being changed by an event, but by another plugin (let's say a variable is being set to a particular actor's weapon id, so is being set by a plugin that changes the Equip screen and equip commands), it's not the developer who's determining when the variable is being changed, but the player, and you can't predict that. In that case, a plugin like yours will be the most appropriate solution. And this is why I added the "unless" disclaimer to my original comment.

Out of interest, would your plugin work if the variable were being changed via a damage formula? I think v[n] is a direct link to $gameVariables._data, isn't it, and it bypasses the $gameVariables.setValue(id, value) function? So if that's the function you aliased, a change to a variable in a damage formula might not trigger it. I suspect that's not what the OP is doing, and even my solution won't help in that case.


Hope this answers your questions. I certainly didn't think you were being rude :)
 
Last edited:

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
954
Reaction score
526
First Language
Javascript
Primarily Uses
RMMZ
Don't get me wrong - I'm not saying a plugin is bad. It's certainly better than a parallel process solution. It's just unnecessary if the variable is only being changed by an event command.

If the variable only gets changed by a Control Variables command, then you know exactly when the variable will change, so you can do a Common Event command immediately after the Control Variable.
True but I would say one of the biggest advantages would be that the dev. not having to go into each place the variable is changed to change which common event will be used if the dev. decides to change it in the future. Also I have it evaluate what is in the plugin parameter on the fly of what variable & common event IDs are used so this could even be set to a game variable it's self to allow changing in-game even ;) So could even let the player pick what common events occur when the chosen variables are changed. (hope that made sense, it's like an inception lol)

I can't view your plugin while I'm at work, but I'm guessing you've aliased a function somewhere that changes a variable, and in your aliased function, you call the original, then you check to see if the variable being changed is the one of interest, and then you trigger the common event.
Quite correct that is what happens :) Only compares the list set in the parameters with the one changed though so quite a short list unless the game dev. sets a lot of variable/common event pairings.

But for every single variable that gets changed during the entire duration of the game, that plugin is checking to see "is THIS the one I need to do something special with?" If the variable is only going to be changed 5 times in the first 15 minutes of the game, and the game goes for 10 hours, that's also a lot of unnecessary checking that's happening. But it won't be a CPU drain like a parallel process would be, because you're being more specific about when / how often you're doing the check.
Hmm... I see what you are sayin', it does have to iterate through the list set in the plugin parameters
Generally speaking it would only be a few checks though as the user sets up but still does no matter what add just a bit extra to those variables that don't need to check... hmm... probably no way around that though huh
Note though, even though you have aliased and called the aliased function, it doesn't guarantee there'll be no compatibility issues. What if you had another plugin that also changes the same function but it didn't alias the function? It's not only your plugin that has to be set up just the right way, but every other plugin as well. And if one isn't, most people probably can't go in and fix that themselves.
Hmm... True I should put in the help section that mine should go at the very bottom for best compatibility...
that way no matter what all I do is alias what ever was last even it was an overwrite of the original.

In addition, a plugin requires the user to download and save it correctly, and to set up the parameters correctly. We've seen numerous
instances where people have struggled with that. If your plugin uses parameters, either it has to be written so the file name doesn't matter, or it has to be saved with the exact, correct filename.
Yeah I'll probably add that soon just in case people rename the file.
Edit:
v1.2 now allows the file to be any name :)

Now, in the case where the variable isn't being changed by an event, but by another plugin (let's say a variable is being set to a particular actor's weapon id, so is being set by a plugin that changes the Equip screen and equip commands), it's not the developer who's determining when the variable is being changed, but the player, and you can't predict that. In that case, a plugin like yours will be the most appropriate solution. And this is why I added the "unless" disclaimer to my original comment.
Yeah probably the biggest advantage and true use of such plugin.
Out of interest, would your plugin work if the variable were being changed via a damage formula? I think v[n] is a direct link to $gameVariables._data, isn't it, and it bypasses the $gameVariables.setValue(id, value) function? So if that's the function you aliased, a change to a variable in a damage formula might not trigger it. I suspect that's not what the OP is doing, and even my solution won't help in that case.
Good point. Checking into it now :) Thank you so much for all your insight :)
Edit:
You were correct it did not, but now I've added that in v1.2
Thanks again for the feedback :)

Hope this answers your questions.
Yes indeed. :)
I certainly didn't think you were being rude :)
Oh so glad to hear it. I kept re-reading it to make sure it didn't sound all snarky and such :guffaw:

Edit:
v1.2 Now Released :)
Added trigger by use of a variable in a damage formula (WIP)
Added ability to rename plugin file

@Shaz Thanks again :)
 
Last edited:

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
954
Reaction score
526
First Language
Javascript
Primarily Uses
RMMZ
Last edited:

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
857
First Language
English
Primarily Uses
RMMV
@Shaz @ct_bolt There is the possibility of reducing the code a bit further by replacing the '_data' property with a Proxy object. The '_data' property will continue to act as an array, and you can execute whatever code is required as the array is being written to. There would be no need to alias 'evalDamageFormula' since you have complete control over the array.

Unfortunately ... I have not found a way to properly serialize the Proxy object to a save file. By default the Proxy object outputs the array stored in the '__data' property during the save process. So once the saved data is loaded the '_data' variable returns to a normal array. It was an interesting exercise, at any rate. I just thought I would plop what I stumbled across here.

Code:
Game_Variables.prototype.clear = function() {
  this.__data = [];
  this._data = new Proxy(this.__data, {

    // Getter
    get: function(data, id) {
      return data[id];
    },

    // Setter
    set: function(data, id, value) {
      if (data[id] !== value) {
        // Execute Common Event
      }

      return data[id] = value;
    }

  });
};
 

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
954
Reaction score
526
First Language
Javascript
Primarily Uses
RMMZ
@Shaz @ct_bolt There is the possibility of reducing the code a bit further by replacing the '_data' property with a Proxy object. The '_data' property will continue to act as an array, and you can execute whatever code is required as the array is being written to. There would be no need to alias 'evalDamageFormula' since you have complete control over the array.

Unfortunately ... I have not found a way to properly serialize the Proxy object to a save file. By default the Proxy object outputs the array stored in the '__data' property during the save process. So once the saved data is loaded the '_data' variable returns to a normal array. It was an interesting exercise, at any rate. I just thought I would plop what I stumbled across here.

Code:
Game_Variables.prototype.clear = function() {
  this.__data = [];
  this._data = new Proxy(this.__data, {

    // Getter
    get: function(data, id) {
      return data[id];
    },

    // Setter
    set: function(data, id, value) {
      if (data[id] !== value) {
        // Execute Common Event
      }

      return data[id] = value;
    }

  });
};
I did actually have a similar thought. I may attempt that soon. ;):thumbsup-right:
 
Last edited:

Latest Threads

Latest Profile Posts

AeroPergold wrote on LUKElcs's profile.
Its-a me, Pergold from Gamejolt!
I added footstep sounds to our content that I posted in a previous thread. I was unable to edit the post, so if you're interested, see below.

DOWNLOAD LINK

I've started working on rpg maker again, and I can see a lot of improvement from when I was 13. I still have a long way to go, but I'm proud that I've gotten this far.
Animated Title screen is by SharkerrBlue(SB)
I am using the Nightmare Land pack.
I just can't walk to the store for lunch without walking past people smoking. It not only smells bad, it's probably poisoning me. I'm afraid I'll eventually have to be hospitalized. I can't take another path because there's too much traffic and drivers in the plaza are reckless sometimes. I almost got run over once, even after looking both ways.
RyanYe wrote on whtdragon's profile.
Your art works about Wyrvens and Dragons are sooooooooo cooooooooool~! I like them so bad
Can you create more? like Archangels, Devils, Titans, Cyclops!!

Forum statistics

Threads
108,811
Messages
1,039,658
Members
141,258
Latest member
Faxion
Top