MZ Need help with making using items a free action.

Hello.

I'd like to make it so when an actor uses an item, it doesn't use up their turn, but it allows the player to pick one more action.
The battle system is set to turn based, and I want the item to be used when the actor is going to act, not instantly when the player picks it.
If the player chooses to use more than one item on that actor's turn, it's okay for them to stack as many as they want.

I tried reading RPG maker's scripts but I'm having trouble figuring out where to add that logic.
So I'm asking here, in case someone more experienced can point me to the right direction.

Thank you in advance.
Generally speaking editing the core scripts themselves is bad practise: edits made that way are harder to track, are less modular/portable (e.g. for testing/sharing), and are lost if/when you update your project's core scripts.

Instead you can use a plugin to modify appropriate parts of the core scripts. 👋

I've moved this thread to Plugin Requests (from MZ Support). Thank you.

In case no-one knows of an existing plugin for this, here's some untested code:
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Free-action item use in battle.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/170628/
 * @help Free to use and/or modify for any project, no credit required.
 */
// Patch - initialise "free action" count (for cancel).
void (alias => {
  Game_Actor.prototype.initMembers = function() {
    alias.apply(this, arguments);
    this._freeActs = 0;
  };
})(Game_Actor.prototype.initMembers);

// Patch - reset "free action" count (for cancel).
void (alias => {
  Game_Actor.prototype.clearActions = function() {
    alias.apply(this, arguments);
    this._freeActs = 0;
  };
})(Game_Actor.prototype.clearActions);

// Patch - add extra action when appropriate.
void (alias => {
  Scene_Battle.prototype.selectNextCommand = function() {
    if (DataManager.isItem(BattleManager.inputtingAction().item())) {
      const a = BattleManager.actor();
      ++a._freeActs;
      a._actions.push(new Game_Action(this));
    }
    alias.apply(this, arguments);
  };
})(Scene_Battle.prototype.selectNextCommand);

