Quantcast
Channel: Answers by "Flynn"
Viewing all articles
Browse latest Browse all 59

Answer by Flynn

$
0
0
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)

Viewing all articles
Browse latest Browse all 59

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>