Why is this break loop not working?

Good day,
I have a game mode that changes a struct once the postlogin event begins.

I have created a function to search the struct for an empty slot to input the players details.

My loop is as such. ‘Null’ is the empty value, and the function searches the array to find the next index that holds a ‘Null’ and outputs that index to be used. If the search is constantly false, then the index produced will me -1 which will allow me use that for other reasons.

What am i doing wrong? If i remove the -1 component, the array establishes the correct indexes to apply my players details.

If you are wondering why i want the -1, it is so I can say that the ‘team’ is full essentially.

Seems ok up to this point.

The core problem is that by that False branch you doing return right after the checking the first element.

The easiest proper way would be:

  • on True return loop index immediately, without going to break;
  • on Completed return constant -1.

The bit more harder way(one you was trying to do), is to:

  • set LoopIndex to -1 at the start of the function before the loop (or by default value for local variable);
  • on True assign ArrayIndex to LoopIndex, break;
  • on False do nothing, as after all elements are checked, your LoopIndex is still the -1;
  • on Completed return LoopIndex.
1 Like

Cheers for the quick reply, I still dont fully understand why the original loop didnt work. But the slight tweaks you recommended worked. Cheers.

you said proper for the non-break concept.
I know my array may not be large, but would it not be best practice to stop the loop to save on running extra steps that are not needed?

I know it might be very minute in difference

The function have a strict start and finish points. It starts at “begin”(abstract term, in your case it’s the violet Find Empty Array Index node) and ends at return node (can have several return nodes). That means that as soon as your execution comes to the return node, the function execution is finished. Including no more loop interations.

The way you think about it, is probably coming from the main EventGraph, where there is not return nodes and you have to break the loop explicitely. But for functions is isn’t the case

you said proper for the non-break concept.
I know my array may not be large, but would it not be best practice to stop the loop to save on running extra steps that are not needed?
I know it might be very minute in difference

So, based on above, it’s the other way round: the immediate return will save you from one extra break call

1 Like

fantastic