RMMZ Best practice for use of common events vs pure javascript

Joined
Sep 18, 2022
Messages
3
Reaction score
0
First Language
English
Primarily Uses
RMMZ
I'm pretty new to RPG Maker so I was wondering what the veterans had to say about the following scenario. Basically, I'm implementing a system where when Character A does their normal attack, some special action will happen. My initial stab at it was the following:

  1. Modify Game_Actor.prototype.performAction to set a game variable to the subject name in the action.isAttack() clause.
  2. Create a common event and have it run whenever the Attack skill is invoked.
  3. The script in the common event will check the game variable set in #1 to see if it is Character A.
  4. If it is Character A, then run the special action.
It works, but I got to thinking, if I'm just running a script anyway, is there any downside in taking the script from the common event and just having it run during the action.isAttack() section of the performAction function? (also please let me know if my coding sucks, pretty new to JS also though have a lot of other coding experience)

To me, that seems like the right way to do it since it puts the logic in less places and is less complex (also, my understanding of common events is that they should be common ie. used in multiple places. This one is just for the one scenario), but not sure if there are any risks modifying the core scripts like that.

JavaScript:
    $.aliasPerformAction = Game_Actor.prototype.performAction;
    Game_Actor.prototype.performAction = function (action) {
        $.aliasPerformAction.call(this, action);

        if (action.isAttack()) {
            //setting battleAttacker to whoever is attacking
            $gameVariables.setValue(161, BattleManager._subject._name);
            console.log('person attacking: ' + BattleManager._subject._name);
        
            //**** Put common event script here instead of in common event?
        }
 
Last edited:

Arctica

Veteran
Veteran
Joined
Jul 19, 2021
Messages
214
Reaction score
324
First Language
----
Primarily Uses
N/A
If you can trim the fat for more speed and optimization, I don't see anything wrong with that, and as always if you can do what you want entirely in a common event, prefer that over a JS plugin.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
6,171
Reaction score
4,144
First Language
English
Primarily Uses
RMMV
Ultimately, you're the only one looking at/working on your project, so do it however you like. That said...
Modify Game_Actor.prototype.performAction to set a game variable to the subject name in the action.isAttack() clause.
You are doing this in a plugin file, correct? It's both bad practice and hazardous to do it directly in the core scripts (not hazardous like you'll lose an eye, but it can be overwritten if you update when MZ does).

if I'm just running a script anyway, is there any downside in taking the script from the common event and just having it run during the action.isAttack() section of the performAction function?
For my tastes, I want everything that can efficiently be in the editor to be there. Who knows how many little tweaks to individual skills/things you might end up creating? If they're all stuck away inside of your code, I know my brain wouldn't keep track.

At the very least, make good use of the Notes section to comment on your work. And if you're using a plugin that could replicate your work with a notetag (like VisuStella Battle Core), I think that's preferable.

To me, that seems like the right way to do it since it puts the logic in less places and is less complex
Those are fine things. Speaking to my point above, having everything that happens with a skill visible with that skill is also a way to have the logic in fewer places (code-wise, it really doesn't make a difference whether it's written into the core function or getting called from a notetag).

not sure if there are any risks modifying the core scripts like that.
As I said above, you should never do that - if MZ updates and you update your core scripts to take advantage of the changes, you'll lose your edits. Always put code into plugins.

if you can do what you want entirely in a common event, prefer that over a JS plugin.
I wouldn't say that's "best practice." It may be your personal preference, but there are many situations where something is achievable in a common event but could be done far more efficiently in JavaScript.

For me, it comes basically down to number of lines. If it's simple enough that the number of event commands is the same or fewer than how many lines of code I'd have to type, I'll use the built-in events. If it's more efficient to write the code, there's no reason to keep it in an event just for the sake of using events.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,402
Reaction score
5,090
First Language
English
Primarily Uses
RMMZ
As @ATT_Turan said, if you're going the plugin route absolutely 100% create a distinct plugin file and don't edit the core scripts directly so that you don't lose all your work when MZ updates.

Although the point made about keeping track of stuff in plugins vs in engine is a valid one and definitely one to think about, I'm potentially going to go against the grain here with my opinion: if you can code something directly, I think you should do so. Not only does it give you finer control over exactly when your desired effect is happening in relation to everything around it, it cuts out the middle man involved in running the editor's Script command, which calls the eval function with your script code as the argument. And I think overall it's almost always going to be better to just run that code on your terms.

