Expanding "Skill that make user cast random skill" thread

Hello !
I wasn't sure how to put it in the title so here is my situation :
Using YEP's multiple plugins, I am making a specific state on a battler that creates fruits on apply. Once the battler is hit, I created a Respond effect which deploys one, casting a skill onto an ally.

This part works fine, now my issue is the selection of the fruit to deploy :
There are 5 different kind of fruits that are produced when the state is applied, and each kind can be stacked up to 5 max.
So for now my state has a <Custom Respond Effect> that randomly chooses a skill according to which fruit is active on the battler (I'm aware it's clunky but I found the thread mentionned in the title after doing my tests) :

Code:
<Custom Respond Effect>
if (this.isHpEffect && value > 0)
{
$gameActors.actor(7)._Deploy = $gameActors.actor(7)._Deploy || 0;
var fruitv = $gameActors.actor(7).getStateCounter(271) || 0;
if (fruitv >= 1)
$gameActors.actor(7)._Deploy += 1;
var fruitc = $gameActors.actor(7).getStateCounter(272) || 0;
if (fruitc >= 1)
$gameActors.actor(7)._Deploy += 1;
var fruitpur = $gameActors.actor(7).getStateCounter(273) || 0;
if (fruitpur >= 1)
$gameActors.actor(7)._Deploy += 1;
var fruitpro = $gameActors.actor(7).getStateCounter(274) || 0;
if (fruitpro >= 1)
$gameActors.actor(7)._Deploy += 1;
var fruitb = $gameActors.actor(7).getStateCounter(275) || 0;
if (fruitb >= 1)
$gameActors.actor(7)._Deploy += 1;
var deploy = $gameActors.actor(7)._Deploy.clamp(0, 5);
var fruitskill = Math.randomInt(deploy);
var fruitdeploy = 258 + fruitskill;
BattleManager.queueForceAction($gameActors.actor(28), fruitdeploy, target);
}
</Custom Respond Effect>

The thing I was wondering about is when there are more than 1 fruit of a kind, could I change the odds of it being selected for the skill ?

The "Skill that make user cast random skill" thread had a code which seems to be the thing I want to achieve :

Code:
var possibleSkills = [8, 9, 10]; // IDs of the skills that might be cast when you use the random spell skill
var index = Math.randomInt(possibleSkills.length);
var skillId = possibleSkills[index];
BattleManager._subject.forceAction(skillId, -2);

I'm asking for help since I'm not sure how to include multiple instances of the same skill in the "possibleSkills" variable here :aswt2:
you could start with an empty array and instead of incrementing that deploy property after each check, you push the corresponding skill's id into the array through a loop and pull a random element from there at the end to determine the skill.
your states are sequential too, so a nested loop setup where the outer one is (int i = 271; i < 276; i++) and the inner one is (int j = 1; $gameActors.actor(7).getStateCounter(i) >= j; j++) would be easy to set up
Alright, I did some research on nested loop since I'm not good with Javascript (mainly I use things said here in the forum when it corresponds to what I need), now I have some issue with my battler learning skills attached to this mechanic :

The actor learns skills 253 and 254 from the start. Skills 255, 256 and 257 are also a part of that mechanic but learned later by the actor. These skills are attached to the states above, like this : skill 253 => state 271.
Then let's assume there are counters only on state 271, the <Custom Respond Effect> is supposed to activate only skill 258.
- Skill 253 produces 2 fruits A on the actor;
- The state 271 is associated with the fruit A, so it has 2 counters on it;
- When the actor is hit, activate skill 258 which is the effect of fruit A on an ally;
so my logic on your idea @Robro33 was to script it like this :

Code:
<Custom Respond Effect>
if (this.isHpEffect && value > 0)
{
var fruit = 0 ;
var fruitodd = 0 ;
if (user.isLearnedSkill(255))
var fruit += 1 ;
if (user.isLearnedSkill(256))
var fruit += 1 ;
if (user.isLearnedSkill(257))
var fruit += 1 ;
var skills = 260 + fruit ;
var fruitstate = 273 + fruit ;
for(var i = 271 ; i < fruitstate; i++){    //returns the state we will check the counter on
    for(var j = 0 ; $gameActors.actor(7).getStateCounter(i) = j; j++){    //returns the counter on the state
        for(var k = 258 ; k < skills ; k++){    //returns the skill associated with the state
var fruitodd += j ; //sum up all the counters that will be checked for the skill pool
}
}
}
//This is the part that I'm not sure about : what do I check to execute the correct skill ?

BattleManager.queueForceAction($gameActors.actor(28), fruitdeploy, -2);
}
</Custom Respond Effect>
now I have some issue with my battler learning skills attached to this mechanic
shouldnt be an issue. assuming those are sequential, you can just put an IF condition in the loop for if the actor knows it by adding an offset to the looping variable


Alright, I did some research on nested loop since I'm not good with Javascript (mainly I use things said here in the forum when it corresponds to what I need)
Id suggest searching for a js tutorial that starts from the beginning instead of just searching for what you just need. picking up code from the forums is fine, but you need to know the basics if youre trying to understand it or looking to make changes

var fruit = 0 ; var fruitodd = 0 ; if (user.isLearnedSkill(255)) var fruit += 1 ; if (user.isLearnedSkill(256)) var fruit += 1 ; if (user.isLearnedSkill(257)) var fruit += 1 ;
you generally shouldnt declare the same variable multiple times like that. for JS var declarations it wouldnt produce an error, but it will be a problem for other declaration types

