GML Making an object follow another object's x position

L

Le_Toto

Guest
I started learning game maker a few days ago and I have slight difficulties. For an object to move around left or right on a room, how would I make it so another object, lets say, is called test, follows the x position of the object moving left or right? As a result, test would also be moving left and right and not up and down.

I don't really need the exact code but the concept itself thanks :D
 
?

!!!!!

Guest
Hmmm, there are lots of ways of doing this. If you're using the inbuilt speed and direction, whenever you set the object's direction to left or right, change test's direction at the same time. Otherwise, you could put in the step event of test:

x=object.x

or maybe

direction=object.direction

Another alternative could be:

move_towards_point( object.x, y, speed )

It depends on what type of movement you want exactly!
 
J

JFitch

Guest
If you want it to always have the same x coordinate, use in the test object's step event:
Code:
x=obj.x
replacing "obj" with the name of the object. That works if you have exactly one instance of that object. Otherwise, you'll need to replace obj with a variable that stores the object's id.

If you want it to chase the object without going over a maximum speed, use in the test object's step event:
Code:
x=median(x-maxspeed,obj.x,x+maxspeed)
replacing "obj" as before and replacing "maxspeed" with the maximum speed you would like the test object to move.
 
Last edited by a moderator:
F

frumple

Guest
Even more simply, when you change the x of one instance, change the x of another by the same amount. You would need the instance ID of either the leading instance or the follower. Let's say you want to move the follower everytime the lead instance moves. In the Create Event add:
Code:
follower = instace_create( x , y , object_follower );
The variable follower will have the instance ID of the instance created. Then in any event that moves the lead instance you use:
Code:
x += something;
follower.x += something;
If the value of 'something' is 5, both instances will move 5 pixels to the right, regardless of where they are in the room.
 
L

Le_Toto

Guest
So basically, I am making an object move down and I want to make another object follow it.

I get this error:
Variable Block.i(100005, -2147483648) not set before reading it.
at gml_Script_move_down (line 9) - global.gx = x;

The object moving down is called Block and the object following it is called Ghost.

This is the script i'm getting errors from
for(i = 0; i < 4; i += 1) {
with(blocks) {
global.gx = x;
global.gy = y*(32*4);
}
}

FYI,
This is from a script from a step event on the Ghost object:
for(i = 0; i > 4; i += 1) {
with(ghost_s){
x = global.gx
y = global.gy
}
}

And finally this is a script from an object with no sprite where all the duplicates of Ghost objects are created to be produced into the room
gblocks[0] = instance_create(x, y, Ghost).id;
gblocks[1] = instance_create(x, y, Ghost).id;
gblocks[2] = instance_create(x, y, Ghost).id;
gblocks[3] = instance_create(x, y, Ghost).id;

global.gx[0] = 0;global.gy[0] = 0;
global.gx[1] = 0;global.gy[1] = 0;
global.gx[2] = 0;global.gy[2] = 0;
global.gx[3] = 0;global.gy[3] = 0;

So what happens is that ghost objects are being created and their positions are initially at the object with no sprite placed randomly in the room.
Using the step event on the Ghost object, the position of the ghost objects should be updated everytime but it keeps giving me these errors.

Help pls?
 
?

!!!!!

Guest
Are you sure that's the script you're getting errors from? You only gave 6 lines and the error appears line 9.

I think you're missing the point of for loops. If you don't use i in the loop then it will just repeat the same thing in the same position 4 times. Incidentally, if global.gx is an array then you will need to refer to it with an index in square brackets to get a value stored in it. ie global.gx[ i ]


Also in your 2nd script:

for(i = 0; i > 4; i += 1)


i > 4 won't make a very exciting for loop!

 
Last edited by a moderator:

kburkhart84

Firehammer Games
In any project where I have an object follow another object, I create a couple of scripts for this. One is setParent(id, xOffset, yOffset) and the other is followParent(). The set parent script sets a couple of variables that are needed for the following, including calculating the angle and distance between 0,0 and xOffset,yOffset. Those values are needed in the followParent() script for the lengthdirX/Y() function calls and since it doesn't change I can pre-calculate them. And even if it does change another call to setParent() will fix it. So my "Ship" creates a "turret" object(or a person creates a "gunpoint tip" or whatever), and using the with statement I have the new object call setParent().

Note that an easy way to make this semi-automatic as well is to create a parent object. For this though, I'm referring to parent/child objects in GMS's system. The parent object would have the followParent() script in it's end step event, which let's it follow the parent right after the parent moves. It also has the advantage of that you can put a step event in it without having to call event_inherited(), though if you want extra code in the end step event you still need that function call. Then, any object that I want to be able to follow a parent, I make sure to make it a child of this one controller-type object, and I make sure when it gets created to call the setParent() script as well.
 
Top