JavaScript switch statement in Unity

This function won’t compile with the switch statement in it:

function isHostile( t : Rocket ){
	if( faction == t.faction ){
		return false;
	}
	
	switch ( faction ){
		case factionEnum.neutral:
		case factionEnum.goodAncient:
			return false;
			break;
		case factionEnum.legion:
		case factionEnum.solenoid:
		case factionEnum.human:
		case factionEnum.arkane:
			if( t.faction == factionEnum.pirate ) return true;
			if( t.faction == factionEnum.evilAncient ) return true;
			return false;
			break;
		case factionEnum.pirate:
		case factionEnum.evilAncient:
			return true;
			break;
	}
}

Is switch implemented at all in UnityScript?

Yep; I think all the case statements need a matching break statement though, even if they don’t do anything.

–Eric

I don’t think you need the breaks since you have a return. And I think it’s ok to pile cases as you did, that’s the point of a switch.
All this I know for sure from C#, but I guess JS is the same.

I’d try to compile it to see what’s wrong but I don’t feel like re-writing your enum :sweat_smile:

Nope, just tested with a very similar switch statement I wrote…you can’t do stuff like

      case factionEnum.human: 
      case factionEnum.arkane: 
         if( t.faction == factionEnum.pirate ) return true; 
         if( t.faction == factionEnum.evilAncient ) return true; 
         return false; 
         break;

You have to write something for factionEnum.human, even if it’s just break. This makes it not like real Javascript, where you can stack cases.

–Eric

Yep, doesn’t seem normal… you should probably report it.

In fact, you can’t do it in C#, either. You get “error CS0163: Control cannot fall through from one case label to another” if you try. Maybe you’re thinking of C/C++, in which falling through from one case to another is allowed.

EDIT: just found this useful page, which tells you how to explicitly fall through cases in C#. Don’t know if that works for Javascript, though. I seem to recall that Unity’s Javascript doesn’t have goto at all.

EDIT 2: and after all that, I can’t get the fallthrough behaviour described above to work for code compiled with the command line Mono tools. I haven’t tried it in Unity itself, but I expect it will behave in the same way. Maybe this is something that Mono doesn’t implement in the usual way.

Really? Without stacking cases, switch statements are no more useful than chained if/elses. :confused:

To NCarter: scroll to the bottom of the page you just sent:

If it doesn’t work in Unity’s C#, it’s probably Mono’s fault. Same with JS, should work, but OTEE’s implementation is still incomplete.

Also, to podperson, last paragraph of that same page:

As a result of the C# rules requiring explicit flow-control to occur at the end of a case block (most usually a break), many people question why the behavior simply wasnt changed such that fall-through didnt occur.  That is, dont make break required, simply change the semantics of switch to not have fall-through for cases.  The reason this wasnt done was so that developers who were very used to C++ wouldnt have a hard time understanding what a switch statement was doing.

So writing

return false;
break;

is an overkill: return already is a flow-control keyword.

Oops, I just realised that I accidentally omitted the break statement in the final case (!), and that was why it wasn’t working for me. Therefore, that page I linked is indeed correct for C# with Mono/Unity, and you were right in the first place.

I thought it was odd that the Mono guys had missed something fundamental like that. :slight_smile:

You bet! Now that I think about it, the whole system would probably crash, as a zillion functions must be relying on switch!..

Wow this is an old post… nether the less… i think i found a work around…

in javascript:

var value : int;

function Update(){
	
	switch(value){
		case 0:; // fall through
		case 1:; // fall through
		case 2:
			print("I can handle case: " + value);
			break;
		default:
			print("I can not handle case: " + value);
			break;
	}
}

Simply putting a line enders after the case, stops it from complaining and makes it work