Re-using the "same" for loop?

In function Update(), I’m looping through my GameManager’s player array to check for conditions. The problem is I need to do this in multiple places, which requires me to use differently named variables for each loop(haha, “for each loop”).

For example:

function Update()
{
	if(winnerWasHooked == false)
	{
		for(var w : GameObject in winningPlayers)
		{
			if(w.GetComponent(WeapGrapple).hookedObject == null)
			{
				raceCamScript.DisplayMessage("HOOKED!", Color.white, true);
				break;
			}
		}
	}
	else if(winnerWasDetermined == true)
	{
		for(var v : GameObject in winningPlayers)
		{
			if((v.GetComponent(Player).hasAchievedDeflection == true) && (deflectionMessage == false))
			{
				deflectionMessage = true;
				matchRestartTimer = Time.time + 3.0;
				raceCamScript.DisplayMessage("FULL DEFLECTION!", winningColor, true);
			}
		}
	}
}

Is there a way I could make a function that returns each GameObject in my player array without the function breaking on the first iteration? I tried using “yield” instead of “return”, but the compiler got angry at me.

I know there HAS to be a clean way to do this. I may even be approaching this entirely wrong. Thanks.

Honestly, there really aren’t many ways to clean up your code’s redundancy (without seriously overcomplicating your code and getting into sticky things like delegates… blech).

However, one last thing you could try is a bit of code juggling:

function Update()
{
	for(var w : GameObject in winningPlayers)
	{
		if(winnerWasHooked == false)
		{
			if(w.GetComponent(WeapGrapple).hookedObject == null)
			{
				raceCamScript.DisplayMessage("HOOKED!", Color.white, true);
				break;
			}
		}
		else if(winnerWasDetermined == true)
		{
			if((w.GetComponent(Player).hasAchievedDeflection == true) && (deflectionMessage == false))
			{
				deflectionMessage = true;
				matchRestartTimer = Time.time + 3.0;
				raceCamScript.DisplayMessage("FULL DEFLECTION!", winningColor, true);
			}
		}
		else
		{
			break;//So that we don't end up checking the same thing over and over again
		}
	}
}

I will warn you, though, oversimplifying your code can be bad – You must find a good balance between simplicity of code, and code readability, both are equally important, and they can conflict.

Using the code above will make the programmer go “wtf” for several seconds before they figure out what the devil you are intending to do, whereas when reading your original code, the programmer will be able to instantly tell what you intended, and not blame you for keeping the two loops separate.

And, of course, this is all not to mention the fact that by simplifying your code in this way, you are far more likely to restrict yourself and what you can do, and get yourself into alot of trouble when doing modifications… It’s not worth the headache tbh.

In my opinion, your code is actually just fine the way it is, no need to worry about sharing loops!

If it is the variables specifically that are making you angry, you could try either using C# instead of JS (C# has better scoping of variables which lets you re-use temporary loop variables), or, you could encapsulate your loops in separate sub-functions (which is actually considered a good programming practice as long as you break them into logical “tasks” – I recommend doing that instead of the aforementioned code jumbling)