MZ How can a plugin command (as skill effects) access the skill subject and targets?

Updated:

I'm new to plugin development. I am writing some feature as plugin commands. The commands are expected to be used as Skill's effect. And it will modify the attributes of the actors involved (either the subject or the targets).

I've registered the commands and set the skill effects. However, when the command was run, I have problem accessing either the subject or the targets of the skill in the command callback function. Following the suggestion of ATT_Turan, I tried accessing BattleManager._subject and BattleManager._targets in the callback, but _subject was null and _targets was [].

Please advise what I could do. Thanks.


Old Question:

I'm new to plugin development. My questions are:
  1. How to use variables in plugins?
  2. How to have a skill setting the skill user's actor ID to a variable?
Thanks.
I know there's a language difference, but can you explain in a bit more detail what you're trying to do? I'm finding the questions vague, so I'm not sure how to answer.

How to use variables in plugins?
Your thread subject is talking about plugin commands, so is that what you're asking here?

In your code, you can declare and use variables normally. Do you mean a game variable from RPG Maker?

Those are referenced with the function $gameVariables.value(ID) - so if you have the player inputting a variable ID with the plugin command, you might reference $gameVariables.value(arg0)

How to have a skill setting the skill user's actor ID to a variable?
This one really doesn't make sense - you can't set an actor ID, that's defined by the actor's database entry. Do you mean the opposite, setting a variable to the actor's ID? Again, do you mean a variable in your code or a game variable?

How are you referencing the user of the skill? Generally speaking, you can do something like
Code:
$gameVariables.setValue(ID, battler.actorId())
but how you get battler depends on the context of your code.
Sorry about the confusion. It might not be relevant to plugin commands directly. Basically, I need a way to expose my plugin functionality as plugin command. And to have the command useful, I need to able to:
  1. Get the skill user's actor ID.
  2. Somehow pass the actor ID to the plugin command.
  3. Then modify the actor's parameter(s).
So I think I need to:
  1. Create a side-effect for a skill to set its user's actor ID to RMMZ's variable.
  2. Then I need to have the plugin command gain knowledge to the variable id.
  3. When the plugin command knows which variable id to look for, I need to load the actual RMMZ variable (from the given variable id) in my plugin code.
  4. Then I can get the actor ID from the RMMZ variable and do the rest.
I don't actually know if the approach was right. But my problem is:
  • I don't know how to have a skill side effect on variable. Is it do-able in the RMMZ GUI (e.g. skill effect)? Or do I have to code some event listener to do it?
  • How do I interact with RMMZ variables in my plugin code?
A bit complicated. Thanks for reading. Please help if you know the answer(s).

P.S. If I got it correct, I can access RMMZ variable with something like this, right?


Code:
$gameVariables.getValue(ID)
I don't mean at all to be insulting, but are you sure you need to be writing a plugin to do this? And you're ready to do so?

Everyone who knows how to write code started learning at the beginning, and it takes time and practice to get good at it.

But some of your questions sound like you don't understand how to read RPG Maker's code, which means you really can't write your own code to modify it. And some of them sound like basic coding knowledge, like how to declare a variable and pass it as an argument into a function.

If you can describe exactly what you're trying to accomplish in the game, we might be able to recommend an existing plugin or some other way to do it.

  1. Get the skill user's actor ID.
  2. Somehow pass the actor ID to the plugin command.
  3. Then modify the actor's parameter(s).
If your code is getting executing during a skill, you can just look at BattleManager._subject

So their ID, presuming it's an actor, would be BattleManager._subject.actorId()

So I think I need to:
Create a side-effect for a skill to set its user's actor ID to RMMZ's variable.
No need, the current actor is globally stored as I mentioned above. Also, why would you need to use a game variable?

Then I need to have the plugin command gain knowledge to the variable id.
Again, why? If you did need to put something into a variable, you just pass that variable into your actual function. For example:
Code:
 PluginManager.registerCommand(pluginName, nameOfTheCommand , args => {
        const arg0 = Number(args.value1 );
        const arg1 = Number(args.value2 );
        const ID = BattleManager._subject.actorId();
        myFunction(arg0, arg1, ID);
    });

When the plugin command knows which variable id to look for, I need to load the actual RMMZ variable (from the given variable id) in my plugin code.
Then I can get the actor ID from the RMMZ variable and do the rest.
As above, this is ignoring the basic JavaScript functionality of passing arguments into a function. You can use the first piece of code I gave you to reference the current actor from anywhere, no need to store and track it.

But even if it were some other information, you'd use actual JavaScript variables, not use up game variables.

I don't know how to have a skill side effect on variable. Is it do-able in the RMMZ GUI (e.g. skill effect)? Or do I have to code some event listener to do it?
Sorry, I don't understand what this part is asking.

How do I interact with RMMZ variables in my plugin code?
I gave you both of the relevant functions in my first reply.

P.S. If I got it correct, I can access RMMZ variable with something like this, right?
Code:
$gameVariables.getValue(ID)
No, those are not the words that I typed for you twice in my first reply. There is value() and setValue(), no getValue().

