GML Passing "i" in "for" function to script inside it[SOLVED]

Carloskhard

Member
This may be a stupid question but I have a 'for' loop and inside I'm running a script, and inside that script I need to know that "i" number.
Up until yesterday I was using just "i" inside the script and the script was reading "i" perfectly.However in today's version of the proyect "i" is not being read correctly and I have to pass "i" to the script doing this:
Code:
script(variables..., i ,more variables..)
and the accesing it with argument[x].

Do you know what could have changed?
How do I read correctly "i" from inside my script?
Thanks!
 

Simon Gust

Member
local variables have to be passed to scripts, instance variables don't.
in your first version you probably didn't write "for (var i" but rather "for (i".
Either don't use var or pass it via arguments.
 

Carloskhard

Member
local variables have to be passed to scripts, instance variables don't.
in your first version you probably didn't write "for (var i" but rather "for (i".
Either don't use var or pass it via arguments.
I know that var don't pass that's why I'm not ussing var, however the problem persist.

Ooooooooooooooh waita second!
So the problem was I WASN`T using var in that event, but I was using it on another event on that same object.
I didn't knew that would affect all "i"s from the object.

So that was it.Problem solved thanks!
 
M

Marcus12321

Guest
Just know, that if you don't use var, and you use the variable i, then the variable i is attached to that instance forever until you destroy it.
 

Carloskhard

Member
It is a big problem. Failing to properly scope temporary looping variables can carry major unexpected side effects. Nothing can save you from this class of bugs, other than learning to put your variables in the right scope and not making a pig sty of your instance scope.
I see,that is very interesting.
However, could I get the same result of passing "i" to the script antoher other way without having to put it manually on the script function?
 

FrostyCat

Redemption Seeker
I see,that is very interesting.
However, could I get the same result of passing "i" to the script antoher other way without having to put it manually on the script function?
The proper course of action is to pass as a parameter. If you want to go back to using instance or global variables for this purpose, fine, but don't complain if that script starts cross-interacting with things it shouldn't lay its hands on.
 

Carloskhard

Member
The proper course of action is to pass as a parameter. If you want to go back to using instance or global variables for this purpose, fine, but don't complain if that script starts cross-interacting with things it shouldn't lay its hands on.
Okay I understand, thanks a lot for the great info,I'll go that way! :)
 
Top