Switch and if together

Started by valter, Thu 05/11/2015 16:20:30

Previous topic - Next topic

valter

Hi, I'm working on version 3.4.0.6.
Are in a "switch" statement allowed to use a construct "if else"?
I tried it and I found that it works fine.
Do you think that might cause unpredictable failures?
Thanks

Valter

Crimson Wizard

#1
Quote from: valter on Thu 05/11/2015 16:20:30
Hi, I'm working on version 3.4.0.6.
Are in a "switch" statement allowed to use a construct "if else"?
I tried it and I found that it works fine.
If you mean putting "if" under "case" statement, then yes. But script example would be nice, because I am not absolutely sure that you mean what I could think you mean.

In programming languages (and AGS Script as well) you can put one construct into another.
You can put loops inside the loops, if/elses into another if/else, and you should be able to put loops, if/else and more switches into switches.

Now, they key word here is "should"; if it fails, then it is a bug, and should be fixed.

valter

Code: ags

GUI *gSideSx;
GUI *gSideDx;
bool is_sx;
short bubbleHeight;
short lblBubbleHeight;
short dxOffset = 50;
if(px < (System.ViewportWidth / 2)) {
	is_sx = true; 
} else { 
	is_sx = false; 
}
switch(increment)
{
	case 1:
		bubbleHeight = 70;
		lblBubbleHeight = 20;
		if(is_sx) { 
			talk_bubble_id = 1;
			gSideSx = gBubbleOS_sx_sx_row1;
			gSideDx = gBubbleOS_sx_dx_row1;
			px = px + (xScale/2); 
		} else {
			talk_bubble_id = 2;
			gSideSx = gBubbleOS_dx_sx_row1;
			gSideDx = gBubbleOS_dx_dx_row1;
			px = px - (xScale/2) - gSideSx.Width - dxOffset;
		}
		break;
	case 2:
        [...]
}


Thanks Crimson, you understood my question well. I know that one or more constructs can be inserted into other constructs but sometimes with other game scripting languages it was not possible to do everything possible for example in Visual Basic, Delphi ...
The code works fine, I just wanted to make sure I do not have problems later.

Snarky


valter

You mean to say that the code is apparently wrong even if it works or what else? I use break ... I do not understand what you want to suggest me :)

Snarky

#5
No, the code is fine. Duff's device is a famously weird programming construct (invented at LucasFilm!) that you can build with a switch statement sort-of half-inside, half-outside of a loop. It probably wouldn't work in AGS script, and there'd be no reason to use it, anyway.

In other words, it was a joke.

valter

I had heard about it, the example I had seen was based on the absence of the break at the end of the case.
Who can say that does not work on AGS? ;-D

Khris

Btw, lines 7-11 can be shortened considerably:
Code: ags
is_sx = px < (System.ViewportWidth / 2);

(Just like in your construct, the boolean expression on the right evaluates to true or false, which you can simply assign directly to the bool.)

valter

Thanks Khris, I like to reduce the code as much as possible.

Gurok

Quote from: Snarky on Thu 05/11/2015 21:01:50
It probably wouldn't work in AGS script, and there'd be no reason to use it, anyway.

Just for completeness, your assumption here is correct. It's not currently possible in AGS script. The compiler only permits case statements in the level directly under a switch() statement.

It could theoretically work. There would be nothing wrong with arranging the statements this way when compiled.
[img]http://7d4iqnx.gif;rWRLUuw.gi

SMF spam blocked by CleanTalk