How to break out of Entities.ForEach?

Is the answer: you can’t?

My foreach is looking for a nearby target among thousands. Once I find a decent target (say by finding the nearest of ten within range) then I want to break out of the foreach.

Plz advise. Thanks.

1 Like

You can use a captured boolean value.

bool foundTarget = false;

this.Entities.ForEach(... {
    if(foundTarget) {
        return;
    }

    if(viable target is found) {
        foundTarget = true;
    }
});

It doesn’t really break out of the loop but does the job.

Cheers. I’m doing that already and it helps but was hoping for a real break rather than a continue.