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

Discussion in 'Programming' started by Carloskhard, Oct 11, 2018.

Tags:
  1. Carloskhard

    Carloskhard Member

    Joined:
    Mar 13, 2017
    Posts:
    140
    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!
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    2,808
    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 likes this.
  3. Carloskhard

    Carloskhard Member

    Joined:
    Mar 13, 2017
    Posts:
    140
    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!
     
  4. Marcus12321

    Marcus12321 Member

    Joined:
    Aug 29, 2018
    Posts:
    57
    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.
     
  5. Carloskhard

    Carloskhard Member

    Joined:
    Mar 13, 2017
    Posts:
    140
    I know, but shouln't be a big problem right?
    What other option could I have if not?
     
  6. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,036
    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.
     
    Carloskhard likes this.
  7. Carloskhard

    Carloskhard Member

    Joined:
    Mar 13, 2017
    Posts:
    140
    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?
     
  8. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,036
    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 likes this.
  9. Carloskhard

    Carloskhard Member

    Joined:
    Mar 13, 2017
    Posts:
    140
    Okay I understand, thanks a lot for the great info,I'll go that way! :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice