GMS 2 Parent system GMS2

R

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 ^^
 
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 ^_^
 
R

Rotciv45

Guest
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 ^_^
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?
 

Edmanbosch

Member
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?
If you want it to work with rotation, just make sure you set the turret's rotation to the vehicle's rotation.
 
R

Rotciv45

Guest
If you want it to work with rotation, just make sure you set the turret's rotation to the vehicle's rotation.
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.
 

Edmanbosch

Member
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.
Where are your origin points located?
 
R

Rotciv45

Guest
Where are your origin points located?
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.
 

Edmanbosch

Member
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.
What are you using to give them the same direction?
 

TheouAegis

Member
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.
 
R

Rotciv45

Guest
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.
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:

TheouAegis

Member
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.
 
Top