// Patch - remove extra action when appropriate.
void (alias => {
  Scene_Battle.prototype.selectPreviousCommand = function() {
    const a = BattleManager.actor();
    if (a._freeActs > 0) {
      --a._freeActs;
      a._actions.pop();
    }
    alias.apply(this, arguments);
  };
})(Scene_Battle.prototype.selectPreviousCommand);
(I imagine it won't work as-is, but it might be a useful starting point.)
Generally speaking editing the core scripts themselves is bad practise: edits made that way are harder to track, are less modular/portable (e.g. for testing/sharing), and are lost if/when you update your project's core scripts.

Instead you can use a plugin to modify appropriate parts of the core scripts. 👋
Moved to: Plugin Requests (from MZ Support) In case no-one knows of an existing plugin for this, here's some untested code:
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Free-action item use in battle.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/170628/
 * @help Free to use and/or modify for any project, no credit required.
 */
// Patch - initialise "free action" count (for cancel).
void (alias => {
  Game_Actor.prototype.initMembers = function() {
    alias.apply(this, arguments);
    this._freeActs = 0;
  };
})(Game_Actor.prototype.initMembers);

// Patch - reset "free action" count (for cancel).
void (alias => {
  Game_Actor.prototype.clearActions = function() {
    alias.apply(this, arguments);
    this._freeActs = 0;
  };
})(Game_Actor.prototype.clearActions);

// Patch - add extra action when appropriate.
void (alias => {
  Scene_Battle.prototype.selectNextCommand = function() {
    if (DataManager.isItem(BattleManager.inputtingAction().item())) {
      const a = BattleManager.actor();
      ++a._freeActs;
      a._actions.push(new Game_Action(this));
    }
    alias.apply(this, arguments);
  };
})(Scene_Battle.prototype.selectNextCommand);

// Patch - remove extra action when appropriate.
void (alias => {
  Scene_Battle.prototype.selectPreviousCommand = function() {
    const a = BattleManager.actor();
    if (a._freeActs > 0) {
      --a._freeActs;
      a._actions.pop();
    }
    alias.apply(this, arguments);
  };
})(Scene_Battle.prototype.selectPreviousCommand);
(I imagine it won't work as-is, but it might be a useful starting point.)

Replace

Spoiler: old
JavaScript:
// Patch - add extra action when appropriate.
void (alias => {
  Scene_Battle.prototype.selectNextCommand = function() {
    if (DataManager.isItem(BattleManager.inputtingAction().item())) {
      const a = BattleManager.actor();
      ++a._freeActs;
      a._actions.push(new Game_Action(this)); //this here refers to Scene_Battle, which should be actor
    }
    alias.apply(this, arguments);
  };
})(Scene_Battle.prototype.selectNextCommand);


With


Spoiler: update
JavaScript:
    // Patch - add extra action when appropriate.
    void (alias => {
        Scene_Battle.prototype.selectNextCommand = function () {
            const action = BattleManager.inputtingAction();
            if (action && DataManager.isItem(action.item())) { //prevent potential runtime error
                const a = BattleManager.actor();
                if (a) {
                    ++a._freeActs;
                    a._actions.push(new Game_Action(a));
                }
            }
            alias.apply(this, arguments);
        };
    })(Scene_Battle.prototype.selectNextCommand);


And it works (kinda) 🙂
Sorry it took me a few days to reply.

Thanks for the help, I tried the code and it seems to be doing the basic thing I want it to.

To be clear, I wasn't planning on editing the core scripts directly.
I'm just reading them to try and understand how they work.

I'm now reading the code you gave me, for the same reason.
Do you mind explaining to me, or refer me to somewhere that does, what the "alias" part of the code does, how it works, and why I should use it?
So far, I was just copy-pasting what I wanted to change from the core scripts to a plugin so I could overwrite it.
Yay!

Do you mind explaining to me, or refer me to somewhere that does, what the "alias" part of the code does, how it works, and why I should use it?
OK, first: (alias => { /* ... */ })(123); creates an anonymous function with a single argument, alias, then immediately invokes that function, passing the value 123 in as the first argument. This is known as an "immediately-invoked function expression", or IIFE:
So this:
JavaScript:
void (alias => {
  Game_Actor.prototype.clearActions = function() {
    alias.apply(this, arguments);
    this._freeActs = 0;
  };
})(Game_Actor.prototype.clearActions);
Is mechanically equivalent to this:
JavaScript:
// Use an IIFE to avoid polluting the global namespace.
void (() => {
  // Store current `clearActions` method in a local variable named `alias`.
  const alias = Game_Actor.prototype.clearActions;
  // Redefine (i.e. override) the `clearActions` method.
  Game_Actor.prototype.clearActions = function() {
    // Call the stored method in the current context with the current arguments.
    // I.e. "do the usual thing".
    alias.apply(this, arguments);
    // Then set a new value on `this` actor.
    this._freeActs = 0;
  };
})();
Basically, it lets you add stuff before and/or after the existing code of that function: a "patch", rather than just a full override.

The void thing is just personal preference; I do it mainly because it acts as a hint for JavaScript's automatic semicolon insertion, in case a semicolon has been forgotten on the previous line.
Oh, okay. I was using IIFEs but like this:
JavaScript:
(() => {
  // …
})();

Just to make sure I understand, is this:
JavaScript:
(alias => {
})(Game_Actor.prototype.clearActions);
equivalent to this:
JavaScript:
(() => {
    const alias = Game_Actor.prototype.clearActions;
})();

Also, is using the alias.apply(this, arguments); "method" better for any other reason other than saving space, when compared to the copy-pasting "method" I've been using?

And, by the way, is this the right place for these questions or am I getting off topic?
Just to make sure I understand, is this:
equivalent to this:
That's the exact example caethyril provided for you above 😛 So yes.

Also, is using the alias.apply(this, arguments); "method" better for any other reason other than saving space, when compared to the copy-pasting "method" I've been using?
They do two different things.

When you copy and paste the existing code, then make your modifications, it will do exactly that. If you add any other plugins and they're written that way, only one of them can possibly function.

When you alias the function, you're making it do what it's already doing, then adding onto it (before or after). That means if you have someone else's plugin that modifies it, you will use their modifications also and be adding onto that, and you don't need to know what they did.

That's impossible to achieve if you're copying, pasting, and overwriting the function.

It's always preferable to alias when possible. The only time you really need to overwrite is when the code you're inserting has to go in the middle of what's existing (or, of course, if you're entirely changing the function and deliberately don't want any of what it originally did).
Also:
And, by the way, is this the right place for these questions or am I getting off topic?
I think it's OK. You're asking to figure out exactly what the code you have been given actually does.

If you want to ask questions like this more generally, e.g. if you find some code posted in an old thread and you want help understanding how it works, then consider starting a thread in Learning JavaScript:

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/need-help-with-making-using-items-a-free-action.170628/