MV A quick question on basic Coding Logic (SOLVED)

Howdy y'all!

I'm learning how to do code with MV and I've come across this basic For Loop that I THINK I'm reading correctly but if I am then the logic seems weird to me, so I'm hoping someone here can help me to understand it all better.

The For Loop is this:
var x = $gameParty.members().length; for (i = 0; i < x; ++i) { $gameParty.members()[i].gainHp(9999); }

And the logic as I understand it is that what it's doing is:
> variable x is set to the length of the array of Party Members.
> variable i is set to 0
> if i is less than x
> then gain party member i's HP by 9999 and add 1 to the value of variable i
> do this all again and stop after i is equal to x


But what I don't understand is why Party Member [0] also gets healed.

Shouldn't this happen in a party of 4 instead?:
i equals 0, x equals 3, add 1 to i, i now equals 1, heal party member i (aka 1) 9999
This would skip Party Member [0] but it's how the code reads to me because the adding of the 1 comes a line before the gaining of the HP.

So I'm struggling to understand why the Healing takes place before the Adding 1 to the value i, as I've tested this code and it DOES in fact heal all 4 Party Members without skipping any.

Any light or clarity y'all can shed on this matter is GREATLY appreciated!
I'd love to be able to do all this coding stuff, and RPG Maker has always been a fun engine to build things in for me.
Loop starts with the initial i value which is 0.

++i happens at the end of each loop cycle.

Here is equivalent logic:
Code:
var x = $gameParty.members().length;
var i = 0;
loop
{
 if (i < x)
  {
   exit loop;
  }
  $gameParty.members()[i].gainHp(9999);
  ++i;
}
It's worth noting that there is a difference between i++ and ++i which is usually not relevant in for loops, but does matter if you're assigning your iteration variable to another identifier inside the loop somewhere for some reason:

JavaScript:
let i = 0;
let j = i++;

j = 0, and i is now 1.

JavaScript:
let i = 0;
let j = ++i;

j = 1, i = 1

With the ++ after the identifier, it's returned and then incremented. With the ++ before, it's incremented and then returned.
Loop starts with the initial i value which is 0.

++i happens at the end of each loop cycle.

Here is equivalent logic:
Code:
var x = $gameParty.members().length;
var i = 0;
loop
{
 if (i < x)
  {
   exit loop;
  }
  $gameParty.members()[i].gainHp(9999);
  ++i;
}

Yeah that's what I thought was happening, but I'm not following why.
Is it some unwritten JavaScript (or maybe MV) rule that the third argument in a For Loop always happens at the end of the each loop cycle?

Are there other things out there like this?
It's intimidating to find out that even without keywords you don't just read top to bottom lol.

I guess you just learn those by doing and adapt?
Thanks for confirming it though!
I'll just have to make a mental note that for loops go: argument 1, argument 2, codeblock below, argument 3.

With the ++ after the identifier, it's returned and then incremented. With the ++ before, it's incremented and then returned.
This (full) comment of yours took me a couple reads to understand lol, but that IS worth knowing!
Thanks for the info!
Yeah that's what I thought was happening, but I'm not following why.
It's just syntax of how javascript for loops are written.

A bunch of coders who created javascript thought it a good idea to have your PC understand for loops only if they are written in this manner. They were like:

"bro! I know, let's have it work this way..."
Code:
FOR (initial value; exit condition; thing that runs at the end of each loop cycle)
It's just syntax of how javascript for loops are written.

A bunch of coders who created javascript thought it a good idea to have your PC understand for loops only if they are written in this manner. They were like:

"bro! I know, let's have it work this way..."
Code:
FOR (initial value; exit condition; thing that runs at the end of each loop cycle)
So lesson of the day, keep an eye out for syntax!
Thanks for the clarity and help (and the welcome in the other post)!
Is it some unwritten JavaScript (or maybe MV) rule that the third argument in a For Loop always happens at the end of the each loop cycle?
No, it's a written rule in JavaScript and every other C/C-like language that uses for loops. 😉

If you just looked up the documentation on any JavaScript site you'd get that explanation, e.g.:

1694052830982.png



Are there other things out there like this?
Yes, there are a number of different kinds of loops, switch statements, default array methods, the whole concept of using code organized by functions is not linear.

