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
493
Reaction score
223
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,059
Reaction score
1,483
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
493
Reaction score
223
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,329
Reaction score
8,522
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

being able to transition music seamlessly is amazing. my main character's theme has lyrics and it goes from vocal version to istrumental when he leaves his room. and vice versa. and thanks to a plugin, the transition is seamless. I love it.
All of the auditions for our game are so good, I'm so glad I'm not in charge of choosing completely. lol I just can't with some of them. I listen to them on repeat. XD
Working on a Patch to integrate AlphaABS into random generated Dungeons. Was a bit tricky, but the outcome is a real Life Changer!!

Will release it the next days!
Been pretty busy... but I got a better headset for recording and listening today. Dad says that he'll consider taking me to Great Wolf Lodge for the first time once I finish summer classes (my final semester!) I'll be taking my plushie friends with me: Bendy, Lolbit, and Helen Henny. (I WUV PLUSHIES!)
SF_Monster3,4 added!

Forum statistics

Threads
105,651
Messages
1,015,301
Members
137,327
Latest member
Mezzy
Top