# Homing missile wrap screen code?

27 replies to this topic

### #1 decroded

decroded

GMC Member

• GMC Member
• 548 posts
• Version:GM8

Posted 29 April 2012 - 11:52 AM

Hi sorry I'm still not very good with these kind of functions so after a little help please.
Its Asteroids game so I screen wrapping on the enemies and bullets working fine.
I basic have homing missile code working ok, now I'm trying to work out how to calculate if it is target is closer by wrapping or not as per diagram below (green is less distance).

So far I have code below but I'm a bit stuck with checking the distance from x/y/ to edge of screen + distance from other edge to target x/y in all 4 directions

obj_homing_missile step event...
```//--TARGET TRACKING...
if !instance_exists(target) target=scr_target_closest_enemy();
if instance_exists(target)
{
//--check if closer to wrap:
/*
...
*/
} else {
}
speed=min(max_speed,speed);
image_angle=direction;
```

Thanks!

Edited by decroded, 29 April 2012 - 12:11 PM.

• 0

### #2 MasterOfKings

MasterOfKings

The True Master

• GMC Member
• 4888 posts
• Version:GM8

Posted 29 April 2012 - 12:57 PM

`dis=point_distance(x-room_width,y,target.x,target.y);`
That should find the distance between the missile and target if it wrapped (the green line). Using both that, and the other distance, you can determine the missile's movement.

Edited by MasterOfKings, 29 April 2012 - 12:58 PM.

• 0

### #3 dhtikna

dhtikna

GMC Member

• GMC Member
• 497 posts

Posted 29 April 2012 - 01:59 PM

I dont think MasterOfKings is correct. He is assuming that you only wrap horizontally and "dis" did not include the distance needed in order to wrap. Lets assign some variables:

Now, we know all the variables except for Y. We also know that the line AC and the line DB have the same slope.

From that we can say:

y1-Y/x1-room_width = Y-y2/0-x2

from this we can find Y = (y1*x2 - y2*x1 - y2*room_width)/(x2-x1+room_width)

So the code would be:

```
direct_dis = point_distance(x,y,target.x,target.y);

warp_y = ((y*target.x)-(target.y*x)-(target.y*room_width))/(target.x - x + room_width);

warp_dis = point_distance(x,y,room_width,warp_y) + point_distance(0,warp_y,target.x,target.y);

```

EDIT:

Wait , i am assuming that when you wrap, your y value remains the same but only your x changes. Right?

Edited by dhtikna, 29 April 2012 - 02:21 PM.

• 0

### #4 MasterOfKings

MasterOfKings

The True Master

• GMC Member
• 4888 posts
• Version:GM8

Posted 29 April 2012 - 02:25 PM

True, it only works horizontally, but what do you mean by "distance needed in order to wrap"? The code I provided basically assumes the missile is on the left side of the room.

Therefore, the distance between that point and the target should be the same as if both distances are added together.

It's too late at night for me to think thoroughly.

EDIT: I just realised what I've given only works if the target is on the left, and the missile is on the right.

Edited by MasterOfKings, 29 April 2012 - 02:28 PM.

• 0

### #5 decroded

decroded

GMC Member

• GMC Member
• 548 posts
• Version:GM8

Posted 29 April 2012 - 02:28 PM

I dont think MasterOfKings is correct. He is assuming that you only wrap horizontally and "dis" did not include the distance needed in order to wrap. Lets assign some variables:

Now, we know all the variables except for Y. We also know that the line AC and the line DB have the same slope.

From that we can say:

y1-Y/x1-room_width = Y-y2/0-x2

from this we can find Y = (y1*x2 - y2*x1 - y2*room_width)/(x2-x1+room_width)

So the code would be:

```
direct_dis = point_distance(x,y,target.x,target.y);

warp_y = ((y*target.x)-(target.y*x)-(target.y*room_width))/(target.x - x + room_width);

warp_dis = point_distance(x,y,room_width,warp_y) + point_distance(0,warp_y,target.x,target.y);

```

Hmm sorry I'm not totally rly following how that is the solution or how I can work out the direction in the case of a wrap
Also, just to be clearer yes I'm trying to wrap any and all directions including a possible combination of both a side and the top/bottom of screen at once, such as in the Case 1 below...

