Somewhat complicated/spreading State

Hey all, it's me again. I've spent a couple hours working on my most complicated state yet, and even though I wasn't sure if I could even get it to work with my very limited knowledge, I actually managed to work it out. What this state does is that it resists death once and then spreads to another target when the affected target dies. Here's my code for it:

Spoiler: Code
JavaScript:
<Custom Apply Effect>

target.deathResist = (1);

</Custom Apply Effect>


<Custom React Effect>

if (target.deathResist == 1) {

  if (this.isDamage() && value >= target.hp) {

    value = (target.hp - 1);
    target.deathResist = (0);
}

} else {

    if (this.isDamage() && value >= target.hp) {
        
      
    var group = [];
    var group2 = target.friendsUnit().aliveMembers();

    group2.forEach(element =>
    {

        if (!element.isStateAffected(105)){
        
            group.push(element);
        }
    });

    if (group.length > 0) {

        var randomTarget = group[Math.floor(Math.random() * group.length)];
        randomTarget.addState(105);
    }
}
}

  </Custom React Effect>

After getting this to work, I thought "Hey, maybe this is not the best way to do this, but it works. What else can I do with this?" The first idea I had was to make it so that instead of just jumping to another target, it jumps to two other targets so that it's actually like a spreading plague. So, I modified it like this:

Spoiler: Code
JavaScript:
<Custom Apply Effect>

target.deathResist = (1);

</Custom Apply Effect>


<Custom React Effect>

if (target.deathResist == 1) {

  if (this.isDamage() && value >= target.hp) {

    value = (target.hp - 1);
    target.deathResist = (0);
}

} else {

    if (this.isDamage() && value >= target.hp) {
        
      
    var group = [];
    var group2 = target.friendsUnit().aliveMembers();

    group2.forEach(element =>
    {

        if (!element.isStateAffected(105)){
        
            group.push(element);
        }
    });

    if (group.length > 0) {

        var randomTarget = group[Math.floor(Math.random() * group.length)];
        var index = group.indexOf(randomTarget);
        if (index > -1) {
            var group3 = group.splice(index, 1);
    }
        var randomTarget2 = group3[Math.floor(Math.random() * group.length)];
        randomTarget.addState(105);
        randomTarget2.addState(105);
    }
}
}

  </Custom React Effect>

The idea was that if I remove the random target I had already selected from the array and make another random selection from the new array, I should have two unique targets. But as you can guess, it's not working. I'm getting an error that suggests my second target returns as undefined. What am I doing wrong here?

Sidenote: I've been posting these questions on MV support but should I be posting them in JS support instead?
I'm getting an error that suggests my second target returns as undefined. What am I doing wrong here?
var randomTarget2 = group3[Math.floor(Math.random() * group.length)];
group is an array that will be one element smaller than group. you chose an index based on the length of group, so its possible to pull an index that group3 doesnt have an element for.
in a 3 person party, group wouldnt 3 wouldnt contain the affected battler, so it has 2 elements. but by the time you make group 3, group 3 only has one element. if Math.random() returns something higher than .5, your group 3 dosnt have a [1] to be used

also just a few comments.

you can replace that portion of starting off group empty and looping through group 2 to populate it by using filter()
Code:
var group = target.friendsUnit().aliveMembers().filter(member => !member.isStateAffected(105))

You seem to be using target.deathResist as you would a boolean, so you can just use boolean values.
Code:
target.deathResist = true;
if (target.deathResist){};
if (!target.deathResist){};
and so on

theres a Math.randomInt(x) function added in MV itself that takes a random integer from 0 up to but not including x. you dont have to do Math.floor(Math.random() * group.length). Math.randomInt(group.length) gets you the same thing

EDIT: might have mixed splice up with slice in retrospect

Edit2: Definitely did but it doesnt change the nature of the error🤣. the 3 person party example would work, but the idea applies to all larger sizes. your group 3 only has a single element no matter what because of splice. anything other than [0] will result in grabbing undefined, and youve wrote randomTarget2 as "group3[Math.floor(Math.random() * group.length)]". also if group only has 1 element to begin with, youre guaranteed an error for randomtarget2 being undefined and using addState on it

Im assuming you meant for that to be group and not group3?
@Robro33

Thanks for your reply!

I didn't use a boolean because while currently, it acts as one, I'm undecided as of yet if I will keep it that way or maybe even add a conditional to increase the amount.

I didn't know about the filter method, thanks for mentioning it! It seems like a more efficient way to do what I'm doing, so I'll be sure to read up on it.

As for the meat of the issue about splice, I'm confused about your explanation.

My understanding is that:

JavaScript:
var index = group.indexOf(randomTarget);

this will give me the position of the first target I randomly select.

JavaScript:
var group3 = group.splice(index, 1);

Then here, the splice will remove the element in that position and return another array without that element which will be group3. I don't understand why group3 would have only 1 element.

Edit: Wait, I might just be dumb. I just realized splice() replaces the original array. I think I need to use toSpliced() instead.
Then here, the splice will remove the element in that position and return another array without that element which will be group3. I don't understand why group3 would have only 1 element.
splice returns an array of the removed values and then modifies the source array to no longer contain the removed elements
your group 3 is a one element array containing one battler. group sill contains all the other battlers

1719096187374.png


EDIT:

