GameMaker: Studio

GameMaker: Studio

View Stats:
Get x and y values of an object?
What function can I use to return the x and y values of an object?
< >
Showing 1-4 of 4 comments
Blyxx Aug 25, 2013 @ 1:41pm 
x and y values are built-in variables, for a particular object you can use [instanceID].x and [instanceID].y to reference them from other objects. Sample:

if (111111.x < 0)
{
111111.h_speed = -111111.h_speed;
}

That would make instance 111111 change its horizontal direction if it went off screen to the left.

You can also make use of an object's own x and y without the prefix if the object itself is calling the code:

if (x < 0)
{
h_speed = -h_speed;
}

You can use the shortcut other.x or other.y to refer to the other object in a collision event. Code in a theoretical left wall border object, in its collision event:


if (other.x < 0)
{
other.h_speed = -other.h_speed;
}

Oh, one more thing I wanted to add. You can make a reference to something like obj_myMovingThing.x, but that is really referring to the x of all instances of that object at once (an array of x values), so it won't work right if you're trying to get the x of a particular instance.
Last edited by Blyxx; Aug 25, 2013 @ 1:45pm
Keys Aug 27, 2013 @ 3:22am 
Wow, way to overcomplicate things x)
here you go:
obj_name.x
obj_name.y
Well "obj_name" is used if you want to get all the objects with that name or if there is only one object with that name. On the other hand, the instance_id is unique to every instance.
Dabbydoodaar Apr 20, 2017 @ 4:47pm 
Originally posted by Aᴄᴄᴀᴇʟ ̺͆|̺͆ ⒹⓍ:
Well "obj_name" is used if you want to get all the objects with that name or if there is only one object with that name. On the other hand, the instance_id is unique to every instance.
+1
< >
Showing 1-4 of 4 comments
Per page: 1530 50

Date Posted: Aug 25, 2013 @ 12:55pm
Posts: 4