Starting to hurt my brain now lol
There will be many uses for this once it is cracked though.
I do imagine though that some sort of loop or series of tests is needed to test all the possibilities and return which was shortest.

Edited by decroded, 29 April 2012 - 02:31 PM.

• 0

### #6 MasterOfKings

MasterOfKings

The True Master

• GMC Member
• 4888 posts
• Version:GM8

Posted 29 April 2012 - 02:37 PM

I'm too tired to think of any examples, but basically you'll want to check the sides depending on which quarter you're in. When in top-left, you check left and top wrapping (if target is in top-right or bottom-right), and so forth.

If you want the direction, it's easier to just assume the missile's point is elsewhere. Using the fore-mentioned example, if we check the left wrap, the missile point is to the right of room (adding room_width to the x). Checking top wrap, the point should be at the bottom of the room (add room_height to the y).
• 0

### #7 decroded

decroded

GMC Member

• GMC Member
• 548 posts
• Version:GM8

Posted 29 April 2012 - 02:43 PM

I'm too tired to think of any examples, but basically you'll want to check the sides depending on which quarter you're in. When in top-left, you check left and top wrapping (if target is in top-right or bottom-right), and so forth.

If you want the direction, it's easier to just assume the missile's point is elsewhere. Using the fore-mentioned example, if we check the left wrap, the missile point is to the right of room (adding room_width to the x). Checking top wrap, the point should be at the bottom of the room (add room_height to the y).

Hmmm yeah checking corners a good idea thanks I'll keep trying.
I'm trying to write a workaround atm that moves the object to the various "outsides" of the room to run the comparisons and get direction, then move back afterwards
• 0

### #8 dhtikna

dhtikna

GMC Member

• GMC Member
• 497 posts

Posted 29 April 2012 - 02:45 PM

I forgot about the vertical wrap

First i need to know this, if I move off of the screen will my y position be the same when I wrap? Could you possibly provide us the wrap code?

The code I gave above can be used to check the wraps distance ONLY if the missile wraps to the right. Here is the full code:

```
direct_dis = point_distance(x,y,target.x,target.y);

warp_y_right = ((y*target.x)-(target.y*x)-(target.y*room_width))/(target.x - x + room_width);
warp_dis_right = point_distance(x,y,room_width,warp_y_right) + point_distance(0,warp_y_right,target.x,target.y);

warp_y_left = ((y*target.x)-(y*room_width)+(target.y*x))/(target.x-room_width+x)
warp_dis_left = point_distance(x,y,0,warp_y_left) + point_distance(room_width,warp_y_left,target.x,target.y);

```

Don't worry if you don't understand the code, unfortunately it involves some geometry. In the US i think that's 10th grade math but the principle is that when you warp, the two lines(green lines) are parallel which mean that their slope is the same.

Now what you need to do is check which one is maximum out of
1)direct distance
2)Left wrap distance
3)Right wrap distance

Then depending on which one is maximum set the directions as:
direct:
point_direction(x,y,target.x,target.y);
Left:
point_direction(x,y,0,warp_y_left);
Right:
point_direction(x,y,room_width,warp_y_right);

I don't have the concentration to check for vertical wraps but I try that latter.

PS: I'll try to PM you the final code you need to implement later

Edited by dhtikna, 29 April 2012 - 02:52 PM.

• 0

### #9 MasterOfKings

MasterOfKings

The True Master

• GMC Member
• 4888 posts
• Version:GM8

Posted 29 April 2012 - 02:50 PM

Again, you don't need all that math.
Moving the starting point means you form a single line where the distance is easily calculated.
• 0

### #10 decroded

decroded

GMC Member

• GMC Member
• 548 posts
• Version:GM8

Posted 29 April 2012 - 04:06 PM

Well bit of a sledgehammer approach that isn't pretty but it seems to work really well as far as I can tell
Didn't need to move the objects I don't know what I was thinking there (late here too lol).

