RMMV return true or false if any affected State has a Note(meta)

Status
Not open for further replies.

Dopan

Veteran
Veteran
Joined
Mar 27, 2020
Messages
478
Reaction score
203
First Language
German
Primarily Uses
RMMV
I want to make a Function where it checks if the Battler has any state affected that has the Note "<Speed_State>" or if thats required i would also use "<Speed_State:true>"

I tried several things ,for example :
( the battler has 2 States ,1state has the note the other state doesnt)
Example_1
JavaScript:
    Game_Battler.prototype.hasSpeedStates = function() {
        this.states().some(function(state) {
            if (state.meta.Speed_State) {
                return true;
            } else {
                return false;
            } 
        });
    };

// this returns "undefined"
Example_2
JavaScript:
    Game_Battler.prototype.hasSpeedStates = function() {
        for (var i = 0; i < this.states().length; i++) {
             if (this.states()[i].meta.Speed_State) {
                 return true;

             }
        }
    };
// this will return true but this way i cant set it to false ..
// if i add a similar Condition for "return false".. or if i use "else"
// .. the state that has the Note will make it true and
// the next state in line that doesnt have the note will set it to false..
I think i need to use the ".some(function(state) {" from Example_1 but i cant make it work correctly..

I am thankfull for any help
 
Last edited:

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,041
Reaction score
1,472
First Language
EN
Primarily Uses
RMMZ
Right idea, just made a couple of little omissions~
  1. You remembered to return true/false for each state, but forgot to return the result from the some function. Also you can just do "return meta" rather than "if meta true, return true, else return false". :)
    JavaScript:
    Game_Battler.prototype.hasSpeedStates = function() {
      return this.states().some(function(state) {
        return state.meta.Speed_State;
      });
    };

  2. return ends a function, so you can just put a return false at the end:
    JavaScript:
    Game_Battler.prototype.hasSpeedStates = function() {
      for (var i = 0; i < this.states().length; i++) {
        if (this.states()[i].meta.Speed_State) {
          return true;
        }
      }
      return false;  // only reaches this if not return true earlier.
    };
They both do the same thing, use whichever you prefer~ :kaothx:
 

Dopan

Veteran
Veteran
Joined
Mar 27, 2020
Messages
478
Reaction score
203
First Language
German
Primarily Uses
RMMV
@caethyril

Thank you again^^

I will use the Example 1
JavaScript:
Game_Battler.prototype.hasSpeedStates = function() {
  return this.states().some(function(state) {
    return state.meta.Speed_State;
  });
};
( it looks shorter)
To be honest ,Most things i learn by "trial and error" & there a much things that i dont know because most Infos i got are only from this forum^^

So the Infos about 1. & 2. which you added are really helpfull to me.
You remembered to return true/false for each state, but forgot to return the result from the some function. Also you can just do "return meta" rather than "if meta true, return true, else return false".
return ends a function, so you can just put a return false at the end:
(that helped me more than just giving me the right Code, atleast i understand now,what was wrong^^)

->This will be reported as "solved". THX!
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
15,317
Reaction score
8,517
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

So I need to come up with quite a few action sequences.
Was stressing about this plugin earlier, but pretty happy with how its looking now! Purple makes it pop more too!! :)
A friend requested me to do a pfp for his twitch channel, so far he approved one of my sketches, so going to digital now. The style he's looking for is quite different from my usual, hopefully I can get it right... :kaoback:
I am so hyped! A new post update and im anxious to see reactions..eeeeeeee

Forum statistics

Threads
105,479
Messages
1,014,033
Members
137,124
Latest member
alpeesharma1212
Top