It's intimidating to find out that even without keywords you don't just read top to bottom lol.
I guess you just learn those by doing and adapt?
Most people learn by...well, learning, I think. Is there a reason you're not using some kind of tutorials or course? Code is much easier to understand without any guesswork involved.

Not that there's anything wrong with you asking questions here, of course, but there's a plethora of resources for you without having to word your question and wait for people to respond. 🙂
No, it's a written rule in JavaScript and every other C/C-like language that uses for loops. 😉

If you just looked up the documentation on any JavaScript site you'd get that explanation, e.g.:

View attachment 275827



Yes, there are a number of different kinds of loops, switch statements, default array methods, the whole concept of using code organized by functions is not linear.


Most people learn by...well, learning, I think. Is there a reason you're not using some kind of tutorials or course? Code is much easier to understand without any guesswork involved.

Not that there's anything wrong with you asking questions here, of course, but there's a plethora of resources for you without having to word your question and wait for people to respond. 🙂
I never said I wasn't using tutorials!
Been following a youtube tutorial series but it didn't cover this question I had so I figured there's no harm in asking the internet! Especially when there's a nice specialized forum about these things!

I wasn't familliar with any documentation before coming here though, but honestly even if I was I'm not sure how good I'd be at understanding it... I've tried documentation for other languages and software in the past and never really found a good way to learn via them, I'm not particularly studious I think Q_Q.

If you've got any particularly good tutorials in mind for getting into JS with MV (that aren't just talking about what common object orientated programming things are - as I'm pretty confident on my variable types, arrays, conditional statements, and what not), then I won't say no to a friendly link!
I never said I wasn't using tutorials!
That's fair enough - it's just your comment about "learning by doing" and the fact that for loops are a pretty basic programming command, I thought if you had been doing any actual tutorial/course it would've been covered.

If the video you're watching is somehow geared toward MV, that would explain it, as:

If you've got any particularly good tutorials in mind for getting into JS with MV
I don't think that's a good approach. You should just learn JavaScript. MV has nothing to do with it - there isn't any syntax or anything that's somehow unique to MV. If you learn how to understand and write in JavaScript, then you'll understand anything you look at in MV's code.

If you do try to just learn how to parse through MV's code files, you're going to be jumping into advanced things without covering the basics, and you'll get into situations like this.

As far as JavaScript tutorials, I like the one I linked you to. Click over to it, start on the Statements chapter on the left, and just go through it.

Good luck!
That's fair enough - it's just your comment about "learning by doing" and the fact that for loops are a pretty basic programming command, I thought if you had been doing any actual tutorial/course it would've been covered.

If the video you're watching is somehow geared toward MV, that would explain it, as:


I don't think that's a good approach. You should just learn JavaScript. MV has nothing to do with it - there isn't any syntax or anything that's somehow unique to MV. If you learn how to understand and write in JavaScript, then you'll understand anything you look at in MV's code.

If you do try to just learn how to parse through MV's code files, you're going to be jumping into advanced things without covering the basics, and you'll get into situations like this.

As far as JavaScript tutorials, I like the one I linked you to. Click over to it, start on the Statements chapter on the left, and just go through it.

Good luck!
Hey! Just want to say, been going through that Tutorial you linked me (with a pen and an a4 notepad making notes) and it's really good so far! So thanks for the link!

I've found it hard in the past to find tutorials that cover everything in a digestable way, it's probably why my coding knowledge has ended up so sporadic/spotty for the few languages I'm interested in (have tried getting into c# and python in the past).

A combination of this being really well presented (and interactable) and my brain somehow giving off happy feelings in response to me using a pen and paper again for the first time in ages are making this feel both fun and actually learnable!

And I'm even picking up a bit of HTML on the side due to the small HTML usage in the tutorial lol.
for (value of temporary variable; condition that must be fulfilled before breaking; variable that NEEDS to be incremented or decremented at the end of the loop)

I refuse to call it a cycle because there's a vital difference between a cycle and a loop. The cycle should go on. It has to. The loop must stop at all costs or you'll get in trouble. Seriously, you will while coding.

SIDE NOTE

Only in Ruby you don't follow that 3 step model while using a for loop. The reason behind it is because it's a wrap for the each loop method. Normally, that's not the case in any other language I've learned so far.

Original thread: https://forums.rpgmakerweb.com/threads/a-quick-question-on-basic-coding-logic-solved.160964/