JavaScript questions that don't deserve their own thread

● ARCHIVED · READ-ONLY
Started by Shaz 3413 posts Page 139 of 171 View original ↗
  1. Hello,
    I have created my own plugin script call, I want simply want to Show Choice and when the player picks one to do a Show Text but when I run this I get "this.setWaitMode is not a function"
    I've looked at forums and I have no idea how to tackle this problem.
    Any help to solve it would be much appreciated.
    this is my code:
    Code:
    // Set choices to A, B, C; default C, no cancel
            $gameMessage.setChoices(['A', 'B', 'C'], 2, -1);
            
            $gameMessage.setChoiceBackground(0);
            
            $gameMessage.setChoicePositionType(2);
    
            $gameMessage.setChoiceCallback(function (responseIndex) {
                if (responseIndex === 0) {
                    $gameMessage.add("1");
                    this.setWaitMode('message');
                }
                else if (responseIndex === 1) {
                    $gameMessage.add("2");
                    this.setWaitMode('message');
                }
                else if (responseIndex === 2) {
                    $gameMessage.add("3");
                    this.setWaitMode('message');
                }
            });
    
            this.setWaitMode("message");
  2. @Zuque - this is a context-dependent keyword. setChoiceCallback passes a function to the message, which by default will be run in a Game_Message context on choice. But setWaitMode is a method of Game_Interpreter, not Game_Message, hence the error.

    One way to avoid that is to manually bind a context to the callback function, e.g.
    JavaScript:
    $gameMessage.setChoiceCallback(function(responseIndex) {
      if (responseIndex === 0) {
        $gameMessage.add("1");
      } else if (responseIndex === 1) {
        $gameMessage.add("2");
      } else if (responseIndex === 2) {
        $gameMessage.add("3");
      }
      this.setWaitMode('message');
    }.bind(this));
    More details here:
  3. caethyril said:
    @Zuque - this is a context-dependent keyword. setChoiceCallback passes a function to the message, which by default will be run in a Game_Message context on choice. But setWaitMode is a method of Game_Interpreter, not Game_Message, hence the error.

    One way to avoid that is to manually bind a context to the callback function, e.g.
    JavaScript:
    $gameMessage.setChoiceCallback(function(responseIndex) {
      if (responseIndex === 0) {
        $gameMessage.add("1");
      } else if (responseIndex === 1) {
        $gameMessage.add("2");
      } else if (responseIndex === 2) {
        $gameMessage.add("3");
      }
      this.setWaitMode('message');
    }.bind(this));
    More details here:
    @caethyril thank you very much, while your reply helped me it simply made the error stop showing i was still unable to make the Show Text function to work,
    what would happen is i would get to the Show Choice, pick a choice and then it won't show anything and simply return me to the start of my event,
    how ever i was able to find THIS threat by @kmph and on post #3 he says that these two actions cannot happen in the same function, so I split the function into two and made a public variable that will be the bridge for these two functions to communicate and i am happy to say that it works.......for now
    Code:
    Game_Interpreter.prototype.Chat_DemoNpc1 = function () {
    
    
            
            // Set choices to A, B default B, no cancel
            $gameMessage.setChoices(['A', 'B',], 1, -1);
            
            $gameMessage.setChoiceBackground(0);
            
            $gameMessage.setChoicePositionType(2);
    
            $gameMessage.setChoiceCallback(function (responseIndex) {
    
                $gameVariables.setValue(5, responseIndex);
    
            }.bind(this));
    
            this.setWaitMode('message')
    
            
        }
        Game_Interpreter.prototype.Test = function () {
            switch ($gameVariables.value(5)) {
                case 0:
    
                    $gameMessage.add('Affirmative!')
                    this.setWaitMode('message')
                    break;
                case 1:
    
                    $gameMessage.add('Negative!')
                    this.setWaitMode('message')
                    break;
            }
        }
  4. @Zuque - ah, I see. Window_ChoiceList#callOkHandler clears $gameMessage almost immediately after processing the onChoice callback, so any response message never gets a chance to show.

    Note that you don't really need to bind the function if you're not using this inside it. Doesn't do any harm, it's just unnecessary.

    Good to hear you got something working! :kaohi:
  5. How will I check if the battle occurred is a random encounter or not?
  6. I usually flip a switch on before forced event encounters and flip it off afterwards to tell if the current one is random
  7. I want the effect of an item to change if the actor has a particular state. The two damage formulae work fine independently, but when I combine them into an if/else nothing happens.

    My formula looks like this:
    if (b.state(142) true) {damage formula one} else {damage formula two}

    I have tried with a comma after (142) and an equals sign.
    Could someone please point out my error.

    Thank.
  8. Kes said:
    Could someone please point out my error.
    2 things i see.

    Kes said:
    b.state(142)
    battler.state() isnt a function. what you want is battler.isStateAffected(stateId).

    Kes said:
    b.state(142) true
    youre not making a comparison when you add that true there. the true there would cause a js error since its not expected there.

    Kes said:
    I have tried with a comma after (142) and an equals sign
    a comma does an operation that you do not want to perform here, and a single equals sign sets a value, not check one (and is useless to perform on a function)

    also, not erroneous, but something to remember. a comparison returns true if true, and conditional statements will accept that. you dont need to explicitly check if a condition == true as that would already be its value if it were and can be used directly.

    try
    Code:
    b.isStateAffected(142) ? special damage formula : default damage formula
  9. @Robro33 Thank you for the help, it now works perfectly. And also thank you for the explanations which mean that I am less likely to make the same mistake in the future.
  10. Conditionals still do my head in.
    I have a skill which targets any actor who has a particular state. What I have so far is this:

    Code:
    <JS Targets>
    targets=$gameTroop.aliveMembers().filter(enemy => enemy.isStateAffected(114));
    </JS Targets>

    However, I assume that if there is no one in the party with that state, this formula means that the enemy will do nothing, which is not what I want.

    How would I make this a conditional so that if there is no one with the state, it does [damage formula]?

    Thank you
  11. if that skill can still be used when theres no viable target, then theres 2 things i can think of that you might want to do here. (im not sure if that tag prevents selection from confirming if the targets array is empty. do make sure that this is the case before continuing)

    Kes said:
    How would I make this a conditional so that if there is no one with the state, it does [damage formula]?
    building off of your question, you could ignore the target js tag, make it target all foes, and then return to the idea before where it deals boosted damage to those with the state active

    Kes said:
    I assume that if there is no one in the party with that state, this formula means that the enemy will do nothing, which is not what I want.
    you can use whatever the VS equivalent for the requirement eval is to make it so that the skill can only be used if theres some foe with the state active. that way it cant be used and do nothing if theres no valid targets to begin with.

    does either of those sound like they'd work? i think theyre better ways to go about it than changing the scope of the skill only if theres no valid targets, and then checking the target count in the damage formula, but itll only work if you say it does


    also, that being said, what you have here confuses me a bit.
    Kes said:
    targets=$gameTroop.aliveMembers().filter(enemy => enemy.isStateAffected(114));
    you said it targets actors at the start, but the skill's target code only looks at troop members, which are only enemies. if its a skill thats only usable by enemies, you should change the $gameTroop to $gameParty. If its actor only, you can keep it as is. if both enemies and actors can use it, you can use user.opponentsUnit().aliveMembers()... to get the user's foes' team
  12. Robro33 said:
    you can use whatever the VS equivalent for the requirement eval is to make it so that the skill can only be used if theres some foe with the state active. that way it cant be used and do nothing if theres no valid targets to begin with.
    Sadly you are dealing with a coding idiot here so I'm unable to say what the VS equivalent is.

    I think, however, your other idea (basically using the same sort of structure as my query yesterday) would work as an AoE dealing either a high damage formula or a low one. I had hoped, though, to have this as a single target skill as I don't want too many AoE skills at this stage of the game.
  13. Kes said:
    How would I make this a conditional so that if there is no one with the state, it does [damage formula]?
    Can you clarify? This part doesn't make any sense.

    It's going to do your damage formula to whoever the skill hits, those two things are unrelated.

    If you're saying you want the skill to target anyone with the state...and no one has the state...then what should happen?
  14. ATT_Turan said:
    If you're saying you want the skill to target anyone with the state...and no one has the state...then what should happen?
    That was what I was trying to explain by my suggestion of a conditional.
    I would like the skill to work like this:
    If there is an actor with state[n] then that actor should be the target. That is in the singular because only one actor will have that state at any one time. I had assumed that the damage done would be what's in the damage formula.
    Else random target [alternative damage formula]

    I hope that's clearer.
  15. Kes said:
    I hope that's clearer.
    Mostly, but you're still conflating two entirely separate things.

    Determining the target of a skill has nothing to do with the damage formula. The damage is calculated after the skill successfully hits (which, of course, is after the target is determined).

    You also did not explicitly clarify Robro's question about who is using and getting hit by your skill, because you keep saying targeting actors but your original code only targets enemies.

    But because you keep saying actors, I'll presume that's what you want.

    So to determine the target:
    Code:
    <JS Targets>
    targets=$gameParty.aliveMembers().filter(actor => actor.isStateAffected(114));
    if (targets.length<1)
        targets=[$gameParty.aliveMembers()[Math.randomInt($gameParty.aliveMembers().length)]];
    </JS Targets>

    Then you use a regular ternary operator in your damage formula to do different amounts of damage depending on whether or not the target has the state.
  16. Kes said:
    I had hoped, though, to have this as a single target skill as I don't want too many AoE skills at this stage of the game.
    Kes said:
    That is in the singular because only one actor will have that state at any one time.
    ah, that makes more sense. when i saw the filter in the target tag and its only condition was a state check, i assumed it was an aoe. i was wondering why you didnt want it it be an aoe while having a condition that could easily lead it to being aoe anyways, but the state only being on one actor at a time changes things
  17. @ATT_Turan Thank you. I think I've got it now. But atm I'm even denser than usual as I'm just recovering from my first ever dose of covid (yeah, I'd managed to slither past it up 'til now) and I've got the dreaded brain fog.

    @Robro33 Sorry I wasn't clearer earlier on.

    Thank you both for your help.
  18. <JS Pre-Start Turn>
    if ($gameActors.actor(1).hpRate() >= 0.5 && $gameActors.actor(2).hpRate() >= 0.5) {
    if $gameParty.members()[0].isStateAffected(1) {
    } else {
    if $gameParty.members()[1].isStateAffected(1) {
    } else {
    $gameTroop.members()[0].removeState(380);
    }
    }
    }
    </JS Pre-Start Turn>

    I tried to make it check for dead state on actors, but failed miserably
    Any ideas how to make it properly?
  19. An explanation of what you intend for it to do would help.
    it is checking for the death state. you didnt write for it to do anything if its found on those actors.

    what you wrote is "if both actors 1 and 2 are above 50% hp, and neither are dead, remove state 380 from that enemy"

    even if you wrote for it to do anything if the death state was found, reread that first condition again and it should be clear why nothing could even happen if dead actors were found
  20. Robro33 said:
    An explanation of what you intend for it to do would help.
    it is checking for the death state. you didnt write for it to do anything if its found on those actors.

    what you wrote is "if both actors 1 and 2 are above 50% hp, and neither are dead, remove state 380 from that enemy"

    even if you wrote for it to do anything if the death state was found, reread that first condition again and it should be clear why nothing could even happen if dead actors were found
    This is state application, that is supposed to trigger like "if both actor at 50% or more HP, state is removed AND if there is only 1 actor alive, if other actor has 50% or more HP, state is removed"
    That's what I wanted ig