GameMaker: Studio

GameMaker: Studio

View Stats:
how to get the value of hspeed and vspeed of an instance
Like the topic says, i want to get the hspeed and vspeed of an instance. Thanks.
< >
Showing 1-2 of 2 comments
Pixel Prophecy Apr 17, 2014 @ 1:08pm 
You may not like my answer but I usually do this via code.

The easiest case is to store the previous X and Y positions of the object from the frame just before and subtract those values from the current coordinates. If you use the abs()-function you will always get a positive number, e.g.
my_hspeed = abs(x - x_prev);
Sera Apr 17, 2014 @ 2:15pm 
If you're talking about a seperate instance from the one looking it up, you'll need to identify which instance that is. There's a few ways to do this, but basically you're looking for the specific instance's instance id.

If you're dealing with a main object spawning something else (ie. projectiles) you can actually get the instance id from instance_create():

TrackingObj = instance_create(x, y, obj_PewPew);

This works because instance_create() returns the instance id of the newly created thing. Functions that return instance ids are basically what you're after here, and there's a lot of them, like instance_nearest(), instance_place(), or instance_find(). If you want to index instances easily, having a control object that tracks all instances of an object on a list or array might prove useful, so that could be worth investigating depending on the situation. The with() statement might also help you here; my point is there's a lot of ways, and the best one will depend on your situation.

That's the hard part. Once you have the instance id, it's really simple to find it's hspeed, vspeed, direction, or any other variable local to it. Working with our previous example of TrackingObj:

InstanceSpeed = TrackingObj.hspeed;

The important thing to take from this is "thing.variable". It's a very important, vital thing to know! (Even globals use this, with "global" taking the place of your instance ids as its own, nebulous thing.) So long as you remember that rule, the hard part is generally just going to be deciding which lookup method serves your needs best.
< >
Showing 1-2 of 2 comments
Per page: 1530 50

Date Posted: Apr 17, 2014 @ 12:12pm
Posts: 2