Case Statements

Started by RedBlob, Thu 20/07/2006 20:48:11

Previous topic - Next topic

RedBlob

Is it possible to use a case statement in place of the else/if  statements?


I'm receiving an error that I have too many nested else/if statements, but I need 78 for a randomized event.

DoorKnobHandle

There is no such things as the switch, case, break and default keywords in AGS as they are in C++. However, if you show us your code, there might be quite a few ways to work around this. I never tried having over 50 if-else-statements in any script myself though...

Rui 'Trovatore' Pires

Does it help if you consider using || a couple of times, instead of eternally nesting if/else?
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

Nathan23

you should check your conditions, perhaps you can tie some of them with logical operators.. && or ||

But.. if you want a switch or case statements I assume you are trying with diferent values.. so you don't need nested if/else
Try this:

Code: ags
 

if (myvar == 1)Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  
{ //do stuf }
ifÃ,  (myvar == 2)
{ //do another stuff}
Ã,  Ã,  Ã,  Ã,  Ã,  .
Ã,  Ã,  Ã,  Ã,  Ã,  .
Ã,  Ã,  Ã,  Ã,  Ã,  .
if (myvar == 80)
{ // do more stuff}
else
{ // alternative stuffÃ,  }




In this form you don't need to have nested the ifs.

RedBlob


Khris

It's very possible that there's a much better way to solve your problem.
I doubt that there's a situation where it's inevitable to use 78 if-statements.

Could you elaborate a bit?

Ashen

I think there's a small flaw with Nathan's code:
Code: ags

if (myvar == 80)
{ // do more stuff}
else
{ // alternative stuff  }


Due to that else, the 'alternate stuff' in that condition will run whenever myvar != 80, i.e. after ALL the other condtions.

Other than that it should work, or you could give use more details.
I know what you're thinking ... Don't think that.

RedBlob

I'm working on a tarot program, so for each of the ten positions, I have a random card pulled and its associated message displayed. There are 78 cards to work with, which is why I was using so many nested else/ifs.

I'll try Nathan & Ashen's suggestions and post back if I'm still stuck. The help is most appreciated!

Rui 'Trovatore' Pires

Well, now that you said what you actually wanted, a dirty-ish but workable way would be:

if (myvar<5) {
  if (myvar==1) {
  }
  else if (myvar==2)
  }
etc...
}
else if (myvar>5 && myvar<10) {
  if (myvar==6) {
  }
  else if (myvar==7)
  }
etc...
}

And so on. Anyway, Nathan/Ashen's way is better, I just wanted to say what I would have said if I had known what you meant before. :=
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

Kweepa

I would assign the strings to an array and then just pick them.

Code: ags

// top of global script
String kResponses[78];

// in game start
kResponses[0] = "You will die, horribly, horribly.";
kResponses[1] = "You will be run over by a truck.";
kResponses[2] = "A meteor narrowly misses your head, and strikes your heart.";
...

// when you need the message
Display(kResponses[myvar]);
Still waiting for Purity of the Surf II

SMF spam blocked by CleanTalk