GM:S 1.4 Variable access on Creation (?)

Discussion in 'Programming' started by MIchael PS, Jun 10, 2018.

  1. MIchael PS

    MIchael PS Member

    Joined:
    Aug 12, 2017
    Posts:
    95
    Hello,


    I have a small question here.
    I have an obj_A and an obj_B. Through obj_ A, I create an obj_B, “instance_create(x,y,obj_B)”, and I want to access a variable in obj_B through obj_A, to change it. I don’t want to do it with “obj_B.variable” because I have a lot of obj_Bs and I want to change only the one I am creating now.
    I have seen someone doing something like this:

    “ var B = instance_create(x,y,obj_B);
    B.variable = whatever; ”

    But that doesn’t seem to work.
    Any ideas?


    Thank you a lot for your time,
    Michael
     
  2. TheSnidr

    TheSnidr Heavy metal viking dentist GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    469
    That should work though! What goes wrong when doing that?
    Another way to do it is as follows:
    Code:
    var B = instance_create(x, y, obj_B);
    with B
    {
        variable = whatever;
    }
     
  3. Carl Nylander

    Carl Nylander Member

    Joined:
    Jul 21, 2016
    Posts:
    212
    the example you give is exactly what you need :)
    why isent it working?
     
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,955
    Instance create returns the id of created instance, so you dot notation B.variable will change the value.
     
  5. MIchael PS

    MIchael PS Member

    Joined:
    Aug 12, 2017
    Posts:
    95
    Thanks for answering.
    Well... I use this way and there is a message appearing that says:

    " local variable inverted(100001, -2147483648) not set before reading it.
    at ��ЩGct_obj_shotgun_ObjAlarm0_1 (line 2) - if inverted = true "

    Here is exactly what I am using:

    =The event where the obj_B is created:

    repeat 10
    {
    var pellets = instance_create(x+lengthdir_x(10,image_angle),y+lengthdir_y(10,image_angle),obj_enemy_pellets);
    pellets.inverted = true;
    }


    =Obj_enemy_pellets, Create ev. :

    var inverted;
    if inverted = true
    {
    direction = point_direction(obj_player.x,obj_player.y,x,y)+random_range(-30,30)
    } else
    {
    direction = point_direction(x,y,obj_player.x,obj_player.y)+random_range(-30,30)
    }
     
  6. TheSnidr

    TheSnidr Heavy metal viking dentist GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    469
    The object's create event is executed before the variable is set ;)
     
    MIchael PS likes this.
  7. MIchael PS

    MIchael PS Member

    Joined:
    Aug 12, 2017
    Posts:
    95
    So.... What should I do?
     
  8. TheSnidr

    TheSnidr Heavy metal viking dentist GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    469
    Well, you could do it like this:
    Code:
    //obj_enemy_pellets's create event
    point_direction(x,y,obj_player.x,obj_player.y)+random_range(-30,30)
    Code:
    var B = instance_create(x, y, obj_enemy_pellets);
    with B
    {
        direction = point_direction(obj_player.x,obj_player.y,x,y)+random_range(-30,30)
    }
    The latter will overwrite the former
     
    MIchael PS likes this.
  9. Carl Nylander

    Carl Nylander Member

    Joined:
    Jul 21, 2016
    Posts:
    212
    you could put the code from the create event and put in a with statment when you create the instance.

    with (instance_create()) {
    direction = point_direction(obj_player.x,obj_player.y,x,y)+random_range(-30,30)
    }
     
    MIchael PS likes this.
  10. MIchael PS

    MIchael PS Member

    Joined:
    Aug 12, 2017
    Posts:
    95
    Well it worked!
    Thank you for your 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