I need help with a script "Loop"

while () {stuff}

I dont know how to use it.
I use the event command, but when I pass it to script I don't understand it.

This would be the example, using event command:

♦ Loop
♦ Variable control: #0011 Hp += 3
♦ If: Hp= HpC
♦ Break loop
♦
:The end
♦
: Repeat the above
♦

I need to put it inside a script, how do I translate it using while (){stuff}

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

while () {stuff}

I dont know how to use it.
I use the event command, but when I pass it to script I don't understand it.

This would be the example, using event command:

♦ Loop
♦ Variable control: #0011 Hp += 3
♦ If: Hp= HpC
♦ Break loop
♦
:The end
♦
: Repeat the above
♦

I need to put it inside a script, how do I translate it using while (){stuff}
JavaScript:
while ($gameVariables.value(11) < $gameVariables.value(12) {
    $gameVariables.setValue(11, $gameVariables.value(11) + 3);
};

Change variable 12 to whichever variable HpC actually is. I changed the operand to "<" since if you're increasing Hp by 3 and it happens not ever perfectly match HpC it'll just keep going forever. This way the loop stops when Hp gets to or above HpC. Though if your stuff makes that not an issue I think "!==" is the one you're wanting.
...I just realized what your question ACTUALLY was. You wanted to know how to use a while loop, not just have that one translated. In my defense I'm tired. lol

Basically, you have while (expression) {stuff}. Each time it goes through the loop, it evaluates the expression. As long as that expression returns true, it will execute the loop. The moment the expression returns false, the loop breaks. Which means if the content of the loop doesn't update the values used in the expression in some way, the loop will NEVER end, because the expression will return the same value every time.

That's why in your example, I changed Hp = HpC to Hp < HpC, because if HpC was 10 and Hp started the loop at 2, the loop would never end, since it would go Hp = 2, then 5, then 8, then 11, and so on. Since Hp would NEVER equal HpC, so it would just keep going... and going... and going.... Change it to Hp < HpC, and it would stop once Hp hit 11, since 11 is not less than 10.

Hope that helps!
...I just realized what your question ACTUALLY was. You wanted to know how to use a while loop, not just have that one translated. In my defense I'm tired. lol

Basically, you have while (expression) {stuff}. Each time it goes through the loop, it evaluates the expression. As long as that expression returns true, it will execute the loop. The moment the expression returns false, the loop breaks. Which means if the content of the loop doesn't update the values used in the expression in some way, the loop will NEVER end, because the expression will return the same value every time.

That's why in your example, I changed Hp = HpC to Hp < HpC, because if HpC was 10 and Hp started the loop at 2, the loop would never end, since it would go Hp = 2, then 5, then 8, then 11, and so on. Since Hp would NEVER equal HpC, so it would just keep going... and going... and going.... Change it to Hp < HpC, and it would stop once Hp hit 11, since 11 is not less than 10.

Hope that helps!
I understand, even so sometimes it does not work, for example, following your explanation, use a
while (Hp> HpC) {Hp-1}; and it locks or does not work, I will see another way to make it work.
I understand, even so sometimes it does not work, for example, following your explanation, use a
while (Hp> HpC) {Hp-1}; and it locks or does not work, I will see another way to make it work.
For that, I would basically do this:
JavaScript:
while (Hp >= HpC) {
    Hp -= 1; //or Hp = Hp - 1
};
That would reduce Hp by 1 over and over again until it was at or below HpC (I'm assuming HP and Max HP, given the variable names)

If you think it's getting stuck in the loop, put this inside the loop:
JavaScript:
console.log(Hp)
That way every time it loops, it puts the current value of Hp in the console (press F8 to access it during a playtest). That way if there's a problem, you should be able to see what it's doing.
For that, I would basically do this:
JavaScript:
while (Hp >= HpC) {
    Hp -= 1; //or Hp = Hp - 1
};
That would reduce Hp by 1 over and over again until it was at or below HpC (I'm assuming HP and Max HP, given the variable names)

If you think it's getting stuck in the loop, put this inside the loop:
JavaScript:
console.log(Hp)
That way every time it loops, it puts the current value of Hp in the console (press F8 to access it during a playtest). That way if there's a problem, you should be able to see what it's doing.
Thank you so much for help me.
But it keeps failing, the result is still above expectations. It does not subtract 1, it does not subtract anything.

JavaScript:
var mhp = $gameActors.actor(1).mhp; var hp = $gameActors.actor(1).hp;
var AN = (hp*100)/mhp;
var Car = AN + 15;
while (Car >= mhp) {
    Car = Car - 1
};
$gameScreen.showPicture(21, "barraCarga", 0, 114, 92, Car, 98, 150, 0);
var V= (AN - 61.9)/0.32; var R= (AN - 42.2)/-0.309;
$gameScreen.tintPicture(21, [R,V,-170,119], 1);
$gameVariables.setValue(189, Car);
$gameVariables.setValue(188, AN);
console.log(Car)

(AN = ancho = width) (Car = carga = load)
It is a medicine. When the medicine is drunk the common event occurs. The common event generates a higher health bar, but that higher health bar should never exceed mhp. mhp = 100, Car (Carga hp) = 115 if life bar is full. It should be Car = 100.

Car = 115 also appears on the console
$ gameVariables.setValue (189, Car); = 115

I also wanted to do it with conditional derivation.
if (Car> mhp) {Car - (Car-mhp)};
it doesn't work for me either.

--------------------------------------------------

This is the only way I found it to work.

JavaScript:
//Script:
var mhp = $gameActors.actor(1).mhp; var hp = $gameActors.actor(1).hp;
$gameVariables.setValue(20,(hp*100)/mhp);
//Event command:
♦ Variable control: #20 + 15
♦ Loop:
      ♦ Conditional derivation: # 20> 100
              ♦ Variable control: # 20 - 1
end
      ♦ Conditional derivation: # 20 <= 100
  ♦ break loop
end
//script:
var mhp = $gameActors.actor(1).mhp; var hp = $gameActors.actor(1).hp;
var AN = (hp*100)/mhp;
var Car = $gameVariables.value(20);
$gameScreen.showPicture(21, "barraCarga", 0, 114, 92, Car, 98, 150, 0);
var V= (AN - 61.9)/0.32; var R= (AN - 42.2)/-0.309;
$gameScreen.tintPicture(21, [R,V,-170,119], 1);
$gameVariables.setValue(189, Car);
$gameVariables.setValue(188, AN);
console.log(Car)
Thank you so much for help me.
But it keeps failing, the result is still above expectations. It does not subtract 1, it does not subtract anything.

JavaScript:
var mhp = $gameActors.actor(1).mhp; var hp = $gameActors.actor(1).hp;
var AN = (hp*100)/mhp;
var Car = AN + 15;
while (Car >= mhp) {
    Car = Car - 1
};
$gameScreen.showPicture(21, "barraCarga", 0, 114, 92, Car, 98, 150, 0);
var V= (AN - 61.9)/0.32; var R= (AN - 42.2)/-0.309;
$gameScreen.tintPicture(21, [R,V,-170,119], 1);
$gameVariables.setValue(189, Car);
$gameVariables.setValue(188, AN);
console.log(Car)

(AN = ancho = width) (Car = carga = load)
It is a medicine. When the medicine is drunk the common event occurs. The common event generates a higher health bar, but that higher health bar should never exceed mhp. mhp = 100, Car (Carga hp) = 115 if life bar is full. It should be Car = 100.

Car = 115 also appears on the console
$ gameVariables.setValue (189, Car); = 115

I also wanted to do it with conditional derivation.
if (Car> mhp) {Car - (Car-mhp)};
it doesn't work for me either.
If you don't want it to exceed mhp, then just do
Code:
if (Car > mhp) {
Car = mhp;
}
Not sure offhand why the loop isn't working, it looks right to me, but if the iteration isn't actually required, you don't need to have a loop. But if you're wanting the loop, stick the console.log INSIDE the loop, at the end, so outputs every time it iterates. That way you can see what it's doing.

Original thread: https://forums.rpgmakerweb.com/threads/i-need-help-with-a-script-loop.134409/