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

GMS 2 Parent system GMS2

Discussion in 'Programming' started by Rotciv45, Aug 27, 2017.

  1. Rotciv45

    Rotciv45 Guest

    Hi guys, I'm writting this because I need help with a project I'm currently working on with GMS 2.

    I created an object (I call it vehicle) and a turret that i placed on it. Then, in the object editor i set the vehicle as the turret's parent, so when i move the first with arrows the second moves with it. But when i rotate the vehicle (using mouse x and y) the turret moves around its origin point (which is quite logical but not what i want).

    i would like to be able to pin the turret to the vehicle (so it stays at the same place on it)

    Thank for the answers

    PS:sorry for my bad English, I'm doing my best ^^
     
  2. BlessHayGaming

    BlessHayGaming Member

    Joined:
    Jun 20, 2016
    Posts:
    83
    GameMaker's parent system is about inheriting code, thus it is not what you need in this case. Instead you should move the turret to follow the vehicle through code - usually this is done in the end step event (though you just need to make sure that it is done after moving the vehicle, so you could also do it from the vehicle itself) :)

    One way to do it, would be to only place the vehicle in the room, and then in the vehicle you create the turret at asign it to a variable.
    Code:
    my_turret = instance_create(x,y,obj_turret);
    Then, after your movement code for the vehicle, you simply need to reposition the turret.
    Code:
    with my_turret {
     x = other.x;
     y = other.y;
    }
    Hope this somewhat helps you ^_^
     
  3. Rotciv45

    Rotciv45 Guest

    Thanks for the help, actualy I'm using the dnd system in GMS2 so I don't really know how to use code (but i can still learn it ^^), are you sure this code also works with rotating objects?
     
  4. Edmanbosch

    Edmanbosch Member

    Joined:
    Jan 6, 2017
    Posts:
    180
    If you want it to work with rotation, just make sure you set the turret's rotation to the vehicle's rotation.
     
  5. Rotciv45

    Rotciv45 Guest

    actually it's what i did but the origin point of the turret is different from the one of the vehicle so when the vehicle rotates, the turret rotates on itself without following the vehicle's rotation.
     
  6. Edmanbosch

    Edmanbosch Member

    Joined:
    Jan 6, 2017
    Posts:
    180
    Where are your origin points located?
     
  7. Rotciv45

    Rotciv45 Guest

    The turret's origin point is on its center, same for the vehicle's one, I thought of just overlapping both of their center but I'm thinking of creating another vehicle type with 2 turrets so it wouldn't work.
     
  8. Edmanbosch

    Edmanbosch Member

    Joined:
    Jan 6, 2017
    Posts:
    180
    What are you using to give them the same direction?
     
  9. Rotciv45

    Rotciv45 Guest

    I'm using the mouse location to rotate the vehicle, the turret can't actually move but i will certainly use keyboard keys
     
  10. Edmanbosch

    Edmanbosch Member

    Joined:
    Jan 6, 2017
    Posts:
    180
    Maybe just give the turret the same direction value as the vehicle?
     
  11. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,143
    Don't set the turret's position to (x+a,y+b), set it to (x+lengthdir_x(n, image_angle), y+lengthdir_y(n,image_angle)) where n is the distance from the vehicle's origin the turret's origin is at.
     
    Edmanbosch likes this.
  12. Rotciv45

    Rotciv45 Guest

    Thanks for the help ^^ does it work if I'm using dnd?

    And how do you get the distance between origin points and the angle, I tried to create a variable but it does not work
     
    Last edited by a moderator: Aug 27, 2017
  13. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,143
    set variable spawn_length to point_distance(0,0,a,b)

    Replace a and b with the x and y offsets (x+a,y+b) you want to spawn the turret. Then use spawn_length in place of n.
     
  14. Rotciv45

    Rotciv45 Guest

    So I tried you're code but I can't make it work, there must be a problem somewhere.
     

    Attached Files:

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