MV Can I have an item note-tag trigger a plugin command

In my Clock & Calendar plugin, I have a survival mode. I am taking this mode out of the base plugin, and make it an ad-on plugin. This all went well.

In this survival mode, you get hungry when you don't eat, and thirsty when you don't drink. Also tired when you don't sleep, but that doesn't work with items.

The way it works now:
  • Your food items should trigger a common event and that common event would trigger a plugin command. This resets your hunger.
  • Your drink items should trigger a common event and that common event would trigger a plugin command. This resets your thirst.
Instead I want to give items an item tag like <food> and <drink>, which would trigger the reset in the plugin, instead of a plugin command, when the items is consumed.

I think I look for the wrong terms, because I can't find how to trigger notetags in a plugin anywhere. I only get plugins specific results.
I'd suggest you patch an appropriate method and check the item for a relevant notetag there, e.g.
JavaScript:
;void (function() {
'use strict';
  const alias = Game_Action.prototype.apply;
  Game_Action.prototype.apply = function(target) {
    alias.apply(this, arguments);
    const meta = this.item().meta;
    if (meta.food) {
      alert(target.name() + " ate some food.");
    }
  };
})();
A plugin command is meant to be run from an event interpreter, but using an item is not something that processes as an event. However, if the "plugin command" is just a function that doesn't expect any particular context then you could just call that from wherever.
I'd suggest you patch an appropriate method and check the item for a relevant notetag there, e.g.
JavaScript:
;void (function() {
'use strict';
  const alias = Game_Action.prototype.apply;
  Game_Action.prototype.apply = function(target) {
    alias.apply(this, arguments);
    const meta = this.item().meta;
    if (meta.food) {
      alert(target.name() + " ate some food.");
    }
  };
})();
A plugin command is meant to be run from an event interpreter, but using an item is not something that processes as an event.
Yeah, the title was chosen poorly, I meant instead of a plugin command.

I'll check the code out in a minute. Seems like it is exactly what I am looking for. Thank you.

However, if the "plugin command" is just a function that doesn't expect any particular context then you could just call that from wherever.
Okay, cool. I'll look into how to make/use functions correctly. 😛 (I'm such a noob when it comes to javascript Luckily on the global stuff, there is more than enough material to figure most things out.)

Thanks for the suggestion.
No worries, it doesn't need to be a function; depending on how your plugin is designed, it might just be a single-line script call, e.g.
JavaScript:
target.hunger = 0;
Good luck!
No worries, it doesn't need to be a function; depending on how your plugin is designed, it might just be a single-line script call, e.g.
JavaScript:
target.hunger = 0;
Good luck!
Okay... how to reset the hunger/thirst is not the issue. But how do I trigger it? 😛

I've been breaking my head over your earlier code, but I can't seem to figure it out... I'm even at the point where I am about to reverse to plugin commands. At least that I was able to do right. 😛

Thanks for your help though.
Okay... how to reset the hunger/thirst is not the issue. But how do I trigger it? 😛

I've been breaking my head over your earlier code, but I can't seem to figure it out...
It's an alias of the Game_Action.prototype.apply method from the core files. Whenever that method gets called by the game (you used an item on someone) it runs with an additional script to check if there is a <food> tag in the notebox. You replace the alert(target.name() + " ate some food."); line with your reset hunger value script
It's an alias of the Game_Action.prototype.apply method from the core files. Whenever that method gets called by the game (you used an item on someone) it runs with an additional script to check if there is a <item> tag in the notebox. You replace the alert(target.name() + " ate some food."); line with your reset hunger value script
I know what it is supposed to do. I just don't get it to do what it is supposed to do.

I did the same thing with transfers. I made an alias, and now when the player transfers, it runs a script. That works just fine, but I can't seen to get it right for the items with notetags.

I put it on hold for now. Tubbing over this for too long will make me lose interest in the project.
Okay... how to reset the hunger/thirst is not the issue. But how do I trigger it? 😛
Well I don't know exactly what line(s) of code you're using to "reset the hunger/thirst", but basically you should just need to put that code inside the if branch, e.g.
JavaScript:
;void (function() {
'use strict';
  const alias = Game_Action.prototype.apply;
  Game_Action.prototype.apply = function(target) {
    alias.apply(this, arguments);
    const meta = this.item().meta;
    if (meta.food) {  // the Item/Skill used has a <food> notetag
      target.hunger = 0;  // reset `hunger` property of action target to 0
    }
  };
})();
If you want to use the notetags as e.g. <food: 20>, you can do something like this:
JavaScript:
;void (function() {
'use strict';
  const alias = Game_Action.prototype.apply;
  Game_Action.prototype.apply = function(target) {
    alias.apply(this, arguments);
    const meta = this.item().meta;
    // Get food value (string -> integer).
    const food = "food" in meta ? parseInt(meta.food, 10) : 0;
    // If the Item/Skill used has a positive <food> value, subtract that from target's hunger.
    if (food > 0) {
      target.hunger = Math.max(0, target.hunger - food);  // min 0
    }
  };
})();
[Edit - made a correction in the second example: food in meta -> "food" in meta.]
Well I don't know exactly what line(s) of code you're using to "reset the hunger/thirst", but basically you should just need to put that code inside the if branch, e.g.
Haha. Uhm, yeah it seems like I don't get my question asked the right way... I keep getting replies that are like the opposite of what I tried to get. 😛

