MV Using a Variable to Determine # of foes to attack using YEP_TargetCore

As the title says... Is there a way to use TagetCore to use a variable to determine how many random foes will be attacked?

I assume it'll use the Lunatic Mode, but I'm not very good at that... Here is the Lunatic mode I think I'm supposed to use:
============================================================================
Lunatic Mode - Custom Target Eval
============================================================================

For users that are proficient with JavaScript, these notetags can be used to
make custom target selections. For the targeting aspect, this will refer to
the skill/item's current scope and the targets made here will be made
independent of the scope with the exception of the target, where if a target
is selected, it will become the 'target' value.

<Custom Target Eval>
for (var i = 0; i < foes.aliveMembers().length; ++i) {
var member = foes.aliveMembers();
if (member.level % 3 === 0) targets.push(member);
}
</Custom Target Eval>

The 'targets' variable is an array that determines which are the targets
to be used during the selection process. Push the desired targets into
the 'targets' array.

But, to my non-scripter mind, this looks like jibberish... Can you help me out please?
Try this...
Code:
<Custom Target Eval>
// Target X targets or every enemy, whichever is less
var numTargets=Math.min($gameVariables.value(X), foes.aliveMembers().length);
var rand;

// Go through the enemy party/troop...
do
{
// Choose a random enemy
    rand=Math.randomInt(foes.aliveMembers().length);

// If you're not already targeting that random enemy, add it to the list
    if (!targets.includes(foes.aliveMembers()[rand]))
        targets.push(foes.aliveMembers()[rand]);
} while (targets.length()<numTargets);
</Custom Target Eval>

You'll need to replace X with the ID of the variable you want to reference.
Try this...
Code:
<Custom Target Eval>
// Target X targets or every enemy, whichever is less
var numTargets=Math.min($gameVariables.value(X), foes.aliveMembers().length);
var rand;

// Go through the enemy party/troop...
do
{
// Choose a random enemy
    rand=Math.randomInt(foes.aliveMembers().length);

// If you're not already targeting that random enemy, add it to the list
    if (!targets.includes(foes.aliveMembers()[rand]))
        targets.push(foes.aliveMembers()[rand]);
} while (targets.length()<numTargets);
</Custom Target Eval>

You'll need to replace X with the ID of the variable you want to reference.
Thank you for the code, it works wonderfully... but one thing I'd like to fix about it. One of the things I like about Target Core is that I can set a skill to target 10 foes and it'll attack ten enemies EVEN if the enemy troop only has 4 enemies. Essentially the skill will cycle through the 4 enemies and randomly attack ten of them by attacking some enemies multiple times.

The skill I want to use this for needs to attack multiple times... Basically, the enemy steals gold from the heroes and attacks them with it. So if the enemy steals 10 gold, he has to be able to attack ten times and if there are only two heroes then that means the heroes will have to be attacked multiple times.

At the moment the above code only attacks as many times as there are targets. Of course, the target list has to be made up of Alive characters.

Again thanks for the help and fast response.
Then delete the line I labeled "If you're not already targeting..."
Then delete the line I labeled "If you're not already targeting..."
Do I delete the whole section of the code or just one line? I tried deleting the whole section and the skill doesn't target anyone now... And I don't know what line to delete and still make it work.
I wrote a comment that says "If you're not already targeting..." in English.

The next line is a line of code that starts with the word "if". Delete it.
I wrote a comment that says "If you're not already targeting..." in English.

The next line is a line of code that starts with the word "if". Delete it.
Did that and it targets again.... but it's still doing the min number of targets thing still. there are two actors and the skill is stealing 10 gold. The skill should attack 10 times, but it only attacks twice still. but it should be able to attack as much as the gold stolen [Variable].... so if the enemy steals 50 gold it should attack 50 times, as an example
Oh, sorry. Just do this:
Code:
<Custom Target Eval>
for (var i=0; i<$gameVariables.value(X); i++)
    targets.push(foes.aliveMembers()[Math.randomInt(foes.aliveMembers().length)]);
</Custom Target Eval>

where X is your variable ID.
Oh, sorry. Just do this:
Code:
<Custom Target Eval>
for (var i=0; i<$gameVariables.value(X); i++)
    targets.push(foes.aliveMembers()[Math.randomInt(foes.aliveMembers().length)]);
</Custom Target Eval>

where X is your variable ID.
Unfortunately, this doesn't work... ish.... The first time the enemy uses the attack nothing happens, the no targetting and no hitting, not even a miss. But the next time the enemy uses the attack the attack hits, but it has both the first set of attacks queued and the new set so it's like its doubled.

Just for reference this is the NoteTag for the skill:

<Custom Target Eval>
for (var i=0; i<$gameVariables.value(163); i++)
targets.push(foes.aliveMembers()[Math.randomInt(foes.aliveMembers().length)]);
</Custom Target Eval>

