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,699
Reaction score
4,658
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,699
Reaction score
4,658
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

People1.png & SF_People1.png added!!!!

Adventurer:
"Why your body looks like a bird?"

Stranger:
"Because I'm harpy"

Adventurer:
"Clap along if you feel like a room without a roof"

*Both dancing to Pharrell Williams song*
Stream will be live shortly with a make-up session of the Interactive Text Adventure! Feel free to drop by!
Pootscooter wrote on Pharonix's profile.
Hey @Pharonix, just wondering what you're up to lately and if you've had an opportunity to check out the LTBS thread in the past few days...(a couple of us could really use your expertise). Hope you are well and Happy Thanksgiving!

Forum statistics

Threads
105,506
Messages
1,014,263
Members
137,173
Latest member
zoetsoi
Top