Step event for obj_homing_missile:
```//--KILL ANY STRAY MISSILES:
if scr_outside_view(id,128) instance_destroy();

//--PICK CLOSEST TARGET:
if !instance_exists(target) target=scr_target_closest_enemy();

//--LOOK FOR SHORTEST DISTANCE (DIRECT VS WRAP):
if instance_exists(target)
{
var testx,texty,bestx,besty;
direct_dist = point_distance(x,y,target.x,target.y);
best_dist=direct_dist+1;
//--left
testx=0-(room_width-target.x);   //left
testy=target.y;
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--left+top
testx=0-(room_width-target.x);   //left
testy=0-(room_height-target.y);  //top
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--top
testx=target.x;                //--
testy=0-(room_height-target.y);  //top
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--right+top
testx=room_width+target.x;     //right
testy=0-(room_height-target.y);  //top
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--right
testx=room_width+target.x;     //right
testy=target.y;                //--
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--right+bottom
testx=room_width+target.x;     //right
testy=room_height+target.y;    //bottom
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--bottom
testx=target.x                 //--
testy=room_height+target.y;    //bottom
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--bottom+left
testx=0-(room_width-target.x);   //left
testy=room_height+target.y;            //bottom
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
if best_dist < direct_dist
{
} else {
}
} else {
motion_add(direction,acc);  //fly off into the sunest
}
speed=min(max_speed,speed);

event_inherited();  //just has stuff like custom collision testing common to projectiles...
```

...and similar code in the target picking script so it can pick a target through a wrap:
```//--TARGET CLOSEST ENEMY:
var ii,closest_dist,closest_target,direct_dist,best_dist,testx,texty,bestx,besty;;
closest_target=noone;
closest_dist = 99999;
for (i=0; i<instance_count; i+=1)
{
ii = instance_id[i];
if (instance_exists(ii))
{
if (object_get_parent(ii.object_index) == par_enemy)
{
//--FIND SHORTEST PATH TO TARGET (DIRECT VS WRAP:
direct_dist = point_distance(x,y,ii.x,ii.y);
best_dist=direct_dist+1;
//--left
testx=0-(room_width-ii.x);   //left
testy=ii.y;
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--left+top
testx=0-(room_width-ii.x);   //left
testy=0-(room_height-ii.y);  //top
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--top
testx=ii.x;                //--
testy=0-(room_height-ii.y);  //top
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--right+top
testx=room_width+ii.x;     //right
testy=0-(room_height-ii.y);  //top
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--right
testx=room_width+ii.x;     //right
testy=ii.y;                //--
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--right+bottom
testx=room_width+ii.x;     //right
testy=room_height+ii.y;    //bottom
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--bottom
testx=ii.x                 //--
testy=room_height+ii.y;    //bottom
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
//--bottom+left
testx=0-(room_width-ii.x);   //left
testy=room_height+ii.y;            //bottom
if point_distance(x,y,testx,testy) < best_dist
{
best_dist=point_distance(x,y,testx,testy);
bestx=testx;
besty=testy;
}
if (best_dist < direct_dist) and (best_dist < closest_dist)
{
closest_dist=best_dist;
closest_target=ii;
} else if direct_dist < closest_dist {
closest_dist=direct_dist;
closest_target=ii;
}
}
}
}
return closest_target;
```

Probably will make a nicer script call instead of all that repeated code but its too late now
• 0

### #11 icuurd12b42

icuurd12b42

Self Formed Sentient

• Retired Staff
• 15056 posts
• Version:GM:Studio

Posted 29 April 2012 - 09:06 PM

```ins = instance_nearest(x, y, Target);
targetx = ins.x;
targety = ins.y;
d1 = point_distance(x, y, targtetx, targety);
d2 = point_distance(x, y, targtetx + room_width, targety);
d3 = point_distance(x, y, targtetx - room_width, targety);

switch(min(d1, d2, d3))
{
case d1:
break;
case d2;
targetx += room_width;
break;
case d3;
targetx -= room_width;
break;
}

move_towards_point(targetx, targety, 5);
```

instance_nearest(x,y,Target); is wrong and need to also abide by the wrapping rule...

```//instance_nearest_hwrap(x,y,Target)
var d, d1, d2, d3, found, maxd;
maxd = 9999999999;
found = noone;
with(argument2)
{

d1 = point_distance(argument0, argument1, x, y);
d2 = point_distance(argument0, argument1, x + room_width, y);
d3 = point_distance(argument0, argument1, x - room_width, y);

switch(min(d1, d2, d3))
{
case d1:
d = d1;
break;
case d2;
d = d2
break;
case d3;
d = d3;
break;
}

if (d < maxd)
{
maxd = d;
found = id;
}
}
return found;
```