//returns the state we will check the counter on
A bit of semantics, but youre not actually returning anything with any of these loops. I understood what you meant, but what you meant isnt what you said, and what said is not whats happening.

for(var j = 0 ; $gameActors.actor(7).getStateCounter(i) = j; j++){ //returns the counter on the state
thats a syntax and a logical error here. The syntax one is that you start a loop and gave it a condition, but the condition is an assignment statement. it wouldnt run because that's not a comparison. the equals to comparison is ==, not =
the logical one is that the initial loop wouldnt start unless the state counter is 0, and if the state counter was 0, it will only ever run once and then end. if you initialize j to be 0 and then increment it after every iteration, even if you started with the state counter at 0, the next iteration's condition would be false. its a roundabout way of saying "if ($gameActors.actor(7).getStateCounter(i) == 0)" in this case

//This is the part that I'm not sure about : what do I check to execute the correct skill ?
this is why i mentioned the empty array and pushing skill IDs into it. if thats done, then it means you have a list of usable skills and by pulling a random ID out of it, the results would be weighed based on the number of times each one shows up in it.


theres a few discrepancies, but based on the given description it should look like this.
Code:
<custom react effect>
var skill = []; //empty array
for (var i = 271; i < 276; i++) //start loop for all the applicable states
    if (target.isLearnedSkill(i-18)) //only continue if the actor knows the base skill
        for (var j = 0; j < target.getStateCounter(i); j++) //loop based on the state counter
            skill.push(i-13); //push the triggered skill's id into the array

skill = skill[Math.randomInt(skill.length)]; //get random ID from the array;
BattleManager.queueForceAction($gameActors.actor(28), skill, -2); //use random skill
</custom react effect>
I changed "user" for target because in a react tag, the battler getting hit is the target. the user is the one doing the attacking.
im assuming that actor 7 is the only actor that can have this state? you checked if the "user" knows the skills at the start and it sounds like theyre the only one who gets them, so im assuming its the case. you can just use target to reference them
shouldnt be an issue. assuming those are sequential, you can just put an IF condition in the loop for if the actor knows it by adding an offset to the looping variable
just before reading that I was looking into arrays and found out about the .push function 😅

Id suggest searching for a js tutorial that starts from the beginning instead of just searching for what you just need. picking up code from the forums is fine, but you need to know the basics if youre trying to understand it or looking to make changes
I understand, I ended up putting a lot of plugins on my project so I often go for threads about them first. But here it was more of a vanilla RMMV thing which is specific enough to not rely on base engine. Aside from that, my full time job forces me to work punctually on my RMMV project, so I don't find the time to learn Javascript...
the equals to comparison is ==, not =
...so I tend to even forget things I already used on other things like that 😅

you generally shouldnt declare the same variable multiple times like that. for JS var declarations it wouldnt produce an error, but it will be a problem for other declaration types
At times when declaring a variable with a "+=" it seemed to not work so I thought this could prevent an error, ironically

this is why i mentioned the empty array and pushing skill IDs into it. if thats done, then it means you have a list of usable skills and by pulling a random ID out of it, the results would be weighed based on the number of times each one shows up in it.
Oh, didn't look at this angle (or maybe translating in my head make me thought about a variable instead), that would have make me do otherwise maybe

im assuming that actor 7 is the only actor that can have this state?
Yup, since I use YEP's STB and Instant Cast plugins I use many other actors as workarounds to Force Actions some skills and ended up avoiding using "user" or "target" when I wasn't sure about the attacker and defender on some states effects
Code:
Code:
<custom react effect>
var skill = []; //empty array
for (var i = 271; i < 276; i++) //start loop for all the applicable states
    if (target.isLearnedSkill(i-18)) //only continue if the actor knows the base skill
        for (var j = 0; j < target.getStateCounter(i); j++) //loop based on the state counter
            skill.push(i-13); //push the triggered skill's id into the array

skill = skill[Math.randomInt(skill.length)]; //get random ID from the array;
BattleManager.queueForceAction($gameActors.actor(28), skill, -2); //use random skill
</custom react effect>
Oh, so a same skill can be added into an array ? like arr1 = [ 258 , 258 , ... ] ? I assumed it could only be inserted once, my bad :facepalm: didn't heard about it from the little digging I did

Anyway thank you, I'll try this code ^^
At times when declaring a variable with a "+=" it seemed to not work so I thought this could prevent an error, ironically
theres a difference between declaration and assignment, and its usually one of the first lessons in a tutorial if you get around to it. it doesnt have to be spending hours at a time. it can just be reading a lesson page and trying an example or 2. https://www.w3schools.com/js/js_variables.asp

var example; is a declaration of a variable called example
example = 4; is assignment of example (well... JS also automatically declares it if it hadnt been already, but theres reasons why should declare manually anyways)
var example = 4; is declaration and assignment of example at one time

that += operator takes the value of the thing to the left and and reassigns it to its own value plus the value on the right to it. its a shorthand to turn "example = example +4;" into "example += 4;" if you tried to use it in a declaration though, thats an automatic error. the variable wouldnt have an initial value to add on to to begin with

Oh, so a same skill can be added into an array ? like arr1 = [ 258 , 258 , ... ] ? I assumed it could only be inserted once, my bad :facepalm: didn't heard about it from the little digging I did
yeah. Array elements dont have to be unique, and in JS they dont even have to be the same datatype. as long as you are aware of what's in it and you treat the data accordingly, you can use them to hold whatever's needed
Thanks ! Also the code seems to be working, I didn't experience any issue with it !

Original thread: https://forums.rpgmakerweb.com/threads/expanding-skill-that-make-user-cast-random-skill-thread.169007/