Good luck with your project!
I've just started with RMMZ yesterday. And I mainly drilled into the Game_Actor system. Haven't really read the BattleManager code. Thanks for pointing me to it.

The command needs to take actor ID because I want to repeatedly use the command on different spells with different effect. Sometimes the same command needs to target the spell target. Sometimes the command needs to target the spell caster.

I am writing this as a plugin because I might use it on different project. Keeping the code DRY as a plugin helps. Also it's a fun way to understand RMMZ.

Again, I'm very new to RMMZ. Doing this helps me understanding the code base better. Thanks for giving direction to study.
Are you referring to RPG Maker's "Variables" or "variable" in terms of JavaScript / programming in general?
Are you referring to RPG Maker's "Variables" or "variable" in terms of JavaScript / programming in general?

I am referring to RPG Maker's "Variables", not JavaScript variables in general.
The command needs to take actor ID because I want to repeatedly use the command on different spells with different effect. Sometimes the same command needs to target the spell target. Sometimes the command needs to target the spell caster.
I feel there is probably an existing/better way to do what you want using existing plugins or code from the default engine. But without knowing what you're trying to accomplish, we can't really make any good recommendations. And, of course, if you just want to do it for learning how to do it, that's fine.

I am referring to RPG Maker's "variables".
There's still very little reason you would want to do that. Unless you're publishing your plugin for other people to use, and this variable stores some kind of data that the user wants to reference in other places, you wouldn't want to take up one of the game variables for that.

You'd store the information in one of your code objects, or even declare a new global variable if it came down to that.
Moved to: Learning Javascript
If your code is getting executing during a skill, you can just look at BattleManager._subject

So their ID, presuming it's an actor, would be BattleManager._subject.actorId()
I've tried this approach but it didn't work.

I added my plugin command to one of the spells as an effect. The effect seems to run only after the spell was resolved. So the BattleManager._subject had become null before plugin command callback was called. Same goes to BattleManager._targets.

I might need to override the BattleManager methods to have it work properly.
To store extra environment variables I overridden a few of the Game_Temp methods:

JavaScript:
/**
 * Modify Game_Temp common event queue behaviour
 * to also store environment variables.
 */
function modGameTemp_commonEventAddEnv() {
    // Add an additional queue to store battle environment
    // variable for the reserved events.
    Game_Temp.prototype._commonEventEnvQueue = [];
    // A side-effect variable to setup for common event execution.
    let commonEventEnv = null;
    const originalReserveCommonEvent = Game_Temp.prototype.reserveCommonEvent;
    Game_Temp.prototype.reserveCommonEvent = function (commonEventId) {
        originalReserveCommonEvent.call(this, commonEventId);

        // -------------------------------------------------------------------
        // construct env variable for use.
        // Note: currently assuming this is a battle environment.
        // Note: I only concern actors for my use. You can do whatever you like.
        const eventEnv = {
            commonEventId: commonEventId,
            subject: null,
            targets: [],
        };
        if (BattleManager._subject && BattleManager._subject.constructor === Game_Actor) {
            eventEnv.subject = BattleManager._subject.actorId();
        }
        // If any of the event targets are game actors, store the actor IDs.
        eventEnv.targets = BattleManager._targets.filter(target => target.constructor === Game_Actor).map(target => target.actorId());
        // eventEnv done ---------------------------------------------------

        // Store the environment.
        this._commonEventEnvQueue.push(eventEnv);
    }
    const originalRetrieveCommonEvent = Game_Temp.prototype.retrieveCommonEvent;
    Game_Temp.prototype.retrieveCommonEvent = function () {
        const commonEvent = originalRetrieveCommonEvent.call(this);
        // Get the environment for the comment event.
        // Assuming originalRetrieveCommonEvent.call would also
        // shift the _commonEventQueue.
        commonEventEnv = this._commonEventEnvQueue.shift();
        // Pass the commonEvent out for execution.
        return commonEvent;
    }
    const originalClearCommonEventReservation = Game_Temp.prototype.clearCommonEventReservation;
    Game_Temp.prototype.clearCommonEventReservation = function () {
        originalClearCommonEventReservation.call(this);
        this._commonEventEnvQueue.length = 0; // clear the environment array along with it.
    }
    // Add a extra method to retrieve common event environment.
    Game_Temp.prototype.getCommonEventEnv = function () {
        return commonEventEnv;
    }
}

// Run the mod.
modGameTemp_commonEventAddEnv();

Then, when needed, I can get the environment variables stored by calling $gameTemp.getCommonEventEnv(). I can get the variables in my plugin command callback then:
JavaScript:
PluginManager.registerCommand(pluginName, nameOfTheCommand , args => {
    const envVars = $gameTemp.getCommonEventEnv();
    const subject = (envVars.subject) ? $gameActors.actor(envVars.subject) : null;
    const targets = envVars.targets.map(target =>$gameActors.actor(target));
    ...
    ...
});

Hope this can help somebody in the same situation.

Original thread: https://forums.rpgmakerweb.com/threads/how-can-a-plugin-command-as-skill-effects-access-the-skill-subject-and-targets.155868/