GML Different objects in a 'for' loop (SOLVED)

Discussion in 'Programming' started by josyanf1, Jul 24, 2019.

  1. josyanf1

    josyanf1 Member

    Joined:
    Jan 12, 2018
    Posts:
    17
    Hi all! Programming objects that have different directions I thought I could make a for loop to optimize code.


    I usually do it this way to assign attributes to an object:
    Code:
    var particle_1 =instance_create(x,y,obj_firework_particle); with particle_1 {direction = 30};
    
    var particle_2 =instance_create(x,y,obj_firework_particle);with particle_2 {direction = 60};

    But I'm still a novice programmer, do you know how it can be done?
    Code:
    for (c = 0; c < 12; c += 1)
    {
    var ?? = instance_create(x,y,obj_firework_particle); with ?? {direction = 30 * c};
    }
    A greeting and thanks for the help! :)
     
  2. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    626
    for (var c = 0; c < 12; c += 1)
    {
    with (instance_create(x,y,obj_firework_particle))
    {direction = 30 * c};
    }

    Can be done I think....?? By having it in the with statement you don't need to define it as a variable, and making the variable in the for loop local it is accessible to the instance in the with statement.
     
  3. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    975
    Using ?? is syntax error. Using inst, foo, or some other non-reserved word would do just fine. Instance ids are just numbers, and as such can be assigned to variables (instance, local, global), and passed around, stored in data structures. In other words, there's nothing special about the return value from instance creation. Just assign it to a name that makes sense to you (and that doesn't conflict with a name in use by GMS already, like id) and use it.
     
  4. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,442
    Well you got very close!
    The ??? Can be whatever you like.
    Also, 'c' needs to be declared local (using var) so that it is visible inside the with block.
    That is because otherwise it is an 'instamce variable', and is only accessible through that instance.
    For example :
    Code:
    for (var c = 0; c < 12; c += 1)
    {
    var particle = instance_create(x,y,obj_firework_particle);
     with particle {direction = 30 * c};
    }
    
    Every iteration of the loop, 'particle' will have a different value.

    Or you can make use of the dot notation to make your code a little simpler:

    Code:
    for (var c = 0; c < 12; c += 1)
    {
    var particle = instance_create(x,y,obj_firework_particle);
    particle.direction = 30 * c;
    }
    
     
  5. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,212
    I think you're confusing yourself a bit with the temporary variables and assigning values to them. In this code you posted:
    Code:
    var particle_1 =instance_create(x,y,obj_firework_particle); with particle_1 {direction = 30};
    
    var particle_2 =instance_create(x,y,obj_firework_particle);with particle_2 {direction = 60};
    You could just as easily do this:
    Code:
    var particle =instance_create(x,y,obj_firework_particle); with particle {direction = 30};
    
    particle =instance_create(x,y,obj_firework_particle);with particle {direction = 60};
    And it would function identically. You only need to keep the ID in the variable as long as you need to manipulate that specific instance, once you have done the manipulation, you are free to use the variable again. That's why it's fine to do it like that in a loop. GMWolf's above code, when broken down into the individual loops, functions exactly like the code above, e.g.:

    Loop 1:
    Code:
    var particle = instance_create(x,y,obj_firework_particle);
    particle.direction = 30 * 0;
    
    Loop 2:
    Code:
    var particle = instance_create(x,y,obj_firework_particle);
    particle.direction = 30 * 1;
    
    Loop 3:
    Code:
    var particle = instance_create(x,y,obj_firework_particle);
    particle.direction = 30 * 2;
    
    There's no need to worry about making separate variables for each instance.
     
  6. josyanf1

    josyanf1 Member

    Joined:
    Jan 12, 2018
    Posts:
    17
    Thank you all for your kind answers and for your patience.
    As many say, it is only about declaring the variable 'c' of the 'for' loop and inside the loop creating the object in a variable with the 'with' in a normal way :)

    Code:
    for (var c = 0; c < 12; c += 1)
    {
    var particle = instance_create(x,y,obj_firework_particle);
    with particle {direction = 30 * c};
    }
     

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