Using item directly from event

Ack! Yea, I think it's because of how the "can use" thing is checked (or rather, not checked) in the case of no targets. I tested this time: this version definitely works for me with a "Scope: None" item. Feel free to add the animation stuff back in~
Code:
(function(v,i,t,a,c,u) {
  if (i = $dataItems[+v || 0]) {
    u = $gameParty.members()[0];
    a = new Game_Action(u);
    a.setItemObject(i);
    t = i.scope === 7 ? [u] : a.makeTargets();
    for (n in t) c = c || a.testApply(t[n]);
    if (u.canUse(i) && (i.scope === 0 || c)) {
      u.useItem(i);
      for (n in t) for (var m = a.numRepeats(); m--;) a.apply(t[n]);
      a.applyGlobal();
}}})(YOUR_ITEM_ID_HERE);
Basically, I just added a scope check to this line, so scope 0 ("None") will ignore the apply test:
Code:
    if (u.canUse(i) && (i.scope === 0 || c)) {

Hi all! I'm having some trouble getting common events to activate when items are used by the script above.

From this thread: https://forums.rpgmakerweb.com/index.php?threads/how-to-use-a-item-in-eventing.112339/

commonevent.png


This is my event, which is supposed to pop up a selection of the available Items in the player's inventory. Once one's selected it's supposed to be immediately used and the common event starts. When I run this as-is... nothing happens. I select the item and the event ends. If I use the same Item from the inventory, it works just fine. Any ideas?

Extra note: I am using a lot of custom plugins, so I tried disabling them, and it doesn't seem to fix anything. I even recreated it in a brand-new project and it still did not work, so I feel like it can't be plugin-related...
Worked fine for me with scope "none" then when I added a scope of any kind it made it not work.
Basically by you adding that line your making it not work.

Not sure that condition statement code is actually doing what your intending it to do.

..but umm yeah this code works for me (using game variable 50 to store the key item):
Spoiler
Code:
(function(v,i,t,a,c,u) {
  if (i = $dataItems[+v || 0]) {
    u = $gameParty.members()[0];
    a = new Game_Action(u);
    a.setItemObject(i);
    t = i.scope === 7 ? [u] : a.makeTargets();
    for (n in t) c = c || a.testApply(t[n]);
      u.useItem(i);
      for (n in t) for (var m = a.numRepeats(); m--;) a.apply(t[n]);
      a.applyGlobal();
}})($gameVariables.value(50));

That code works for you right?
If so then what else needs to be done to properly suit you needs? ...I ask since you are trying to achieve something else with that line of code you added?
Worked fine for me with scope "none" then when I added a scope of any kind it made it not work.
Basically by you adding that line your making it not work.

Not sure that condition statement code is actually doing what your intending it to do.

..but umm yeah this code works for me (using game variable 50 to store the key item):
Spoiler
Code:
(function(v,i,t,a,c,u) {
  if (i = $dataItems[+v || 0]) {
    u = $gameParty.members()[0];
    a = new Game_Action(u);
    a.setItemObject(i);
    t = i.scope === 7 ? [u] : a.makeTargets();
    for (n in t) c = c || a.testApply(t[n]);
      u.useItem(i);
      for (n in t) for (var m = a.numRepeats(); m--;) a.apply(t[n]);
      a.applyGlobal();
}})($gameVariables.value(50));

That code works for you right?
If so then what else needs to be done to properly suit you needs? ...I ask since you are trying to achieve something else with that line of code you added?

This does not work for me 👋I have my item set to "none" scope as well. And if I change it to have a specific item ID instead of a variable, it still doesn't work... (but I did triple check to make sure it was getting the correct variable from Select Item; it is)

Edit (Again): I don't understand how or why, but apparently it's the MVCommons plugin that's causing the problem? When I disable that one, it works. Any ideas on what the conflict there could be?
Edit again: I made another whole brand new project and tried your code and it works, even though the other one didn't 😅 But I still have no idea why it won't work in my own. I'm using quite a few YEP plugins including ItemCore, ItemCategories, and ItemPictureImg, and although it doesn't work when they're turned off, could something still be causing an issue?
Hmm.. your item looks good

...and you've tested with every single plugin off correct?

... I just made it into a small plugin if ya wanna test this now instead..
works for me.. honestly not sure if it will be any different for you though or not

1663306718743.png

Script Call:
JavaScript:
$gameParty.useItemMV(memberId, itemId)
Script Call Example:
JavaScript:
$gameParty.useItemMV(1, $gameVariables.value(50))

CTB_UseItemMV Plugin: Click Here to Download
Hmm.. your item looks good

...and you've tested with every single plugin off correct?

... I just made it into a small plugin if ya wanna test this now instead..
works for me.. honestly not sure if it will be any different for you though or not

View attachment 240546
Script Call:
JavaScript:
$gameParty.useItemMV(memberId, itemId)
Script Call Example:
JavaScript:
$gameParty.useItemMV(1, $gameVariables.value(50))

CTB_UseItemMV Plugin: Click Here to Download
Your plugin works great without MVCommons on (Thank you 😢but it still does not work with MVCommons on. I tried checking the Console Trace log and the error just says TypeError: target.isDead is not a function... So clearly it's the problem but I'm not sure why.

EDIT: I'm still not sure what the problem was, but it's solved! I searched all over the forums for that error and found this on another thread:
As I mentioned before, if the array is empty then testApply should not be being invoked at all. Perhaps the "empty" array has 1+ non-standard enumerable properties added to it? Try replacing this line:

for (n in t) c = c || a.testApply(t[n]);

...with one of these:
  1. For testing:

    for (n in t) console.log('target', n, 'is:', t[n]);

  2. As an alternative:

    for (n of t) c = c || a.testApply(n);

    If that works, then you should also replace this line:

    for (n in t) for (var m = a.numRepeats(); m--;) a.apply(t[n]);

    ...with this:

    for (n of t) for (var m = a.numRepeats(); m--;) a.apply(n);

Technical details about the difference between for...in and for...of:
Otherwise I think I'm out of ideas for now. 😴
And changing my lines to this fixed it!

The final result that is working perfectly now:

(function(v,i,t,a,c,u) { if (i = $dataItems[+v || 0]) { u = $gameParty.members()[0]; a = new Game_Action(u); a.setItemObject(i); t = i.scope === 7 ? [u] : a.makeTargets(); for (n of t) c = c || a.testApply(n); if (u.canUse(i) && (i.scope === 0 || c)) { u.useItem(i); for (n of t) for (var m = a.numRepeats(); m--;) a.apply(n); a.applyGlobal(); }}})($gameVariables.value(15));

Thank you for your help 😍Looks like this one was on me for not thinking that MVCommons would be a problem... since it says it's meant to improve compatibility 😅
Your plugin works great without MVCommons on (Thank you 😢but it still does not work with MVCommons on. I tried checking the Console Trace log and the error just says TypeError: target.isDead is not a function... So clearly it's the problem but I'm not sure why.

EDIT: I'm still not sure what the problem was, but it's solved! I searched all over the forums for that error and found this on another thread:

And changing my lines to this fixed it!

The final result that is working perfectly now:

(function(v,i,t,a,c,u) { if (i = $dataItems[+v || 0]) { u = $gameParty.members()[0]; a = new Game_Action(u); a.setItemObject(i); t = i.scope === 7 ? [u] : a.makeTargets(); for (n of t) c = c || a.testApply(n); if (u.canUse(i) && (i.scope === 0 || c)) { u.useItem(i); for (n of t) for (var m = a.numRepeats(); m--;) a.apply(n); a.applyGlobal(); }}})($gameVariables.value(15));

Thank you for your help 😍Looks like this one was on me for not thinking that MVCommons would be a problem... since it says it's meant to improve compatibility 😅
No prob. Happy to help (or at least try lol)

lol
Well at least it works now. Glad to hear it's solved.

Hmm.. ok cool that's good to know.. guess it looks like instead of t[n] it's better to just use n

Happy Game Making! ☺️
Yea, some plugins add members to existing objects, which then get picked up by for...in. So then when this script call loops over the targets, some of them might be values like numbers or something...which don't have an isDead method. Best to use for...of (or a simple for loop with integer indices) for iterating arrays.

I've edited that info to my old post for anyone else who encounters a similar error~

Original thread: https://forums.rpgmakerweb.com/threads/using-item-directly-from-event.151321/