I think I need to use toSpliced() instead.
you dont have to. group still contains all the battlers it originally had except one randomly chosen one. you could just choose a second battler from group

Also, you could loop it and give it 2 conditions
Code:
for(var i=0; group.length > 0 && i<2; i++) {
    var randomTarget = group.splice(Math.randomInt(group.length),1);
    randomTarget.addState(105);
}
Since Robro is helping with the code portion of the question:
Sidenote: I've been posting these questions on MV support but should I be posting them in JS support instead?
Yes. Per the description of MV Support,
"Are you having event troubles? Encountering bugs in the base program even without plugins? How do I and How to? Questions" - it's for questions about how to use MV's native functionality.

So this stuff should more appropriately go in JS/Plugin Support. It's not a big deal, you can Report your post and ask a mod to move it, or if one happens upon it they probably will just to keep things orderly.

Just out of curiosity,
JavaScript:
target.deathResist = (1);
Where did you get that weird syntax from? I'm actually a little surprised it doesn't break anything, but there's definitely no reason for you to have parentheses around the number 1. I see you do it below with (0), also - that's not considered correct syntax.
@Robro33

I see! I was under the impression that splice returned not the removed element but the array without that element. The examples I looked at on w3schools showed the altered array and not the removed(or added) elements but that's because the example displayed the array and not the return value of the splice method itself. That's my bad for not fully paying attention to what was being done in the example.

Now I understand how to make it work. Btw, is there another method that returns the altered array instead of the removed/added elements like splice does?

@ATT_Turan

Understood. I've seen quite a few threads on MV support about plugins/JS so I wasn't sure if that was okay or not, but I guess those threads were created by people like me who didn't know better. I'll be sure to post on JS support from now on.

As for your question about the syntax, I'm honestly not sure. I had this idea in my head that I should use parenthesis when assigning values or it might brick something, but I guess that was just misinformation that I stumbled upon somewhere.


Edit: Both target selections work now, however another issue that I didn't think about cropped up: If there aren't enough legal targets, I get a crash, which in retrospect, makes sense. I feel like I may be able to solve this myself by maybe adding another conditional somewhere and checking if there are enough legal targets but if there is an easy solution, I'm not above getting the answer handed to me at this point. 😛
is there another method that returns the altered array instead of the removed/added elements like splice does?
Just copy the array to the other variable before calling splice() on it.
Code:
let group3 = group;
group3.splice(whatever);
@ATT_Turan That makes perfect sense. I was thinking if there wasn't a function like pull(), which I guess would be the opposite of push(), but from your answer and some googling, it doesn't appear that exists.


@Robro33

I've revised the code based on your suggestions and it ended up a lot shorter, which is great. But now, it's not working, which isn't great. 😛

Here's the latest version:

JavaScript:
<Custom Apply Effect>

target.deathResist = 1;

</Custom Apply Effect>


<Custom React Effect>

if (target.deathResist == 1) {

  if (this.isDamage() && value >= target.hp) {

    value = (target.hp - 1);
    target.deathResist = 0;
}

} else {

    if (this.isDamage() && value >= target.hp) {
       
     
    var group = target.friendsUnit().aliveMembers().filter(member => !member.isStateAffected(105));

    for (let i=0; group.length > 0 && i<2; i++) {

        randomTarget = group.splice(Math.randomInt(group.length),1);
        randomTarget.addState(105);

    }
}
}
  </Custom React Effect>

The error I'm getting is randomTarget.addState is not a function. My guess as to what's going wrong is that randomTarget is assigned an array because of the splice method instead of an object, and addState() function does not work on an array. As the array assigned to randomTarget only contains one element, the problem might be solved if the array is converted to an object but I don't know how to do that. Then again, I may be completely wrong and the issue is something else entirely.

I could, of course, go back to my old method but the loop method seems a lot cleaner and should, in theory, fix my issue about not having enough legal targets.
Then again, I may be completely wrong and the issue is something else entirely.
nah, youre correct about why that error is there, but youre overthinking the solution. its a 1 element array. use [0]
Code:
randomTarget[0].addState(105);
the array doesnt grow on subsequent iterations either. it gets reassigned. youll only be dealing with the first element in it
nah, youre correct about why that error is there, but youre overthinking the solution. its a 1 element array. use [0]
Code:
randomTarget[0].addState(105);


I was just about to post that I had solved the problem by doing this:

Spoiler: Code
JavaScript:
<Custom Apply Effect>

target.deathResist = 1;

</Custom Apply Effect>


<Custom React Effect>

if (target.deathResist == 1) {

  if (this.isDamage() && value >= target.hp) {

    value = (target.hp - 1);
    target.deathResist = 0;
}

} else {

    if (this.isDamage() && value >= target.hp) {
       
    var group = target.friendsUnit().aliveMembers().filter(member => !member.isStateAffected(105));

    for (let i=0; group.length > 0 && i<2; i++) {

       
        randomTarget = group[Math.randomInt(group.length)];
        randomTarget.addState(105);
        if (group.indexOf(randomTarget) > -1) {
            group.splice(group.indexOf(randomTarget),1);
    }

    }
}
}
  </Custom React Effect>

Which is a way more roundabout solution than your simple one. 😛 Trying out your solution now!

Edit: Yep, everything works perfectly now. Thanks for all the help!

Original thread: https://forums.rpgmakerweb.com/threads/somewhat-complicated-spreading-state.169714/