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
481
Reaction score
204
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,044
Reaction score
1,474
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
481
Reaction score
204
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,518
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 Profile Posts

currently listening to Driftwood Gaming streaming some MZ dev time and it's delightful! had no idea they were this entertaining, love it
In quarantine, will be tested soon. This year is really something... special.
I was at another orientation yesterday and I realized I'll be working with an old friend! We met at the local water park a few years back and it was so nice to see her again! We had a chat, and next week when I officially start, I'll see if maybe we can trade our Zoom IDs so we can talk!
I made some sideview closets for MV!
You can find them in my MV resource thread.

Forum statistics

Threads
105,509
Messages
1,014,319
Members
137,194
Latest member
purboy
Top