x


while(true) ???

im confused. I kow how a while loop works and what it does but im stumped on this line while(true)... while what is true??? i dont get it. here is the code that is in question copied from the fps tutorial ai sript.

function Start () {
    // Auto setup player as target through tags
    if (target == null && GameObject.FindWithTag("Player"))
       target = GameObject.FindWithTag("Player").transform;

       animation["idle"].wrapMode = WrapMode.Loop;

    Patrol();
}

function Patrol () {
    var curWayPoint = AutoWayPoint.FindClosest(transform.position);
    while (true) {
       var waypointPosition = curWayPoint.transform.position;
       // Are we close to a waypoint? -> pick the next one!
       if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
         curWayPoint = PickNextWaypoint (curWayPoint);

       // Attack the player and wait until
       // - player is killed
       // - player is out of sight     
       if (CanSeeTarget ())
         yield StartCoroutine("AttackPlayer");

       // Move towards our target
       MoveTowards(waypointPosition);


       yield;
    }
}

so what is going on here can someone please explain.

more ▼

asked Mar 24 '12 at 05:11 PM

nemisis83 gravatar image

nemisis83
2 1 1 1

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

It means 'do this loop forever' which, unless there were yields there (or early returns), would hang a program. In this case, it means this script will execute those commands until the game stops or the object it's on is destroyed or disabled.

more ▼

answered Mar 24 '12 at 05:18 PM

DaveA gravatar image

DaveA
26.4k 151 171 256

so once you yield the method would need to be called again through and update() or ect.

Mar 24 '12 at 09:27 PM nemisis83

You should use 'add new comment' rather than Answer. I assume you're asking me. No, the framework takes care of it. Yield basically says 'go off and do other things, like process events and play sounds or whatever, but come back to this point in this method and continue from there'

Mar 24 '12 at 09:29 PM DaveA
(comments are locked)
10|3000 characters needed characters left

A while loop continues to loop as long as the condition is true. Since true is always true, while (true) is an infinite loop.

more ▼

answered Mar 24 '12 at 05:17 PM

Eric5h5 gravatar image

Eric5h5
80k 41 131 518

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x294
x60
x27

asked: Mar 24 '12 at 05:11 PM

Seen: 983 times

Last Updated: Mar 24 '12 at 09:33 PM