MV Need Help Using YEP_MapSelectSkill

I'm currently working on some on-map utility skills Golden Sun style. I already got the meat of the skill to work. As in, if I go into the main menu -> skills -> Geokinesis, I can make the boulder move as intended. The problem is, I don't want players to have to go through all those menus every time they want to cast one of these on-map skills. I already have both YEP_MapSelectSkill & YEP_ButtonCommonEvents installed. I put the plugin command MapSelectSkill 9 1 2 into one of my Common Events, which is bound to Spacebar (for now). I understand that YEP_MapSelectSkill simply saves a skillId into a variable. But I'm not sure what to do w/ that variable after it's set. I simply want the game to use the skill as if the player had gone through all the menus w/o actually going thru the motions. Any ideas?
What are you trying to get the map select skill function to do?

Are you trying to make it bind to your use key when selecting a skill?
If that's the that case you could have one common event bound to your key of choice, and have it check the variable set by the map select skill. For example if you had 3 spells to choose from, the event would check if variable 1, 2, or 3 is active using conditional branches, and inside each branch put the appropriate skill.

But If you're trying to straight up bind skills to a set key then I don't see a need for the map select skill at all, it could be done straight through button common events (or mano input plugin). And you would build the skills in separate common events.

An example of a skill.
Let's say Geokinesis costs 5MP.
- Set a variable to the game data of your actor's current MP.
- Conditional branch if that variable is equal to or greater than 5.
- If so, subtract 5MP from the actor and run the skill + any animations
Perhaps this video will explain my situation more clearly.


Ignore the cat meows, it's for testing purposes lol...

Edit:
For example if you had 3 spells to choose from, the event would check if variable 1, 2, or 3 is active using conditional branches, and inside each branch put the appropriate skill.
This is the part I'm having trouble with. Let's say I have a skillID saved to a variable from the MapSelectSkill plugin. I don't know what to put in a Common Event in order to cast/use the skill. I get that I'm supposed to reference the variable set by the MapSelectSkill plugin, but I can't find any command to actually use said skill.
I see what you mean, I think the best way to do this is to recreate what the skill does inside the common event. Does your skill currently work by running a common event? If so then a structure like this should work.

1656975654144.png

You just need to make sure anything the skill does is included, such as sound effects or animations. This is recreating the skill instead of telling the game to run it directly.
Hmm, I guess that'll have to do. Of course, having the game directly execute the skill would be preferable, but if it works, it works. I guess there really isn't a "use skill" command, huh? Unfortunate, but I'll make do, for now. Thanks!

Edit: Wait...shouldn't there be a way to use a skill (which is basically an item) with the useItem() function?
No, you need to construct and apply a new action based on the item/skill. Check out this thread:
Notably the end result (mostly Aesica's work):
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)) {
You can swap $dataItems for $dataSkills, I think everything else should be OK as-is?
No, you need to construct and apply a new action based on the item/skill. Check out this thread:
Notably the end result (mostly Aesica's work):

You can swap $dataItems for $dataSkills, I think everything else should be OK as-is?
Hmm, I'm getting a "target.isDead" is not a function error. This is what my common event looks like now upon using the suggested code:

1657036758447.png


I didn't change anything other than $dataItems -> $dataSkills and also YOUR_ITEM_ID_HERE -> $gameVariables.value(9).

1657036669896.png


Edit: I disabled the YEP_OptimizeScriptCalls plugin, which seems to have fixed the error message. However, the skill doesn't actually execute. I added a Sound Effect just to make sure the Common Event was being invoked, which it was. The skill works when I use it normally from the menu, but nothing happens if I use it via this Common Event...

Edit2: Nvm, disabling YEP_OptimizeScriptCalls didn't make the error go away. It simply made it so the game doesn't crash. But in the console, the same error persists.

Edit3: I also tried using the original code (for Items) and tested it using a Health Potion. But I still get an error, albeit a different one.
1657039206081.png
That's odd for a couple of reasons:
  1. makeTargets ought to return an empty array for actions with Scope: None. The script call invokes testApply once per target, which should be 0 times for a skill with Scope: None.

  2. By default actors and enemies both inherit from Game_BattlerBase, therefore have the isDead method. Apparently Game_Action#makeTargets is returning something different for you, though.
I'm guessing one or more of your plugins drastically changes how actions are processed (LeTBS, right?). In that case you'll probably need a script call specifically for the plugin(s) you're using.

For testing purposes you could add a bunch of console logs before the testApply line, e.g.

(function(v,i,t,a,c,u,n) { if (i = $dataSkills[+v || 0]) { console.log('using skill ID:', v); u = $gameParty.members()[0]; console.log('skill user:', u); a = new Game_Action(u); a.setItemObject(i); t = i.scope === 7 ? [u] : a.makeTargets(); console.log('targets:', t); 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(); }}})($gameVariables.value(9));
Then check the console to see if the values match what you expect.
That's odd for a couple of reasons:
  1. makeTargets ought to return an empty array for actions with Scope: None. The script call invokes testApply once per target, which should be 0 times for a skill with Scope: None.

  2. By default actors and enemies both inherit from Game_BattlerBase, therefore have the isDead method. Apparently Game_Action#makeTargets is returning something different for you, though.
I'm guessing one or more of your plugins drastically changes how actions are processed (LeTBS, right?). In that case you'll probably need a script call specifically for the plugin(s) you're using.

For testing purposes you could add a bunch of console logs before the testApply line, e.g.

The makeTargets() function is indeed returning an empty array (due to the scope being "None"), so that's all well and good right? LeTBS doesn't actually change anything in Game_Action. I know for sure that isDead() works elsewhere since I've used it several times before w/o incident. Here's the result of the console logs:
1657039475364.png


Edit: I also disabled all my LeTBS plugins and the error persists.
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. 😴
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);
That fixed it! Thank you!!

P.S. I was also able to trace the original issue to LeUtilities.js, which is a required plugin for LeTBS. This is the console logs in regards to that:
1657042707998.png


Edit: So basically, LeUtilities.js was adding extra enumerable properties? And due to this, the original code that was posted was reading those when we didn't want it to, correct? The issue is resolved, but I just wanted to learn what I can from this experience.
Great! 😂

Yes, for...in iterates over all enumerable properties. By contrast, for...of references the object's iterator generator function to determine the iterated values, technical details here:
If you simply add a new property to an object, e.g. myObj.test = 123, that new property will be enumerable. Object.defineProperty can be used to specify a non-enumerable property. Details here:
I'm curious, why was for...in chosen over for...of in the original code? Like, what advantage does it have in that use case that warrants for...in over for...of?
I just copied Aesica's structure; my guess is that they were unaware of for...of, or just forgot it exists (I can relate).

As far as I'm aware, for...of is a better option whenever you're doing any sort of "normal" loop over an object's values. for...in can be useful if you want to use the key names as well as the values, but that's not necessary in this case.

For arrays, another option is to use a basic for loop, from 0 to length - 1. 👋

Original thread: https://forums.rpgmakerweb.com/threads/need-help-using-yep_mapselectskill.149178/