Jump to content

Sign in to follow this  
terminus

Testing for a matching position using a loop

Recommended Posts

Hi all,

I'm trying to get a unit, to go to a players position no matter where he is and even if he is on the move.

I thought I'd use a loop and test it at the end of each loop to see if both positions match, if not loop again.

in the unit called sb1, I put

sbgroup = group this

in the init field

m1 is the name of the player unit

======================

#loop

sbgroup move (getPos m1)

? (getpos sb1) != (getpos m1): goto loop

sb1 globalchat " I have arrived"

=======================

Its not working.  An error comes up something about teh test at the end of the lopop being wrong. How do you test for matching positions

Can anyone suggest a better way of scripting it.

Also is there a command or script snippet to test for how much distance a unit is away from a target location.

Many thanks in advance

Share this post


Link to post
Share on other sites

Their positions will never be the same as that would involve them more or less merging. You can however, check the distance between two objects. For example..

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_ai doMove getPos player

@_ai distance player <= 5

_ai teamChat "I'm right with you."

<span id='postcolor'>

Which, if you don't know what the @ is for, would cause the script to halt until _ai distance to player is less than or equal to five.

Share this post


Link to post
Share on other sites

ok that worked.. So I tried putting that into a loop so the Ai would continualy test for how far he is from the player then redo the domove until he is within 5 metres.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">#loop

sb1 doMove getPos m1

@sb1 distance m1 < 5

?sb1 distance m1 < 5 :goto loop

sb1 globalchat " Hello, I'm here"

<span id='postcolor'>

But the loop doesn't seem to work.

If the player moves after the  AI starts a domove, Ai just goes to the original player position at the time of script starting.

Am I missing something ?

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Terminus @ Mar. 20 2003,10:45)</td></tr><tr><td id="QUOTE"><span id='postcolor'>

Try this:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

#loop

sb1 doMove getPos m1

~0.1

?sb1 distance m1 > 5 :goto loop

<span id='postcolor'>

And sometimes you have to use move instead of DoMove.

Share this post


Link to post
Share on other sites

Nope. the unit just goes straight to saying "hello" from his point of origin Doesn't even attempt to move.

Share this post


Link to post
Share on other sites

Sorry, I found the bug:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

#loop

sb1 doMove getPos m1

~0.1

?sb1 distance m1 > 5 :goto "loop"

<span id='postcolor'>

Loop markers have always to be in inverted commas!

Share this post


Link to post
Share on other sites
Guest jacobaby

First, make sure the names of the units etc are exactly right. That may sound simple but you would be surprised how the little things get overlooked.

First you NEED to have a delay in there, or you will get an infinite loop error.

To save CPU time you can use a second or longer. The AI will move to the last detected position, so the more frequently you update it the more accurate he will be, but also there's no point overdoing it.

You can also use DOFOLLOW I think for this, which may not need the constant updating.

As a matter of interest, you can use UNITREADY with domove, the follower will give the READY signal when he has moved to your position.

TJ

Share this post


Link to post
Share on other sites

Names are fine.

I added ~2

I replaced sb1 doMove getPos m1 with sb1 dofollow m1

None worked. The ai just goes straight to saying hello.

I'm not sure where I would insert a unitready sb1

Could you give me an example of unitready check ?

Thanks

Share this post


Link to post
Share on other sites
Guest jacobaby

try this. Also, make sure SB1 is the leader of the group, or he may get conflicting orders, and make sure he hasnt got waypoints

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

#loop

SB1 domove getpos player

~2

? unitready SB1:goto "ready"

goto "loop"

#ready

SB1 sidechat "Whew, caught you up at last!"

<span id='postcolor'>

I KNOW that works. Definetly.

TJ

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×