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
542
Reaction score
255
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,117
Reaction score
1,525
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
542
Reaction score
255
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,377
Reaction score
8,536
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

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,040
Messages
1,018,469
Members
137,821
Latest member
Capterson
Top