problem with switch()

ki77bot

Member
Hi folks,

I guess it is obvious what I am trying to do here, but it doesn't work. It seems to always return 8. Is have_skill() not a valid boolean expression? :confused:

Code:
switch {
	case ( have_skill($skill["Advanced Saucecrafting"]) ) :	
		adv_sauce = 3;
	case ( have_skill($skill["The Way of Sauce"]) ) :	
		adv_sauce = 5;
	case ( have_skill($skill["Advanced Saucecrafting"]) && item_amount($item[Gravyskin Belt of the Sauceblob]) > 0 ) :	
		adv_sauce = 6;
	case ( have_skill($skill["The Way of Sauce"]) && item_amount($item[Gravyskin Belt of the Sauceblob]) > 0 ) :	
		adv_sauce = 8;
		break;
	default:
		adv_sauce = 0;
}

Cheers,
ki77bot
 

slyz

Developer
If you don't add a break at the end of a case, it will continue to evaluate and execute the other cases. In your code, if you have all of those skills, adv_sauce will change value several times. The last one will be 8.

You could do something like this:
PHP:
int adv_sauce = 0;
switch {
	case ( have_skill($skill["Advanced Saucecrafting"]) ) :	
		adv_sauce += 3;
	case ( have_skill($skill["The Way of Sauce"]) ) :	
		adv_sauce += 2;
	case ( item_amount($item[Gravyskin Belt of the Sauceblob]) > 0 ) :	
		adv_sauce += 3;
}

EDIT: I think this snippet does exactly what you where aiming for, but maybe it should skip everything if you don't have Advanced Saucecrafting.

EDIT2: this doesn't work, see below for xKiv's explanation of how switch() works.
 
Last edited:

ki77bot

Member
The thing is: my code will always return 8, regardless which of those skills the character has and I do want to understand why. I do appreciate that you are offering another solution that should work, but that would also stop the learning process... ;)
 

slyz

Developer
I thought the first paragraph was a clear enough explanation :D

For someone who has all the skills, this is what happens:
PHP:
case ( have_skill($skill["Advanced Saucecrafting"]) ) : adv_sauce = 3;
The condition is true, so adv_sauce is set to 3. The switch then proceeds to the next case:
PHP:
case ( have_skill($skill["The Way of Sauce"]) ) : adv_sauce = 5;
The condition is true, so adv_sauce is now set to 5. The switch then proceeds to the next case etc... until it reaches the last one. Since the condition is true, adv_sauce is finally set to 8.

To make your code do what you want, add a break at the end of each case so it doesn't carry on evaluating the other cases, like this:
PHP:
switch {
	case ( have_skill($skill["Advanced Saucecrafting"]) ) :	
		adv_sauce = 3; break;
	case ( have_skill($skill["The Way of Sauce"]) ) :	
		adv_sauce = 5; break;
	case ( have_skill($skill["Advanced Saucecrafting"]) && item_amount($item[Gravyskin Belt of the Sauceblob]) > 0 ) :	
		adv_sauce = 6;  break;
	case ( have_skill($skill["The Way of Sauce"]) && item_amount($item[Gravyskin Belt of the Sauceblob]) > 0 ) :	
		adv_sauce = 8; break;
	default:
		adv_sauce = 0;
}
 

ki77bot

Member
Obviously I am misunderstanding something here. I thought your last snippet would break if the conditions were true. So, if I added break it would only mean that adv_sauce would always be 3 (in the case that said character had Advanced Saucecrafting). I am using this for different characters though, which have different skillsets.

If I used your snippet, it would work if I "turned the cases upside down":

PHP:
switch {
    case ( have_skill($skill["The Way of Sauce"]) && item_amount($item[Gravyskin Belt of the Sauceblob]) > 0 ) :    
        adv_sauce = 8; break;
    case ( have_skill($skill["Advanced Saucecrafting"]) && item_amount($item[Gravyskin Belt of the Sauceblob]) > 0 ) :    
        adv_sauce = 6;  break;
    case ( have_skill($skill["The Way of Sauce"]) ) :    
        adv_sauce = 5; break;
    case ( have_skill($skill["Advanced Saucecrafting"]) ) :    
        adv_sauce = 3; break;
    default:
        adv_sauce = 0;
}
 
Last edited:

slyz

Developer
Oh, I hadn't thought of that. Simply change the order then, or do something incremental like the code I posted earlier.

This should work:
PHP:
switch { 
    case ( have_skill($skill["The Way of Sauce"]) && item_amount($item[Gravyskin Belt of the Sauceblob]) > 0 ) :     
        adv_sauce = 8; break; 
    case ( have_skill($skill["Advanced Saucecrafting"]) && item_amount($item[Gravyskin Belt of the Sauceblob]) > 0 ) :     
        adv_sauce = 6;  break; 
    case ( have_skill($skill["The Way of Sauce"]) ) :     
        adv_sauce = 5; break; 
    case ( have_skill($skill["Advanced Saucecrafting"]) ) :     
        adv_sauce = 3; break; 
    default: 
        adv_sauce = 0; 
}

EDIT: I completely misread your OP. Or I need my coffee. If what you posted returns 8 for a character that doesn't have The Way of Sauce, there's a problem I'm not seeing.

EDIT2: I very badly need my coffee.
 
Last edited:

xKiv

Active member
If you don't add a break at the end of a case, it will continue to evaluate and execute the other cases.

Emphasis mine.
As far as I can tell, it won't *evaluate* the cases - it will execute all code between the first case that matches and the first break after that (assuming implicit break at the end of the whole switch block). That's how it works in C and java ...
 

ki77bot

Member
That would explain why it always returned 8...

Now i got a solution for the problem AND also understood why it occured in the first place.

Thanks a lot guys... :D
 
Top