GM:S 1.4 basic for loop

Discussion in 'Programming' started by woods, Jul 11, 2019.

  1. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    i am trying to understand how the basic for loop works..

    i have figured out how to draw my basic toolbar... but how would i make this happen for more than one row?
    this gives me a row of ten...
    Code:
    ///draw slots
    
    // draw hotbar bottom center
    var my_x = 320
    for (i=0; i<10; i++;)
    {        
        my_x = my_x+32
        instance_create(my_x,672,obj_slot);
    }
    
    
    
    
    //drawing bag slots top right
    // draw row 1
    var my_x = 704
    for (i=0; i<5; i++;)
    {        
        my_x = my_x+32
        instance_create(my_x,224,obj_slot);
    }
    
    // draw row 2
    var my_x = 704
    for (i=0; i<5; i++;)
    {        
        my_x = my_x+32
        instance_create(my_x,192,obj_slot);
    }
    // draw row 3
    var my_x = 704
    for (i=0; i<5; i++;)
    {        
        my_x = my_x+32
        instance_create(my_x,160,obj_slot);
    }
    
    // draw row 4
    var my_x = 704
    for (i=0; i<5; i++;)
    {        
        my_x = my_x+32
        instance_create(my_x,128,obj_slot);
    }
    
    // draw row 5
    var my_x = 704
    for (i=0; i<5; i++;)
    {        
        my_x = my_x+32
        instance_create(my_x,96,obj_slot);
    }
    
    
    how would i make it so it draws more rows of five? (without using another for loop with a different y value)

    it works but....
    i dont think this second bit is how we should do it?
     
    Last edited: Jul 11, 2019
  2. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    415
    Code:
    ///draw slots
    var my_x = 320
    for (i=0; i<10; i++;)
    {         
        my_x = my_x+32
        instance_create(my_x,y,obj_slot);
        instance_create(my_x,y-32,obj_slot);
    }
    
     
  3. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    so simple when you know the answers ;o) thanks curato



    Code:
    ///draw slots
    
    // draw hotbar bottom center
    var my_x = 320
    for (i=0; i<10; i++;)
    {         
        my_x = my_x+32
        instance_create(my_x,672,obj_slot);
    }
    
    
    
    //drawing bag slots top right
    // draw row 1
    var my_x = 704
    for (i=0; i<5; i++;)
    {         
        my_x = my_x+32
        instance_create(my_x,224,obj_slot);
        instance_create(my_x,192,obj_slot);
        instance_create(my_x,160,obj_slot);
        instance_create(my_x,128,obj_slot);
        instance_create(my_x,96,obj_slot);
    }
    
    
     
  4. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,442
    You could use a loop inside the loop.
    The outer loop loops over he X values. In inner loop loops over the y values.

    Or, make a 'create' row function, and call it multiple times.
     
  5. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    im just trying to figure out how this works so i can get my inventory moving forward.... rather difficult when you dont understand the tools you have available ;o)
     
  6. jr carey

    jr carey Member

    Joined:
    Sep 22, 2016
    Posts:
    172
    theres an easier method:
    //drawing bag slots top right
    // draw row 1
    var my_x = 704
    var ii = 0;
    for (i=0; i<5; i++;)
    {
    my_x = my_x+32
    repeat(5){
    ii++
    instance_create(my_x,96*ii,obj_slot);
    }
    ii = 0
    }
    try that
     
  7. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    223
    thanks guys.. works great

    appreciate the help
     

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