Script Help - Inn Pricing Based on Party's HP

● ARCHIVED · READ-ONLY
Started by elsathehobo 4 posts View original ↗
  1. Hello,
    I have an event that allows the party to restore all hp. I have rigged up the dialogue, a variable that holds the inn's price, all that, but there is one thing I am having trouble doing: setting the price. I would like the price to be the total missing hp of the party - however the formula to do so is giving me a lot of trouble. Using google I came up with the following formula: (spaced out for readability)
    Code:
    $gameParty.members().forEach(function(m){
         $gameVariables.setValue(5, $gameVariables.value(5) + (m.mhp - m.hp))
    })
    This is inserted into a Control Variables event command. However, when testing it out ingame, the price is consistently 0 no matter the health of my party. I can't figure out what I'm doing wrong.

    Here's a screenshot of the event in case the script isn't enough:
    Spoiler
    upload_2018-11-10_22-58-48.png

    Thank you!
    Elsa
  2. Code:
    var totalAmount = 0;
    for (var i = 0 ; i < $gameParty.members().length; i++) {
       var ca = $gameParty.members()[i];
       totalAmount += (ca.mhp - ca.hp);
    }
    $gameVariables.setValue(5, totalAmount);

    Didn't test it, but it should work, unless I made a typo.
  3. MushroomCake28 said:
    Code:
    var totalAmount = 0;
    for (var i = 0 ; i < $gameParty.members().length; i++) {
       var ca = $gameParty.members()[i];
       totalAmount += (ca.mhp - ca.hp);
    }
    $gameVariables.setValue(5, totalAmount);

    Didn't test it, but it should work, unless I made a typo.
    It didn't work at first - the price stayed at 0g - but then I inserted the code into a Script Command instead of a Control Variables command and it worked! I haven't checked to see whether my formula would work as a script command, though I suppose it would too.

    I see the problem now with my formula - it probably worked fine in the Control Variables command, but then bc there was no returned number at the end the command defaulted to 0, undoing the function.

    May I ask, is there a difference between using a generic loop vs .forEach?
  4. @elsathehobo There are no big differences between both loop. "foreach" reads every elements in an array while "for" simply runs through the loop as long as the condition is valid. It's just that I prefer the "for" loop, that's why I use it instead of "foreach"