• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[solved] distance_to_object not working

J

jgaryedyen

Guest
Title is not 100% accurate but I'm not sure what to call my problem. The distance_to_object is working but it's not working for what I want it to do.
I'm trying to create a basic tower defense but I'm having some problems with the distance_to_object function. It seems to have boundaries in a square shape however I want to use it for the typical circular tower radius.

This is the step event:
if placed = false {
x = mouse_x
y = mouse_y
} else if placed = true {

if instance_exists(target) == true && distance_to_object(target) < range {
coolDown -= 1;

if coolDown == 0 {
instance_create_depth(x, y, 0, proj);
coolDown = fireRate;
}
}
}

And this is the draw event for the tower:
draw_self();

draw_set_color(c_dkgray);
draw_set_alpha(0.5);

if selected = true {
draw_circle(x, y, range + 32, false);
}

draw_set_alpha(1);

Any other advice such as how to improve my code in general is also appreciated.
(This question might have already been answered but I don't know what to search to find it)
 

Rob

Member
Title is not 100% accurate but I'm not sure what to call my problem. The distance_to_object is working but it's not working for what I want it to do.
I'm trying to create a basic tower defense but I'm having some problems with the distance_to_object function. It seems to have boundaries in a square shape however I want to use it for the typical circular tower radius.

This is the step event:
if placed = false {
x = mouse_x
y = mouse_y
} else if placed = true {

if instance_exists(target) == true && distance_to_object(target) < range {
coolDown -= 1;

if coolDown == 0 {
instance_create_depth(x, y, 0, proj);
coolDown = fireRate;
}
}
}

And this is the draw event for the tower:
draw_self();

draw_set_color(c_dkgray);
draw_set_alpha(0.5);

if selected = true {
draw_circle(x, y, range + 32, false);
}

draw_set_alpha(1);

Any other advice such as how to improve my code in general is also appreciated.
(This question might have already been answered but I don't know what to search to find it)
I think you'd be better suited using collision_circle. It returns an id or noone, which means if any object is within the circle's radius, the function will return its id.

To get and store the id of that instance inside the circle you'd just run this in your step event:

Code:
target = collision_circle(x, y, radius, obj_enemy, false, false);

if (target != noone){
   //there's a target inside the circle so now you can do stuff to it/at it
}
The limitation of this function is that it will only return 1 id so if there are multiple enemies inside the circle, only one of them is getting shot at. If you're feeling confident and you want to store the id's of every target that's within the circle, you can use collision_circle_list

The insert icon/toolthingy at the top allows you to format code in a nicer looking way so that it's easier to read.
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
distance_to_object takes collision masks into account. If the mask of the calling instance is a rectangle, it's gonna be rectangular.

point_distance is circular, always. If it's implemented with simple math according to the Pythagorean Theorem, it should even be faster than collision functions when there's only one potential, pre-determined target. (Not that collision functions don't work, but I believe there may have been a mixup between the two distance functions, so just to clear that up...)
 
Top