Script Help - Inn Pricing Based on Party's HP

elsathehobo

Warper
Member
Joined
Nov 11, 2018
Messages
2
Reaction score
1
First Language
English
Primarily Uses
RMMV
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:
upload_2018-11-10_22-58-48.png

Thank you!
Elsa
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
3,729
Reaction score
4,683
First Language
English
Primarily Uses
RMMZ
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.
 

elsathehobo

Warper
Member
Joined
Nov 11, 2018
Messages
2
Reaction score
1
First Language
English
Primarily Uses
RMMV
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?
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
3,729
Reaction score
4,683
First Language
English
Primarily Uses
RMMZ
@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"
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

I should realize that error was produced by a outdated version of MZ so that's why it pop up like that
Ami
i can't wait to drink some ice after struggling with my illness in 9 days. 9 days is really bad for me,i can't focus with my shop and even can't do something with my project
How many hours have you got in mz so far?

A bit of a "sparkle" update to the lower portion of the world map. :LZSexcite:
attack on titan final season is airing tomorrow, I'm excited and scared at the same time!

Forum statistics

Threads
105,882
Messages
1,017,231
Members
137,607
Latest member
Maddo
Top