- Home /
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.
Answer by Flynn · Jan 18, 2013 at 08:46 PM
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)
Thank you. And yes, it is the variables that are making me frustrated. There's only so many ways I can concisely write "winningPlayer". I'll look into the encapsulation you mentioned.
I might try C# again, but I was never really a fan of the way variables were declared and that you had to put an "f" at the end of every float... It took me long enough to remember the semi-colons, haha! Although, I do like that C# has #region... Why can't I get the best of both worlds! :P
I used to hate C# for those reasons too, but I have come to appreciate them -- The nice thing about having to always specify your variable type is that if you accidentally get confused and use the wrong variable, or forget what type of variable you are using, C# will be right there to slap you awake -- which can really speed up the debugging process. It also boosts code clarity which is cool. I agree, though, about the "f" at the end of floats-- constants should (in my opinion) automatically detect if they need to be float or double based on context...
What I meant when I said I don't like C# variable declaration was that I really like the colons in Javascript to separate the name and the type. Heh, I actually always specify the variable type in Javascript for the exact reasons you listed. I'm pretty OCD about my code. Hmm, perhaps I really should switch to C# if I'm like this. You know what, my next project shall be entirely C#!
Your answer
Follow this Question
Related Questions
Breaking out of a function from an if statement inside..... Noob 1 Answer
Yielding in between functions... 2 Answers
Have a function wait unitl a boolean is false 1 Answer
while loop not looping 2 Answers
Pause the coroutine inside a loop 1 Answer