Modified to return the x,y of the target (does what script 1 does)
```//instance_nearest_hwrap_with_coords(x,y,Target)
var tx,ty, d, d1, d2, d3, found, maxd;
maxd = 9999999999;
found = noone;
//cords stored in instance variable __targetx, __targety;
__targetx = 0;
__targety = 0;
with(argument2)
{

d1 = point_distance(argument0, argument1, x, y);
d2 = point_distance(argument0, argument1, x + room_width, y);
d3 = point_distance(argument0, argument1, x - room_width, y);
tx = x;
ty = y;
switch(min(d1, d2, d3))
{
case d1:
d = d1;
break;
case d2;
d = d2
tx+=room_width;
break;
case d3;
d = d3;
tx-=room_width;
break;
}

if (d < maxd)
{
maxd = d;
found = id;
other.__targetx = tx;
other.__targety = ty;
}
}
return found;
```

• 0

### #12 decroded

decroded

GMC Member

• GMC Member
• 548 posts
• Version:GM8

Posted 30 April 2012 - 02:53 AM

Thanks for instance_nearest(), assuming this works with parents then good to know for normal use but I don't think in this case.

Hmmmm...overall looks much neater but it looks to me (sry if I'm missing something!) like it doesn't wrap vertically?
And even if modified to do that, then wouldn't handle left + top wrap, bottom + right wihout alot more conditions.
And by the time all those extra conditions were entered, extra variables added and switch conditions it probably wouldn't be much less code except the core test logic would have to be repeated inside the switch (didn't feel like looking for 2 lots of bugs at 2am haha).
Also it would recalculate the target every time (easy fix)?
And I use the set_motion() instead of move_towards() because I think it gives a more "realistic" inertia effect (especially when you buy cheap missiles lol).

Anyway I've ended up with with code below in obj_homing_missile step event:
```if fuel > 0
{
//--PICK CLOSEST TARGET:
if !instance_exists(target) target=scr_target_closest_enemy();

//--LOOK FOR SHORTEST DISTANCE (DIRECT VS WRAP):
if instance_exists(target)
{
var target_pos;
scr_direct_vs_wrap(x, y, target.x, target.y, "target_pos");               //check if closer to wrap or go direct.
motion_add(point_direction(x, y, target_pos[0], target_pos[1]), acc);   //aim for target.
} else {
motion_add(direction,acc);  //fly off into the sunest...
}
speed=min(max_speed,speed);
}
fuel-=1;

event_inherited();  //parent has screen wrap, collision test loop etc. common to all the projectile weapons.
```

And this is the scr_direct_vs_wrap():
```//////////////////////////////////////////////////////////////////////////
//Find smallest distance between 2 points including via screen wrap.
//argument0: x1
//argument1: y1
//argument2: x2
//argument3: y2
//argument4: name (as string) of the var to "return" the best x/y as array
//////////////////////////////////////////////////////////////////////////
var testx, testy,i, bestx, besty;

show_message(argument4);

//-DIRECT TO TARGET:
testx[0] = argument2;                  //DIRECT
testy[0] = argument3;                  //DIRECT
//--WRAP LEFT:
testx[1] = 0-(room_width-argument2);   //LEFT
testy[1] = argument3;
//--WRAP LEFT-TOP:
testx[2] = 0-(room_width-argument2);   //LEFT
testy[2] = 0-(room_height-argument3);  //TOP
//--WRAP TOP:
testx[3] = argument2;
testy[3] = 0-(room_height-argument3);  //TOP
//--WRAP RIGHT-TOP:
testx[4] = room_width+argument2;       //RIGHT
testy[4] = 0-(room_height-argument3);  //TOP
//--WRAP RIGHT:
testx[5] = room_width+argument2;       //RIGHT
testy[5] = argument3;
//--WRAP RIGHT-BOTTOM:
testx[6] = room_width+argument2;       //RIGHT
testy[6] = room_height+argument3;      //BOTTOM
//--WRAP BOTTOM:
testx[7] = argument2
testy[7] = room_height+argument3;      //BOTTOM
//--WRAP BOTTOM-LEFT:
testx[8] = 0-(room_width-argument2);   //LEFT
testy[8] = room_height+argument3;      //BOTTOM

//FIND SHORTEST DISTANCE:
best_dist=99999;
for (i=0;i<=8;i+=1)
{
if point_distance(argument0, argument1, testx[i], testy[i]) < best_dist
{
best_dist=point_distance(argument0, argument1, testx[i], testy[i]);
bestx=testx[i];
besty=testy[i];
}
}
//--RETURN CLOSEST POS:
variable_local_array_set(argument4,0,bestx);
variable_local_array_set(argument4,1,besty);
```

