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
484
Reaction score
205
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,054
Reaction score
1,481
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
484
Reaction score
205
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,323
Reaction score
8,521
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

Just got FES pack for my MZ. The details of the tilesets and chars seem not very compatible with MZ rtp. Look like they are scaled up. Not saying it's bad, but just won't fit well.
I've been busy with other things lately but I'm getting back into working on stuff for my game next month. I think I'm going to fiddle with making NPC busts next, mixing and matching stuff from DLC I've purchased.
Just finished composing a boss battle theme!

Forum statistics

Threads
105,566
Messages
1,014,675
Members
137,239
Latest member
roeselerfinnole
Top