Select Case?

Started by Calin Leafshade, Wed 26/08/2009 17:53:15

Previous topic - Next topic

Calin Leafshade

Is there a scripting command in AGS similar to the Select case command in VB.. i forget what its called in C

Essentially you pass a variable to the command and it check various outcomes i.e

Select Case NumberOfApples

Case 1
You have one apple

Case 2
You have 2 apples

Else
Any other possibility

End select

I know this can be done with nested if statements but if you have a lot of them it can get confusing.

Its check using inventory items on things. So it checks for all the cases where it does have an action and if none of those are applicable it gives a stock "does not work" message.

Gilbert

Quote from: Calin Leafshade on Wed 26/08/2009 17:53:15
Is there a scripting command in AGS similar to the Select case command in VB..
No. Just use nested if's.

NsMn

Sorry that this is not quite on-topic, but in your case, you only need 2 or 3 if-statements.

Code: ags

if(player.InventoryQuantity[iApple.ID]==0)Display("You don't have any apples.");
else if(player.InventoryQuantity[iApple.ID]==1)Display("You have one apple.");
else Display("You have %d apples.",player.InventoryQuantity[iApple.ID]);

Gilbert

He mentioned already that he knew about nested if statements.

NsMn

Yeah, I know. It was "...if you have a lot of them it can get confusing." that I was answering off. But, depends on wether this was only an example or not.

Calin Leafshade

The apples were kind irrelevent.

The point was that i needed a way of testing for ALL other possibilities.

However the else if statement works fine. I forgot about that

else if is not technically a NESTED if statement.

A nested if statement looks like

Code: ags

If (condition1) {
     If(condition2) {
            If(condition3{

}
}
}

Stee

Does AGS not support switch/case statements?

I thought it was basically c++.

Time for a suggestion methinks, if it's not already in the todo list

EDIT: nvm just noticed Gilbets link
<Babar> do me, do me, do me! :D
<ProgZMax> I got an idea - I reached in my pocket and pulled out my Galen. <timofonic2> Maybe I'm a bit gay, enough for do multitask and being romantical

monkey0506

Pardon me if I'm restating things that have already been said here, but I'd like to make a note here regarding how "confusing" if-statements can apparently become. If you're accustomed to using "Select Case" or "switch" then perhaps the easiest thing for you to do to simplify is store your "case" in a variable. Consider the following example:

Code: ags
int case = player.InventoryQuantity[iApples.ID];
// compare to: Select Case NumberOfApples
// compare to: switch (NumberOfApples) {
if (case == 1) {
  // compare to Case 1
  // compare to case 1:
  player.Say("You have one apple!");
}
else if (case == 2) {
  // compare to Case 2
  // compare to case 2:
  player.Say("You have two apples!");
}
else {
  // compare to Else
  // compare to default:
  player.Say("You have %d apples!", player.InventoryQuantity[iApples.ID]);
}


Sure the syntax is different, I can accept that. But I fail to see why it would be a hugely impacting ordeal. I fully understand once you get into a certain way of doing things, that's the way you are used to doing them. But AGS isn't VB, AGS isn't C++, etc. and so forth. I'm not necessarily directing this at anyone, I'm just trying to make the point that just as there are obvious blatant differences between C-languages and BASIC there will be differences here.

It's just about learning "how to do it in this language". Sort of similar to learning a foreign language, "How do you say...?" :P

I know you said you already understand about nested if statements, and then you sorted it out with the else; I just wanted to point out that with a bit of manipulation you can actually make the syntax, though different, still very similar to what you're used to. ;)

Calin Leafshade

I had just forgotten about 'else if'

im sure you can appriciate that without 'else if' the situation becomes much more confusing with multiple exclusive conditions since they are nested. 'else if' isnt a nested if statement.


SMF spam blocked by CleanTalk