-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;
Theraot
  • 9,261

1 Answers1

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.

Flater
  • 58,824