-2

This is for unity

I tried another way of stopping player after he dies and it worked fine but now it does not work with sounds

That was my way the only thing that I changed is the state

Ben's was

if  (state  ==  state.Alive) { Update() }

Mine was

if (state == state.Dying) { return; }

They look the same but the problem is the Success Sound will not play

Ben's code is working fine with me but I want to understand how it works

is mine the same?

void Update()
{
   if (state == State.Dying) 
   { 
       return; 
   }
   Rotate();
   Thrust();
}

Ben's way

void Update()
{
   if (state == State.Alive)
   { 
       Rotate();
       Thrust();
   }
}

For People Asking For The Enum Definitions:

enum State { Alive, Dying, Transcending }
State state = State.Alive;
  • Can you post the definition of State? Does it have only two possible values or are there others? – John Wu Jan 9 at 21:46
  • Do you have a link to the API docs for the list of State enum values? If there are more than 2 than the two queries are not equivalent. If you changed your statement to if (state != State.Alive) { return; } then they would be exactly equivalent. – Berin Loritsch Jan 9 at 21:47
  • 1
    What does single stepping through the code using the debugger tell you? – Doc Brown Jan 9 at 21:49
  • Another thought, if there is more inside the Update() method after the query for State then returning from the method early will cause all of that to be skipped. – Berin Loritsch Jan 9 at 21:50
  • 1
    If you're going to insist on the braces, they need to be on their own lines. – Robert Harvey Jan 9 at 21:55
1

No, they are not the same.

IF Alive and Dying were mutually exclusive (i.e. it's always one or the other), then it would mostly be the same (see below for pedantic caveat). But as there is a third enum value, the behavior is different.

  • In the first example, Transcending behaves the same way as Alive (as they are both != Dying)
  • In the second example, Transcending behaves the same way as Dying (as they are both != Alive)

Which one is correct is up to you to decide.


One minor caveat, it's possible to use undefined values in an enum, e.g.

public enum MyEnum { A = 0, B = 1, C = 2 }

MyEnum test = (MyEnum)42;

Console.WriteLine(test == MyEnum.A); // false
Console.WriteLine(test == MyEnum.B); // false
Console.WriteLine(test == MyEnum.C); // false

Counterintuitively, test is a MyEnum yet it does not equal any defined MyEnum value. This means the earlier bullet points need to be slightly extended:

  • In the first example, an unexpected enum value will cause rotation and thrusting.
  • In the second example, an unexpected enum value will cause nothing.

You don't particularly need to defend against this at every turn, but in cases where you do value conversion, it's good to remember that using an undefined value does not register as a runtime error (by itself) but does lead to all expected equality checks failing.

|improve this answer|||||
  • Thank you for the help, this helped me understand enums a lot better and I will revert back to bens method to do this. – FullMetalAlchemistModz_YT Jan 9 at 22:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.