GMS 2 (Solved) Grab instance ID once for multiple instances of an object

Discussion in 'Programming' started by Solitary Confinement, Jul 11, 2019 at 4:38 PM.

  1. Solitary Confinement

    Solitary Confinement Member

    Joined:
    May 20, 2017
    Posts:
    22
    Heya,

    So I'm trying to grab the instance ID and store it in order to check a variable for a specific instance and apply an action for that instance only whose ID was grabbed.

    The setup will help explain this better.

    . One object player, two to four instances at any given time. Object player has a single "chain" id which connects the instances of various objects it creates to itself (bullets, items etc.). This chain id is simply called "myid"
    . Instances of object player shoot bullets. The bullets have the same myid variable as the player instance that creates them.
    . When any player instance shoots bullets at a monster object, I'd like it to setup a variable (lets call it "hp" for simplicity) that responds only to the player that shoots the bullets at it (hp reduction and a response, both of which only happen when the hp variable intended for that specific player instance, falls to 0).
    . E.g. if player A shoots at monster, monster's hp variable intended for A only takes hits, falls to 0 and the monster attacks player A only. And if player B shoots at monster, monster's hp variable intended for B only takes hits and the same happens (attacking only B).

    A straight forward way would be to simply have four different hp variables, setting up the ids for the player instances from the get-go and checking to see if the ids match. However, I'm fairly sure this method is not a good one since its not dynamic, and is probably inefficient.

    What would be the best approach to this situation?

    Any guidance appreciated. Thanks!
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,118
    I would use a ds map to store every type of variable for every Player.

    create event of enemy
    Code:
    var hp = ds_map_create();
        hp[? string( /* id of Player A */ )] = 100;
        hp[? string( /* id of Player B */ )] = 100;
    
    player B hits the enemy or the bullet of Player B hits the enemy, doesn't matter, myid is chained.
    Code:
    var enemy = instance_place(x, y, obj_enemy);
    if (enemy != noone)
    {
        enemy.hp[? string(myid)] -= 10;
    }
    
     
    Solitary Confinement likes this.
  3. Solitary Confinement

    Solitary Confinement Member

    Joined:
    May 20, 2017
    Posts:
    22
    Thank you @Simon Gust

    Before I can proceed, I'd like to be able to track those hp variables. Here's what I have in the enemy create event:

    Note:
    . "ocon_op" refers to the master object that chains the variables of the player, the bullet and everything else it creates with the chained "myid" variable
    . con_op_id is the same as the id of the ocon_op master object, just named differently since its called from the enemy create event

    Code:
    var i;
    for (i = 0; i < instance_number(ocon_op); i += 1)
       {
       con_op_id[i] = instance_find(ocon_op,i);
       }
    
    var hp = ds_map_create();
        hp[? string( con_op_id[i] )] = 100; //A
        hp[? string( con_op_id[i+1] )] = 100; //B
    But how would I see the hp figures (100 in your example) for each of the players? I tried the following in the draw gui event:

    Code:
    draw_text(1000,200,"plA   "+string(con_op_id))
    This correctly shows me the ids of the two players' master objects (though the formatting is wierd and shows as {id1, id2} }). I tried adding con_op_id.hp to the string above but that gave me an illegal array operation error.
     
  4. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,118
    There are 2 structures here. One is your array with the ids in it and the others a map with the values accessed by the ids.
    Each time you want to receive a value (such as hp), you would have extract them from the map first.

    Before I'll show you, you need to remove the var decleration for the ds map, this is a mistake on my part.

    Code:
    var hp_A = hp[? string(con_op_id[0])];
    draw_text(1000, 200, "player A health: " + string(hp_A));
    
     
  5. Solitary Confinement

    Solitary Confinement Member

    Joined:
    May 20, 2017
    Posts:
    22
    I'm getting out of range errors, or an error where the hp variable in
    Code:
    var hp_A = hp[? string(con_op_id[0])];
    was not recognized.

    Here's what I have:

    create:
    Code:
    var i;
    for (i = 0; i < instance_number(ocon_op); i += 1)
       {
       con_op_id[i] = instance_find(ocon_op,i);
       }
      
    hp = ds_map_create();
    hp[? string( con_op_id[i] )] = 100; //A
    hp[? string( con_op_id[i+1] )] = 100; //B
    var hp_A = hp[? string(con_op_id[0])]
    
    draw gui:
    Code:
    draw_text(1000, 200, "player A health: " + string(hp_A));
    
    //draw_text(1000, 220, "player B health: " + string(hp_B));
    /*hoping all players' hp vars can be tracked.
    Thanks again.
     
    Last edited: Jul 12, 2019 at 3:52 AM
  6. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,118
    2 Things. Like any other variable, the ds map named hp must be in scope with other variables.
    E.g. you cant access a variable created in objectA in another object just like that.

    Secondly, you are creatin
    con_op_id as an array and you fill in position 0 to however many objects are.
    2 lines later you are accessing array positions out of range.
    Notice, you use i again, but i has already passed every array index and was not reset to 0 beforehand.
     
    Solitary Confinement likes this.
  7. Solitary Confinement

    Solitary Confinement Member

    Joined:
    May 20, 2017
    Posts:
    22
    Thanks again for the guidance.

    I can now see the instance ids, along with the hp variables, and I can access variables for con_op_id.

    My most recent attempt:

    Create:
    Code:
    var i;
    for (i = 0; i < instance_number(ocon_op); i += 1)
       {
       con_op_id[i] = instance_find(ocon_op,i);
       }
     
    hp = ds_map_create();
    hp[? string( con_op_id[0] )] = con_op_id[0].hp_current; //A
    hp[? string( con_op_id[1] )] = con_op_id[1].hp_current; //B
    
    hp_A = hp[? string(con_op_id[0])]
    hp_B = hp[? string(con_op_id[1])]
    Draw gui:

    Code:
    draw_text(1000, 200, "player A health: " + string(hp_A));
    draw_text(1000, 220, "player B health: " + string(hp_B));
     
    Last edited: Jul 13, 2019 at 6:21 PM

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