Adding a if statement to some JS

Hey there gang, a while back I got a bit of code to add to the RPG_Object.js that removed the ability to flee from random battles. I want this to only happen on my games hard setting however. So I need to add an extra check to see if variable 14 = 2. If it does = 2 then the escape command should be removed.

I've tried some things from searching .js forums but have had no luck so I thought I would ask here. I'll post the code I use.

Game_Player.prototype.executeEncounter = function() {
if (!$gameMap.isEventRunning() && this._encounterCount <= 0) {
this.makeEncounterCount();
var troopId = this.makeEncounterTroopId();
if ($dataTroops[troopId]) {
BattleManager.setup(troopId, false, false);
BattleManager.onEncounter();
return true;
} else {
return false;
}
} else {
return false;
}
};

Would love any help I could get on this one if anyone would like to lend a hand. And thanks for taking a look. 🙂
In the future if you need to post any code, use the code formatting option cause it makes it way easier for someone else to actually be able to read it. Anyways, the following code should work.
Spoiler: Code
Code:
Game_Player.prototype.executeEncounter = function() {

    var canEscape = true;
    if($gameVariables.value(14) == 2)
        canEscape = false;
    if (!$gameMap.isEventRunning() && this._encounterCount <= 0) {
        this.makeEncounterCount();
        var troopId = this.makeEncounterTroopId();
        if ($dataTroops[troopId]) {
            BattleManager.setup(troopId, canEscape, false);
            BattleManager.onEncounter();
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
};
Apologies with the code, I'm not a coder so I didn't realize, but now I know I'll make sure to post code correctly in the future. Thanks for letting me know about that. And a huge thanks for the new code. It works perfectly, and that was super quick too. 🙂

I've moved this thread to Learning Javascript. Thank you.

JavaScript:
BattleManager.setup(troopId, false, false);

The second argument in that function is rather or not you can escape.
so you could do something like:

JavaScript:
Game_Player.prototype.executeEncounter = function() {
if (!$gameMap.isEventRunning() && this._encounterCount <= 0) {
this.makeEncounterCount();
var troopId = this.makeEncounterTroopId();
if ($dataTroops[troopId]) {
$gameVariables.value(14) == 2 ? BattleManager.setup(troopId, false, false) : BattleManager.setup(troopId, true, false);
BattleManager.onEncounter();
return true;
} else {
return false;
}
} else {
return false;
}
};

Edit:
JavaScript:
var value  = $gameVariables.value(14)
BattleManager.setup(troopId, value == 2 ? false : true, false)
Pretty sure that will work as well, its also cleaner.
I wanted to try this out but there was a little error in the code and I wanted to make sure I have it set up right. This is how I have it and it works but I was a little unsure about a couple things so I thought I would ask just in case there is an error.

Code:
Game_Player.prototype.executeEncounter = function() {
if (!$gameMap.isEventRunning() && this._encounterCount <= 0) {
      this.makeEncounterCount();
      var troopId = this.makeEncounterTroopId();
      if ($dataTroops[troopId]) {
            var value  = $gameVariables.value(14)
            BattleManager.setup(troopId, value == 2 ? false : true, false)
            BattleManager.onEncounter();
            return true;
      } else {
            return false;
      }
 } else {
            return false;
      }
};
In the future if you need to post any code, use the code formatting option cause it makes it way easier for someone else to actually be able to read it. Anyways, the following code should work.
I didn't even see that >_> I'm blind...
I wanted to try this out but there was a little error in the code and I wanted to make sure I have it set up right.
what error is it throwing you?
No error I meant in your code above, this part.
(troopId, false, false) : BattleManager.setup
I changed the : to a ;
and I wasn't sure I had the format right so I was just wondering If I set it all up right.
I changed the : to a ;

Ohhhhh, no it susposed to be a :

JavaScript:
condition ? /* if true */ : /* if false */;
Oh dang, sorry I thought it was a miss type comparing it with other scripts. What is weird is that it works the way I changed it but it's good to know I messed up. Thanks for setting me straight.

Original thread: https://forums.rpgmakerweb.com/threads/adding-a-if-statement-to-some-js.118030/