Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: valter on Thu 05/11/2015 16:20:30

Title: Switch and if together
Post by: 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.
Do you think that might cause unpredictable failures?
Thanks

Valter
Title: Re: Switch and if together
Post by: Crimson Wizard on Thu 05/11/2015 18:30:47
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.
Title: Re: Switch and if together
Post by: valter on Thu 05/11/2015 19:30:18
Code (ags) Select

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.
Title: Re: Switch and if together
Post by: Snarky on Thu 05/11/2015 20:08:59
Duff's device here I go!
Title: Re: Switch and if together
Post by: valter on Thu 05/11/2015 20:46:16
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 :)
Title: Re: Switch and if together
Post by: Snarky on Thu 05/11/2015 21:01:50
No, the code is fine. Duff's device (https://en.wikipedia.org/wiki/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.
Title: Re: Switch and if together
Post by: valter on Thu 05/11/2015 21:23:25
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
Title: Re: Switch and if together
Post by: Khris on Thu 05/11/2015 22:18:46
Btw, lines 7-11 can be shortened considerably:
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.)
Title: Re: Switch and if together
Post by: valter on Fri 06/11/2015 07:33:29
Thanks Khris, I like to reduce the code as much as possible.
Title: Re: Switch and if together
Post by: Gurok on Fri 06/11/2015 10:40:50
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.