<setup action>
display action
motion Attack C: user
wait: 30
ACTION ANIMATION: all actors
WAIT FOR ANIMATION
wait: 10
animation 40: user
WAIT FOR ANIMATION
wait: 5
COMMON EVENT: 102
wait: 10
motion Attack C: user
wait for motion
CAMERA FOCUS: focus, CENTER, 10
ZOOM: 150%, 20
wait: 10
</setup action>
<target action>
motion Damage: user
projectile icon 634: start user, goal target, duration 7, spin 6
wait for projectile
action animation: target
animation wait: 5
action effect: target
</target action>
<finish action>
reset camera: 10
reset zoom: 10
wait for zoom
CLEAR BATTLE LOG
change variable 163 = 0
</finish action>
with the variable cleared at the end of the skill the same thing happens the next time it attacks, nothing....

Here is the Common Event that is called -
Code:
Control Variables: #164 Gold = Gold
Wait: 5 FRAMES
Script: $gameVariables.setValue(163, ($gameVariables.value(164) * 0.05));
Wait: 5 frames
Text: None, Window, Middle
      \autoGold stole \c[10]\i[901]\dg]\v[163]]\c[0] \G!
Change Gold: - {Gold Attack}
Wait: 5 frames
Script: $gameVariables.setValue(166, ($gameVariables.value(166) + $gameVariables.value(163)));
Wait: 5 frames
Control Variables: #164 Gold = 0
Wait: 5 frames


Or maybe it's just that all the damage is dealt at once instead of for each hit... I'll have to reflect on this one.
So there's kind of a lot wrong there, to my reading.
The first time the enemy uses the attack nothing happens, the no targetting and no hitting, not even a miss.
Well, of course. You're not giving variable 163 any value until the common event that happens after the skill. So the first time you try to use it, 163 is undefined and causes all math involving it to automatically fail.

Aside from that, here are things that are weird to me:
Code:
Control Variables: #164 Gold = Gold
Script: $gameVariables.setValue(163, ($gameVariables.value(164) * 0.05));
How much gold does your party have? And therefore what value is actually getting stored in variable 163? If it's a high number, you could essentially end up overloading your skill trying to queue up a ton of targets.

Also, there's a very good chance 163 is a decimal value (which won't affect the code I gave you, as i increments in integers, but it could be an undesired result).

Why are you using two variables for this at all?

Why are you using a script call for this?

This can easily be done using one variable and two event commands.
Control Variable -> 164 = Gold
Control Variable -> 164 Div 20

Why bother setting your variables to 0 at the end of everything?

I'm just not really following your logic.
So there's kind of a lot wrong there, to my reading.

Well, of course. You're not giving variable 163 any value until the common event that happens after the skill. So the first time you try to use it, 163 is undefined and causes all math involving it to automatically fail.
So what if I run the common event in the troop events on a turn-by-turn basis... so the variable is always loaded with the most up-to-date info? I'll experiment... Thanks.

Aside from that, here are things that are weird to me:

How much gold does your party have? And therefore what value is actually getting stored in variable 163? If it's a high number, you could essentially end up overloading your skill trying to queue up a ton of targets.
The script actually takes the amount of gold the party currently has [Variable 164] and multiplies it by 5% which is essentially dividing by 20, as you said, and then storing that information in variable 163. I then take that info from Variable 163 and ADD it to Variable 166 Since My monster's GOLD reward Value is controlled by that Variable.

Also, depending on the amount of GOLD the party has I want the monster to be a little on the harder side. The enemy is like a special boss, someone you don't have to fight. I'll have him appear in special areas throughout my game and the trick will always be... The enemy is easier if you broke.

And he's made of GOLD so he gives huge Gold Rewards...
Also, there's a very good chance 163 is a decimal value (which won't affect the code I gave you, as i increments in integers, but it could be an undesired result).
In my tests, the value always came back as an integer and never a decimal so I'm not too worried about that. If it does start giving decimals, I add Math.floor to the Script call... which usually fixes the problem.
Why are you using two variables for this at all?

Why are you using a script call for this?

This can easily be done using one variable and two event commands.
Control Variable -> 164 = Gold
Control Variable -> 164 Div 20
This is a good point... But really I have two skills that use the Party's GOLD Value. I have the other skill pull from Variable 165 while this skill pulls from Variable 163. They have different values because they have different formulas for each.

.Why I used a script call is because the Control Variable command can get cumbersome. I could use the Script option in the Control Variable Command, but that's basically the same as what I did here... I don't know, it's just how I've always done it.
Why bother setting your variables to 0 at the end of everything?

I'm just not really following your logic.
I set certain Variables to 0 at the end in order to start fresh when it's used again. I just don't want any leftovers messing with my numbers and such. The only variable changed to 0 is the Party's Gold Variable, so when the variable tries to find the value again it's not adding it to a value unless that value is 0... It's a paranoia thing... I guess. I just remember in earlier maker programs that I had issues with that and so I have kind of gotten into the habit of clearing the values like that.

Thank you so much for the help... I'll try something out and let you know how they work.


EDIT: With a little adjusting it works great.... for the most part. just got to fix a few hang-ups

EDIT2: Tweaks done.... it works exactly as I want. Thank you so much for the help.

Original thread: https://forums.rpgmakerweb.com/threads/using-a-variable-to-determine-of-foes-to-attack-using-yep_targetcore.149719/