Help using ID to display stats of a selected object

Discussion in 'Programming' started by Darren, Jan 11, 2019.

  1. Darren

    Darren Member

    Joined:
    Jun 25, 2016
    Posts:
    105
    If I have multiple walls, each the same object, and I define their health for example using hp = 100, and then I want to read out the HP of the selected object from a different object, how would I go about doing that?

    So for example I have

    selected = false;
    obj_name = id;
    hp = 100;​

    So I know the ID of the object, but then I want to write the stats for that specific object within my information object, so I use the with function

    with obj_building_info
    {
    draw_text(x,y,"Wall");
    }​
    but then how would I draw the hp of the selected object, for example? I want to be able to say with the building info object, display hp of the ID of the selected object. Can't seem to get it working, and I have tried the manual and I'm still not grasping how I can then use that ID to help display each stat in another object.

    Thanks!
     
  2. Bayesian

    Bayesian Member

    Joined:
    Sep 13, 2016
    Posts:
    402
    If the wall object is calling this

    Code:
    with obj_building_info
    {
    draw_text(x,y,"Wall");
    }
    do draw_text(x,y,string(other.hp));
     
  3. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,086
    First you need a variable that can hold the id of the selected wall (I think you wanted to use obj_name for it?)
    When you click on a wall, example
    Code:
    if (mouse_check_button_pressed(mb_left))
    {
       instance_selected = instance_position(mouse_x, mouse_y, obj_wall);
    }
    
    Then, if you clicked on something, instance_selected should get the id of the instances you've clicked on.
    If you clicked nothing, noone is always returned.
    Code:
    with (instance_selected)
    {
       draw_text(x, y, "health: " + string(hp));
    }
    
     

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