Other plugins that offer notetags which can provide those effects are also an idea, but you'll never get a more fit-for-purpose, more efficient method of accomplishing your goal than bespoke code you wrote yourself specifically to do that thing and nothing else.
 
Joined
Sep 18, 2022
Messages
3
Reaction score
0
First Language
English
Primarily Uses
RMMZ
Thanks everyone for the detailed feedback!

You are doing this in a plugin file, correct?
Ah yes, I am. I should have been more specific to say I'm aliasing the core scripts and extending them via a plugin. Sorry, am still new to the terminology.

For my tastes, I want everything that can efficiently be in the editor to be there. Who knows how many little tweaks to individual skills/things you might end up creating? If they're all stuck away inside of your code, I know my brain wouldn't keep track.
That's a good point. Without the common event, from the RPG Maker GUI, I won't know what is actually running when attack is invoked. I like the idea of adding Notes. It seems notetags are a thing I will need to research also.

Although the point made about keeping track of stuff in plugins vs in engine is a valid one and definitely one to think about, I'm potentially going to go against the grain here with my opinion: if you can code something directly, I think you should do so.
This is also more appealing to me for the reasons you mentioned, but I wasn't sure if it broke some cardinal rule of rmmz scripting. It's good to know there are others out there who have done it this way. Also, I just realized I have your "Jump into Javascript" link open on another one of my tabs. Thanks for compiling all that!
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,402
Reaction score
5,090
First Language
English
Primarily Uses
RMMZ
Thanks everyone for the detailed feedback!


Ah yes, I am. I should have been more specific to say I'm aliasing the core scripts and extending them via a plugin. Sorry, am still new to the terminology.


That's a good point. Without the common event, from the RPG Maker GUI, I won't know what is actually running when attack is invoked. I like the idea of adding Notes. It seems notetags are a thing I will need to research also.


This is also more appealing to me for the reasons you mentioned, but I wasn't sure if it broke some cardinal rule of rmmz scripting. It's good to know there are others out there who have done it this way. Also, I just realized I have your "Jump into Javascript" link open on another one of my tabs. Thanks for compiling all that!
I really need to get the next part written...
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
6,171
Reaction score
4,144
First Language
English
Primarily Uses
RMMV
That's a good point. Without the common event, from the RPG Maker GUI, I won't know what is actually running when attack is invoked. I like the idea of adding Notes. It seems notetags are a thing I will need to research also.
If you're unfamiliar with notetags and what some of the options are, you might want to spend a bit more time exploring existing plugins before writing a lot of your own code.

It's usually better to use someone's code that's already been written than to spend your own limited time rehashing that ground. And worst case, even if you don't end up using a given plugin, you might learn more about how you can do things.

Note that I was specifically not suggesting it would be a good idea to keep the common event in your scenario.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,402
Reaction score
5,090
First Language
English
Primarily Uses
RMMZ
The other thing to keep in mind is that you can write comments in code as well, so there's nothing stopping you from documenting in your plugin what's being done where and what relates to the functionality of what would have otherwise been a common event.
 
Joined
Sep 18, 2022
Messages
3
Reaction score
0
First Language
English
Primarily Uses
RMMZ
True True to all the comments. I am all about not reinventing the wheel and also documenting code the best I can.
 

Latest Threads

Latest Posts

Latest Profile Posts

working on an A/V sync system for my game! was NOT expecting the first roadblock to be accessing a variable from a public class!! Computers are so finicky sometimes :kaosigh:
For the first real time since my game's inception I found myself actually having fun playing around with my party's skills while testing out some battles! So much so, that I actually forgot what I was doing for a minute and had to force myself to move on to the next thing! :guffaw:

That's got to be a good sign considering it only has place holder animations, enemy skills, troop comps, etc. right now. Feels good!
Bex
Finally upgraded to RMMZ.
ScreenShot_11_28_2022_7_52_34.png
FINALLY, I figured out how to get the "tail" to appear IN the window and not directly below it lol. now it looks like it's part of the window and not hanging from the bottom of it.
Twitch! Stream is about to go live with some Dark Souls 2! Feel free to drop by!

Forum statistics

Threads
126,925
Messages
1,182,404
Members
166,759
Latest member
Ninja777
Top