What goes inside this IF branch works just fine. It is just a simple variable being edited and states being changed. But I can't figure out where in my plugin to put the script you gave me.

The same thing happens when the player does a transfer, that works fine. Also with an alias of some kind.

I assume the location of where to put the code is important. And I think I did put them in the wrong place. Like, everything else is inside one large fuction, so I tried to do your code in this function too, or below it, or above it.

I am really a complete javascript noob. I'm willing to try and experiment, but when it turns out too complicated, I just switch back to the point where it all worked. 😛 Because it did all work, using common events with a plugin command.
I would just link the item to the common event which calls the plugin command.
as why overcomplicate things if it could be easy?
I would just link the item to the common event which calls the plugin command.
as why overcomplicate things if it could be easy?
Well, I'd be okay with that. But if I distribute the plugin, my audience might not be. 😛 I of course want to share a nice as possible product. 😛

In about 20 minutes I'll be home and I can try again with the help of @caethyril 's latest suggestions.

Also, Caethyril. I hope it doesn't come across as if your help isn't good enough or something. That I can't get it to work, is 100% because of my noobiness. 😛 Your help is much appreciated.
the code is important. And I think I did put them in the wrong place. Like, everything else is inside one large fuction, so I tried to do your code in this function too, or below it, or above it.
I guess I'm not seeing what's so complicated here. You put it somewhere on its own either before all your code or after. Don't put it inside another function.
I guess I'm not seeing what's so complicated here.
Like I said, I'm a complete noob. I know literally only of javascript what I needed to learn to make script calls. 😛

Now I try taking the step to plugins, but that is a bit more complicated. So far I am able to make most of what I want work the way I want it, accept for this. Well, this and MZ plugin commands. 😛 I don't understand those either. 😛

You put it somewhere on its own either before all your code or after. Don't put it inside another function.
I did try the first piece of code inside, before and after the other function, without success. I'm going to try again. 😛

I made a quick video, showing what I did... Maybe you guys see what I did wrong.

(Oops, didn't mean to have the stream layout there. 😛 This thread including some usernames is in the video for a second, but the video is uploaded privately and only shared here. I'll remove it as soon as it isn't needed anymore.)
No worries, totally OK! 👋

I don't see any obvious mistakes in your video. Thoughts:
  1. Maybe it's just conflicting with another event that sets the same variable.
    • Tools -> Event Searcher might help to track down potential conflicts.
    • You could also try changing the variable ID to see if that changes anything.
  2. Maybe there's a syntax error or similar elsewhere in your plugin.
    • If an error occurs while loading a plugin, the entire plugin will be ignored.
    • You can check the Console tab of the dev tools window (F8 or F12) for any error messages.
  3. If the item has Scope: None, it will never apply to any targets.
    • In that case you could try replacing both instances of Game_Action.prototype.apply with Game_Action.prototype.applyGlobal.
I've attached a plugin that works for me (tested with Scope: 1 Ally). It's basically the same code as what I suggested earlier, I just added a plugin header section and changed the if branch:
JavaScript:
    if (meta.food) {
      // +1 to game variable ID 1.
      $gameVariables.setValue(1, $gameVariables.value(1) + 1);
      // Show a message to prove it triggered.
      // Important because maybe the variable gets set elsewhere.
      alert(target.name() + " ate some " + this.item().name);
    }
When using an Item with <food> in its Note field, this causes variable 1 to increment, and also shows an "alert" message box for testing purposes.
Cool.
I had target set to none, so that might be it.

Edit: yes, the scope being set to none was the problem. Omg. 😛

I tried EVERYTHING with that code. 😛 Turns out it was the scope.

Its kind of a bummer that it needs a scope though, but on the other hand, I can now give each party member their own hunger/thirst... Maybe... It would bring a new challenge with it: Giving each party member their own timer. 😛 But that is probably something I can figure out with arrays or something.

Anyhow, thanks for the help!
Great, you're welcome!

You could add a patch for applyGlobal specifically to handle no-scopes, if you like. E.g.
Spoiler
JavaScript:
;void (function() {
'use strict';
  const alias = Game_Action.prototype.applyGlobal;
  Game_Action.prototype.applyGlobal = function() {
    alias.apply(this, arguments);
    const item = this.item();
    if (item.scope === 0) {  // only for Scope: None
      // Your code here, e.g.
      if (item.meta.food) {
        alert("The party ate some " + item.name);
      }
    }
  };
})();
applyGlobal is usually just for the Common Event effect: it always runs once per action.
Thanks. However, after some puzzling, I figured out how to give each character their own times, and have all the timers (or time stamps) stored in a single array.

Like:
JavaScript:
$gameVariables.value(32)[member.actorId] = Date.now();

Before it was so that the whole party shared the hunger, thirst and fatique states. Having a scope did complicate the whole process at first, it also allows for individual states for individual characters.

The only reason I did not want a scope at first, is because I had no idea how to give each character their own timer. 😛

Original thread: https://forums.rpgmakerweb.com/threads/can-i-have-an-item-note-tag-trigger-a-plugin-command.170062/