As programgamer said I think you need to go back and understand the structure of GM more, but to break a for loop down I'll try expressing it this way. A for loop is just wrapping around a while loop.
The for loop
for(var i = 0; i < 2; i++) {
doThing()
}
is just a neater way of writing
var i = 0
while (i < 20) {
doThing();
i++
}
They are functionally the same, someone just realised that since people use this pattern so often they might as well put it into one line.
For the sake of expressing myself I'm now just going to use the while loop to explain what's happening.
Ultimately, as you say, the purpose is to make a specific behaviour happen n number of times (in this case twice, but sometimes it's determined in other ways)
There are four things happening in the above code.
1. they have an expresion they want to do some number of times. The expression is whatever is inside the {}
2. they create a new local variable, by convention called i (for iteration I think), which holds how many times the loop has executed. It starts at 0 because when the variable is created the loop has executed 0 times.
3. the slightly more complex part, the while(condition). When a while loop is executed, it test the condition (the part between the
()), if the result is true, then the while loop executes the expression, in this case the expression is
Once it has finished executing the expression, it then retests the condition (it checks if i is still less then 2), if the condition is still true then it executes the expression again, then rechecks the condition, then executes the expression and so on, until the condition is false, where it stops and moves onto the line after the while loop. This is a pretty fundamental programmatic idea, so you should try and familiarise yourself with it, since you will use it a lot. As an interesting side note, if the condition never becomes true, as in the loop below
the while loop will never finish, and thus the program never moves on from it (I think after some amount of time GM realises it's stuck and throws an error).
4. (The easiest) to prevent our example loop from going forever, on the last line of the expression we add 1 to i (i++ is short for i = i + 1). So, because each time the expression is executed, i increases by 1, after 2 iterations of the loop i is no longer less than 2, and the loop ends having run the expression 2 times.
To bring this back to your problem, you need to realise that when you say
, the reason it runs twice every step event, is because i is being reset every step, thus the loop runs the expression twice every step. In fact if you reexamine your tutorial, it wasn't that the message was being shown two steps in a row, it was being shown twice in one step. I don't know the exact context of the tutorial, but it's reasonable to assert there must have been another control structure influencing it so that the for loop was only run once (but the for loop run the expression twice on that step). One easy solution would be to wrap the for loop in an if expression, like
if (doMakeMessages) {
for(var i = 0; i < 2; i++) {
doThing()
}
doMakeMessages = false
}
Then in whatever event you want to trigger the expression to occur twice, just set doMakeMessages to true. Important note, I do not create doMakeMessage in the above snippet. The code
var doMakeMessages = true
if (doMakeMessages) {
for(var i = 0; i < 2; i++) {
doThing()
}
doMakeMessages = false
}
will have the exact problem you are encountering now, since doMakeMessages is being set as a local var every step, doMakeMessages will always be true at the time of the if statement checking it's condition, thus will always run the for loop.
I hope I've helped, again as said by ProgramGamer don't be discouraged, programming is totally different to anything else a human does, and feels counter intuitive to everyone when they begin, and sometimes feels counter intuitive to people who have been doing it for years. Just hang in, keep going through examples and tutorials and ask questions when you are stuck, and eventually you'll start understanding it. (Also frantically google searching often works).
edit: Jesus Christ I wrote a lot, I hope I helped?