1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Different x y positions on several instances of same object

Discussion in 'Programming' started by Axl Trauts, May 6, 2019.

  1. Axl Trauts

    Axl Trauts Member

    Joined:
    Jun 26, 2016
    Posts:
    33
    Hi,

    I am trying several things for this with no luck yet.

    I created a shop, where you can buy 1-4 wingmen for the player (a plane). They will go to their respective position: two on the left, one behind the other and the other two on the right, one behind another, like a V formation. When the player moves, their x-y positions will move to keep formation.

    I can buy all the four at the same time. They are created on the Draw Event of obj_player like this:

    while(M_NUM_ALLY <> num_ally) // M_NUM_ALLY is a macro that points to an array position
    { instance_create(x,y,obj_ally); // The ally object
    num_ally++; // a variable created at the start of obj_player to prevent creating infinite instances
    break;
    }

    Now I need to do something on Step like this example:
    //for the first ally:
    x=obj.player.x+10
    y=obj.player.y

    //for the second ally:
    x=obj.player.x+20
    y=obj.player.y+10

    //for the third ally:
    x=obj.player.x-10
    y=obj.player.y

    //for the fourth ally:
    x=obj.player.x-20
    y=obj.player.y+10

    But I am missing something about instance_number() somewhere. How can I do this?
     
  2. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    Try something like this in your player instance. Just put their coordinates in each case.
    Code:
    if(instance_exists(obj_ally)){
    
    var _num_wingman = instance_number(obj_ally);
    
    switch(_num_wingman){
    case 1:
        break;
    case 2:
         break;
    case 3:
         break;
    case 4:
         break;
    }
    }
     
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,148
    Code:
    ///Player Create///
    AllyPos[0]=10;
    AllyPos[1]=0;
    AllyPos[2]=20;
    AllyPos[3]=10;
    AllyPos[4]=-10;
    AllyPos[5]=0;
    AllyPos[6]=-20;
    AllyPos[7]=10;
    
    Code:
    ///Player End Step (or at the very end of the Player's normal Step)///
    var i = 0;
    with obj_ally {
        x = other.x + other.AllyPos[i++];
        y = other.y + other.AllyPos[i++];
    }
     
  4. Axl Trauts

    Axl Trauts Member

    Joined:
    Jun 26, 2016
    Posts:
    33
    Thanks for your response! It worked but it moves all instances to the same position, depending on each case, as I feared. I am missing something:

    Code:
    if(instance_exists(obj_aliado)){
    
    var _num_wingman = instance_number(obj_ally);
    
    switch(_num_wingman){
        case 0:
            break;
        case 1: 
            obj_ally.x=x-largo/2
            obj_ally.y=y
            break;
        case 2:
            obj_ally.x=x+largo/2
            obj_ally.y=y
            break;
    case 3:
            obj_ally.x=x-largo/2
            obj_ally.y=y+alto/2
         break;
    case 4:
            obj_ally.x=x+largo/2
            obj_ally.y=y+alto/2
         break;
    }
    }
    
    largo and alto just stores spride_width and sprite height
     
  5. Axl Trauts

    Axl Trauts Member

    Joined:
    Jun 26, 2016
    Posts:
    33
    Thanks! it works! but when I wanted to make the coordinates less hardcoded, using sprites length and width, it fails because the ally hasn´t been created yet. I´m not sure if I can change ally´s sprite later yet. For example:
    AllyPos[0]=x-obj_ally.largo/2

    obj_ally.largo stores obj_ally sprite_width.

    Code:
    PosAmigo[4]=-largo/2-16;
    PosAmigo[5]=alto/2;
    
    I´d like to change 16 to obj_ally.largo but well the "not set" error happens. Anyways I´m using it as a good base. Thanks
     
  6. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    Are they simply drawn, or they are active objects with behavior? If the former, just use use draw_sprite_* for each one of them. In the case they are all objects with their own behavior, you will have to track their instance id's. You could have, for example, a 4-slots array or list which would contain every active ally id. Then use that id to set their position, not obj_ally (that will move all of them in the same spot, all 4 of them are obj_ally, after all).
    Hope that helps
     
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,148
    sprite_width is already a useable variable that gets updated with sprite_index.

    you are getting errors because you are trying to reference the largo of the player, not the Ally, probably.

    So, going off of my method, change the player's create event array to just 1, 0 or -1 (so +10 and +20 would be +1, -10 and -20 would be -1) for each entry. Then the End Step code would be

    Code:
    with obj_ally {
       x = other.x + other.AllyPos[i++]*sprite_width;
       y = other.y + other.AllyPos[i++]*sprite_height;
    }
     

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