Get the array length of states in a condition?

Hey,


for a skill that cures all negative states, I want to write a pice of my own code inside Yanflys action sequence. It looks like this:

<target action>
for (var i = 0; i < $dataStates.length; ++i){
  if (target.isStateAffected(i)){
    if ($dataStates[stateId].meta.good){
    }
    else{ 
    target.removeState(i);
    }
  }
}
</target action>

the ".meta.good" is already coded elsewhere and works perfectly fine. It flags states as good and not affected by luk's chance of not getting it.


The two errors I keep getting are: i is undefined or unexpected token {.


However, $dataStates is an array of which you can check the array length (at least I think you can), and I literally checked these brackets 20 times and they are correct.


If you could help me with that skill and that specific line of code, I would be really thankful!
Usually you want to do i++, instead ++i.


<target action>
for (var i = 0; i < $dataStates.length; i++) {
if (target.isStateAffected(i)) {
if ($dataStates[stateId].meta.good) {
//Do nothing
}
else{
target.removeState(i);
}
}
}
</target action>

//Optional

<target action>
var i = 0;
for (i < $dataStates.length; i++) {
if (target.isStateAffected(i)) {
if ($dataStates[stateId].meta.good) {
//Do nothing
}
else{
target.removeState(i);
}
}
}
i = 0; //Reset i
</target action>




I think that should help with i is undefined issue, hopefully.
thank you for the quick reply!


I am a javascript noob and I will still have to learn much about math and variables.


Unfortunately, I keep getting the unexpected token { error, so I cant tell whether this has fixed it for sure.


I will go through all of yanflys tutorials again and look if he has some similar code in his tutorials.


I will reply again when I found a solution. Good night!
I was thinking, since you seem to be getting a token error, we can remove some braces. @Saathan


<target action>
var i = 0;
for (i < $dataStates.length; i++) {
if (target.isStateAffected(i)) {
if ($dataStates[stateId].meta.good)
//Do nothing
else
target.removeState(i);
}
}
i = 0; //Reset i
</target action>




Hopefully that will fix the error. If that doesn't fix the error, it might be the other braces in this call.
Hi,


I finally got that unexpected token error. Yanfly's Action sequence doesn't even support brackets and the "for var..." loop.


That's why both of my errors occured. I will try to run this with a custom eval code within action sequence or via a common event script call.


Ill post a solution if I find one.
I FOUND A SOLUTION


Yanflys Action sequence has problems processing more complicated codes (or I am just too dumb to figure out how), even when using the eval code.


Instead of putting the code into <target action>, leave it blank and use Yanflys Skill Core plugin. Before the Action sequence stuff, I put:

<After Eval>


for ( var i = 0; i < $dataStates.length; i++) {


if (target.isStateAffected(i)) {


if ($dataStates.meta.good) {


}


else {


target.removeState(i);


break;


}


}


}


</After Eval>

and it worked perfectly!


I hope this helps everyone that has the same problem as I had, and thank @Kino for the help 🙂


Edit: Put squared brackets with an i inside after $dataStates and before .meta, this forum cant show squared brackets somehow.

Original thread: https://forums.rpgmakerweb.com/threads/get-the-array-length-of-states-in-a-condition.66127/