Now this code worked perfectly UNTIL I moved it to the script and now I'm stuck trying to return the best x/y into the target_pos array (by passing array name as string to the script).
Its driving me nuts lol the error is:

___________________________________________
ERROR in
action number 1
of Step Event
for object obj_hero_homing_missile:

Error in code at line 11:
^
at position 41: Unknown variable target_pos

Can anyone please see what I've done wrong here?
I know its something wrong with the way I'm "returning" the array at the bottom of the script or something but I'm going blind and or crazy here lol

Edited by decroded, 30 April 2012 - 02:55 AM.

• 0

### #13 icuurd12b42

icuurd12b42

Self Formed Sentient

• Retired Staff
• 15056 posts
• Version:GM:Studio

Posted 30 April 2012 - 03:29 AM

[quote name='decroded' date='29 April 2012 - 10:53 PM' timestamp='1335754422' post='3948611']
[quote name='icuurd12b42' date='30 April 2012 - 07:06 AM' timestamp='1335733616' post='3948456'][/quote]

Thanks for instance_nearest(), assuming this works with parents then good to know for normal use but I don't think in this case.

Hmmmm...overall looks much neater but it looks to me (sry if I'm missing something!) like it doesn't wrap vertically?
[/quote]
just add d3 and d4 for y-room_height and y+room_hieght

[quote]
```variable_local_array_set(argument4,0,bestx);
variable_local_array_set(argument4,1,besty);
```

Now this code worked perfectly UNTIL I moved it to the script and now I'm stuck trying to return the best x/y into the target_pos array (by passing array name as string to the script).
Its driving me nuts lol the error is:
[quote]

I showed how to set local variables for multiple values returned via __temp transfer variables. Using a string variable_local_array_set("x",value) to bypass GM's limitation in not the answer when it offers such more effective streamlined method.
• 0

### #14 icuurd12b42

icuurd12b42

Self Formed Sentient

• Retired Staff
• 15056 posts
• Version:GM:Studio

Posted 30 April 2012 - 03:29 AM

Thanks for instance_nearest(), assuming this works with parents then good to know for normal use but I don't think in this case.

Hmmmm...overall looks much neater but it looks to me (sry if I'm missing something!) like it doesn't wrap vertically?

just add d3 and d4 for y-room_height and y+room_hieght

```variable_local_array_set(argument4,0,bestx);
variable_local_array_set(argument4,1,besty);
```

Now this code worked perfectly UNTIL I moved it to the script and now I'm stuck trying to return the best x/y into the target_pos array (by passing array name as string to the script).
Its driving me nuts lol the error is:

I showed how to set local variables for multiple values returned via __temp transfer variables. Using a string variable_local_array_set("x",value) to bypass GM's limitation in not the answer when it offers such more effective streamlined method.
• 1

### #15 decroded

decroded

GMC Member

• GMC Member
• 548 posts
• Version:GM8

Posted 30 April 2012 - 03:42 AM

Thanks for instance_nearest(), assuming this works with parents then good to know for normal use but I don't think in this case.

Hmmmm...overall looks much neater but it looks to me (sry if I'm missing something!) like it doesn't wrap vertically?

just add d3 and d4 for y-room_height and y+room_hieght

```variable_local_array_set(argument4,0,bestx);
variable_local_array_set(argument4,1,besty);
```

Now this code worked perfectly UNTIL I moved it to the script and now I'm stuck trying to return the best x/y into the target_pos array (by passing array name as string to the script).
Its driving me nuts lol the error is:

I showed how to set local variables for multiple values returned via __temp transfer variables. Using a string variable_local_array_set("x",value) to bypass GM's limitation in not the answer when it offers such more effective streamlined method.

Ok thanks, and I think I would need to add another 4 conditions for the corners to cover all combinations?

The only point of this project is to learn so can I please know how to bypass the limitation anyway i.e. what I was doing wrong here?
Even when I set 2 separate vars instead of array it says those vars don't exist.
Surely its not local to the script.

Also I want to extend the use of this script so would be really handy to return 2 values instead of 1 for different objects with different var names.
Unless I'm just not understanding the use of the ___ I just don't know what that means sorry and not sure what to search for to find out

Thanks.

Edited by decroded, 30 April 2012 - 03:47 AM.

• 0

### #16 icuurd12b42

icuurd12b42

Self Formed Sentient

• Retired Staff
• 15056 posts
• Version:GM:Studio

Posted 30 April 2012 - 04:04 AM

I guess you could add as many alternate positions as you wanted

what are you doing wrong? You tried to set a variable using set_local, which uses a string to create a local variable (AKA an instance variable in the proper new terminology)

variable_local_set("x",10)
is the same as
x=10;

same deal with array set...

//argument4: name (as string) of the var to "return" the best x/y as array

Your going the extra mile here to make it more flexible in use. however this extra mile will cost you the race. (slow and clumsy)

It is much simpler to comment that your function will set 2 temp variables __targetx and __targety...

for example
ins = instance_nearest_hwrap_with_coords(x,y,TargetObj)
if(ins != noone)
{
__targetX and __targetY are set.
}

instead of telling people they have to setup or define the name of the array to fill

Not sure why your code complained. maybe you made a typo in the name of the array. of forgot to use ""
• 1

### #17 decroded

decroded

GMC Member

• GMC Member
• 548 posts
• Version:GM8

Posted 30 April 2012 - 04:09 AM

I guess you could add as many alternate positions as you wanted

what are you doing wrong? You tried to set a variable using set_local, which uses a string to create a local variable (AKA an instance variable in the proper new terminology)

variable_local_set("x",10)
is the same as
x=10;

same deal with array set...

//argument4: name (as string) of the var to "return" the best x/y as array

Your going the extra mile here to make it more flexible in use. however this extra mile will cost you the race. (slow and clumsy)

It is much simpler to comment that your function will set 2 temp variables __targetx and __targety...

for example
ins = instance_nearest_hwrap_with_coords(x,y,TargetObj)
if(ins != noone)
{
__targetX and __targetY are set.
}

instead of telling people they have to setup or define the name of the array to fill

Not sure why your code complained. maybe you made a typo in the name of the array. of forgot to use ""

Oohhh I see sorry I'm such a derpa.

Yeah still don't know why the code complains though I checked all the "" and typing
• 0

### #18 icuurd12b42

icuurd12b42

Self Formed Sentient

• Retired Staff
• 15056 posts
• Version:GM:Studio

Posted 30 April 2012 - 04:37 AM

I don't see how your code has failed. the only trouble is bestx and besty not being set if and instance is farther that 99999

variable_local_array_set
variable_local_set

are getting phased out. maybe you are using GM:HTML5 or Studio.
• 0

### #19 decroded

decroded

GMC Member

• GMC Member
• 548 posts
• Version:GM8

Posted 30 April 2012 - 04:41 AM

I don't see how your code has failed. the only trouble is bestx and besty not being set if and instance is farther that 99999

variable_local_array_set
variable_local_set

are getting phased out. maybe you are using GM:HTML5 or Studio.

8.0 Pro, I should have mentioned that.
Good to know those functions phasing out so I won't waste my time trying to learn it!
• 0

### #20 Yourself

Yourself

The Ultimate Pronoun

• Retired Staff
• 7352 posts
• Version:Unknown

Posted 30 April 2012 - 04:51 AM

You don't need to actually check which wrapping direction is the closest. The problem is actually far simpler than that. Assume that your missile was always in the very center of the screen. Then, as long as we wrapped the target to that screen, the regular distance between the two would always be the shortest. So really all we need to do is wrap the target to a "screen" centered on the missile. This is actually quite easy:

```var dx, dy;

dx = target.x - missile.x;
dy = target.y - missile.y;

while( dx > room_width / 2 ) dx -= room_width;
while( dx < -room_width / 2 ) dx += room_width;

while( dy > room_height / 2 ) dy -= room_height;
while( dy < -room_height / 2 ) dy += room_height;

aimAtX = missile.x + dx;
aimAtY = missile.y + dy;```

This could also be accomplished using the mod operator, but I figure this is easier to understand.

Edited by Yourself, 30 April 2012 - 04:52 AM.

• 3

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users