If vs Switch
I have following code, how do I convert it into switch and would it make any difference in performance.
void Update ()
{
AnimatorStateInfo currentState = anim.GetCurrentAnimatorStateInfo (0);
if (currentState.IsName ("Run")) {
soldier.velocity = new Vector3 (0,0, -moveSpeed);
//mf.SetActive (false);
}
if (currentState.IsName ("StrafeLeft"))
{
soldier.velocity = new Vector3 (-moveSpeed,0,0);
//mf.SetActive (true);
}
if (currentState.IsName ("StrafeRight"))
{
soldier.velocity = new Vector3 (moveSpeed,0,0);
//mf.SetActive (true);
}
if (currentState.IsName ("RunFire")) {
soldier.velocity = new Vector3 (0,0, -moveSpeed);
//mf.SetActive (true);
}
if (currentState.IsName ("RunBackFire")) {
soldier.velocity = new Vector3 (0, 0, moveSpeed);
//mf.SetActive (true);
}
if (currentState.IsName ("Fire")) {
soldier.velocity = new Vector3 (0, 0 ,0);
// mf.SetActive (true);
}
if (currentState.IsName ("Die")) {
soldier.velocity = new Vector3 (0, 0 ,0);
//mf.SetActive (true);
}
}
Answer by Tourist · Feb 15, 2017 at 02:21 PM
switch(myString)
{
case "Test": break;
case "Other string": break;
}
As for the performances, I'll let you do your tests. However, I would highly recommend that you keep the hashed name of each state you want to test using Animator.StringToHash and used the hashed integers as based of the switch. It would clearly be the most efficient way.
i'm very weak in coding , could you please give me a full elaborated code please.
In your class :
private static readonly int runId = Animator.StringToHash("Run");
.... // do the same for all states you want to check
In your function :
AnimatorStateInfo currentState = anim.GetCurrentAnimatorStateInfo (0);
int fullPathHash = currentState.fullPathHash;
if(fullPathHash == runId)
{
// Do run stuff
}
else if(....)
...
Answer by Kossuranta · Feb 15, 2017 at 02:06 PM
As AnimatorStateInfo doesn't have value for name, just IsName(string) check I can't think of simple way to make it with switch-case. It might be somewhat more efficient in same cases, but difference is really small if you fix your current code to be proper if-elseif-else.
Currently what your code does it will check every if-clause one by one even if the first one is true it will still check all that come after it. As only one can be true at once first one should be if() and all that come after that should be if else().
For example:
if (currentState.IsName ("Run"))
{
soldier.velocity = new Vector3 (0,0, -moveSpeed);
}
else if (currentState.IsName ("StrafeLeft"))
{
soldier.velocity = new Vector3 (-moveSpeed,0,0);
}
else if (currentState.IsName ("StrafeRight"))
{
soldier.velocity = new Vector3 (moveSpeed,0,0);
}
This code would first check if the name is "Run" and if it is true it will skip all the rest. Only if it is false it will continue to check the next one. First one will be fastest as there is only one check so you could also do small optimization by ordering the list. Technically settings the most heaviest option as first would give most stable framerates, but in practice you probably wont even notice the difference.
Your answer
Follow this Question
Related Questions
Button bool doesn´t work correctly weird 1 Answer
Refactoring Nested Switch Statements 0 Answers
How to use WaitForSeconds? 2 Answers
Unparalleled problem with if-statement 0 Answers
My "if" statement doesn't work? 2 Answers