RMMV Is there a way to check and count states on a target, without using a million if conditions?

TheTitan99

Veteran
Veteran
Joined
Nov 2, 2015
Messages
217
Reaction score
99
First Language
English
Primarily Uses
RMMV
So, I've made this simple little function:

Code:
//Check conditions.
//Returns a value. The higher the number, the more positive status effects are n it. Lower means more negative effects.
Titan_CheckConditions = function(b) {
    var n = 0;

    //Positive states
    if(b.isStateAffected(42))
        n = n + 1;

    if(b.isStateAffected(43))
        n = n + 2;

    if(b.isStateAffected(44))
        n = n + 1;

    if(b.isStateAffected(45))
        n = n + 2;

    //Negative stats
    if(b.isStateAffected(52))
        n = n - 1;

    if(b.isStateAffected(53))
        n = n - 2;

    if(b.isStateAffected(54))
        n = n - 1;

    if(b.isStateAffected(55))
        n = n - 2;

    //Positive conditions
    if(b.isStateAffected(62))
        n = n + 1;

    if(b.isStateAffected(63))
        n = n + 1;

    if(b.isStateAffected(72))
        n = n + 1;

    if(b.isStateAffected(73))
        n = n + 2;

    //Negative conditions
    if(b.isStateAffected(82))
        n = n - 1;

    if(b.isStateAffected(83))
        n = n - 1;

    if(b.isStateAffected(91))
        n = n - 1;

    if(b.isStateAffected(94))
        n = n - 3;

    if(b.isStateAffected(95))
        n = n - 2;

    return n;
}


Basically, bad states lower the returned number, and good states raise the number. This is useful for, say, enemy AIs, as they can now check to see if they should dispel targets. You don't wanna dispel a poisoned ally if it has a million positive states on it after all!

The only thing is, this code feels super bad. It works, but I feel there's gotta be a better way to do this than a mountain of if conditions! Like, there's gotta be simple loops or something, right?

I think this is the right board for this question.
 

Soulrender

Jack Of All Trades
Veteran
Joined
Jan 12, 2019
Messages
972
Reaction score
566
First Language
Poland
Primarily Uses
RMMV
@TheTitan99
Code:
var positiveStates = []; //put positive states ids
var negativeStates = []; //put negative states ids
for (var i = 0; i <= positiveStates - 1; i++){
    n += 1;
}

for (var i = 0; i <= positiveStates - 1; i++){
    n -= 1;
}

But then, if you want a different values for different states, then

Code:
var positiveStates = []; //put positive states ids
var negativeStates = []; //put negative states ids
for (var i = 0; i <= positiveStates - 1; i++){
    if (b.isStateAffected(positiveStates[i]){
        n += Number($dataStates[positiveStates[i]].meta["buff"]);
    }
}

for (var i = 0; i <= negativeStates - 1; i++){
    if (b.isStateAffected(negativeStates[i]){
        n -= Number($dataStates[positiveStates[i]].meta["debuff"]);
    }
}

and place in your states notetags: https://prnt.sc/xo8lnx

Actually you can stick to the only one notetag, but using two will give you further informations about boosts.
Because of my age I leave that way a hint for myself in case I forgot what function/state etc do
 
Last edited:

TheTitan99

Veteran
Veteran
Joined
Nov 2, 2015
Messages
217
Reaction score
99
First Language
English
Primarily Uses
RMMV
Thank you! There were a few typos in that code, but after fixing those, it works like a charm! Much, much cleaner code.
 

Latest Threads

Latest Posts

Latest Profile Posts

Does anyone know of NPC sprites for RMMZ? I know of Flirion's RMMV recolours, but RMMZ's sprites are slimmer and more detailed, so I'm a bit on the edge to use Flirion's im RMMZ...
I can't believe that after all those years, KH Union X Dark Road is closing the services.

Due to a situation in which I thought plugin adaptations had stopped for the QPlugins, I "ghetto" converted these 7 over yesterday. They appear to work perfectly as long as you use old style plugin commands. I am uncertain if I will have a lot of free time in the future to work on this, but if you'd like them, here they are.
Was there a thread that you could share a screenshot of your game? I feel like I found it once but can't locate it again.

I was being lazy and just copied a map to test a new parallax, but I forgot to delete all the events. This was the result. :o

Forum statistics

Threads
108,818
Messages
1,039,786
Members
141,277
Latest member
acoolusername
Top