How do I call variables added by plugins out of scope?

I'm noob so sorry if you don't understand my question but I have the target core plugin and use the variable target and such. I would like to be able to use the target.friendsUnit() var in other places like the in the custom requirement section of my code. Right now I'm just pushing results to the game variables but it seems like extra steps?
Like, Skill Core's <Custom Requirement> tag? That supports use of user, you could do:
Code:
user.opponentsUnit()
you need to be more specific in your question.

Basically in a plugin that has been programmed in a professional way the variables can't be used out of scope because they don't exist out of scope. But often that scope is bigger than you think, allowing the variables to be used in specific places where they exist.

So there is no general answer, but if you specify where you want to use what then someone can tell you if it is possible and how to do it.
Like, Skill Core's <Custom Requirement> tag? That supports use of user, you could do:
Code:
user.opponentsUnit()
Yes this! How do you know this? It doesn't say that on the skills core page.

you need to be more specific in your question.

Basically in a plugin that has been programmed in a professional way the variables can't be used out of scope because they don't exist out of scope. But often that scope is bigger than you think, allowing the variables to be used in specific places where they exist.

So there is no general answer, but if you specify where you want to use what then someone can tell you if it is possible and how to do it.

I'm using only Yanflys plugins ATM because I trust he/she? programs in a professional way. To be more specific with what I'm trying to do, I'm making skills for enemies like this one:
Code:
<Custom Target Eval>

for (var i = 0; i < target.friendsUnit().aliveMembers().length; ++i) {

var member = target.friendsUnit().aliveMembers()[i];

if (member.hp < 50) targets.push(member);

$gameVariables.setValue(8, [targets.length]);

}

</Custom Target Eval>

<damage formula>

var dmg = $gameVariables.value(8);

value = 200 / dmg;

</damage formula>

<Custom Requirement>

for (var i = 0; i < $gameTroop.aliveMembers().length; ++i)

if ($gameTroop.aliveMembers()[i].hp < 50) {

$gameVariables.setValue(8, [i]);

value = true;

} else {

value = false;

$gameVariables.setValue(8, 0);

}

<\Custom Requirement>
I guess my question is am I going about this the right way by using game variables. Its mostly working but sometimes the game variable I'm using stays 0 and no one does the spell until I hit an enemy under the 50 hp threshold.

Also I have the rmmv scrip call list and plugin command list for Yanfly but how do I find all the functions and variables plugins are using?

Edit: using $gameTroop works but if I could use target.friendsUnit in the requirements sections the player could also use the skill
How do you think someone else knows?
I mean, there aren't many solutions: you know either by reading the code or by trying it and seeing if it works.

Trying it is easy and requires no understanding other than knowledge of the console.log script call.
Boot up the console, get your target object, and see if the friendsUnit call works.
Hell, do a console.log inside the target eval to know it. Quick, easy, 5 seconds to test in battleTest and you'll have your answer. That's the easy way: It gets you no knowledge about the general case. It justs gets the job done, quick and dirty.

The hard way is by reading the code, and understanding the context of the function call.
Friends unit can be found in the game action class. In there you can see it calls subject.friendsUnit(), that means friendsUnit is actually a function from the subject itself. Subject will either be an actor or enemy object so you go look into there.
Lo and behold, friendsUnit returns $gameParty for instances of game actor, and $gameTroop for instances of game_enemy.
There you have it. You can use friendsUnit on actors or enemies and you know what it will return.
It took me a grand time of 10 seconds searching rpg_objects to find this. As easy as ctrl-f and pasting friendsUnit. Hell, it took me longer to type this than to find this info.

Using script calls without understanding why they work is just a recipe for disaster, and an exercise in frustration. Do yourself a favor, and search the core files before writing a script call, just so you know what you're actually doing.

I'd actually advise also a quick search of relevant plugins you use to see if they're not modifying the behavior of the core scripts for that particular function. This is done easily by using the console.log script call and telling it to return to you the actual function (by omitting the brackets and parameters), it will then tell you in which file, and which line to look, so you can understand what you're dealing with.
Yea, I just searched through Skill Core's code for the relevant notetag's eval and verified that the user variable is defined there.

It'd be a bit odd to have a function for friendsUnit and not have a counterpart for the opposing side, so I just went to Game_Actor.prototype.friendsUnit in rpg_objects.js and hunted around a little...voila, opponentsUnit. 🙏

Problem solved? 😅

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-call-variables-added-by-plugins-out-of-scope.110385/