Frictional Games Forum (read-only)

Full Version: RandInt and Switch Statements.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone! I got a script problem (well, two of them.). I saw RandInt's and thought "Why not make a switch statement about it?". This was a great idea until i realized switch statements and RandInt's are hard. How do i use them?

Help is highly appreciated.


Sincerely,
JustAnotherPlayer
Code:
switch(RandInt(min, max))
{    
    case min:    
    {    
    }    
    case max:    
    {    
    }
    case ((min+max)/2):  
    {    
    }
}

It's quite easy really.
Change min and max with your own values, of course. Switch the case values with the values you want to check (1, 3, 4.56, hope you got me)
(04-13-2013, 02:02 PM)ClayPigeon Wrote: [ -> ]
Code:
switch(RandInt(min, max))
{    
    case min:    
    {    
    }    
    case max:    
    {    
    }
    case ((min+max)/2):  
    {    
    }
}

It's quite easy really.
Change min and max with your own values, of course. Switch the case values with the values you want to check (1, 3, 4.56, hope you got me)

Like this?
Code:
switch(RandInt(1, 3))
{    
    case 1:    
    {    
    }    
    case 3:    
    {    
    }
    case ((1+3)/2):  
    {    
    }
}
OMG JustAnotherPlayer does not know something its a wonder Wink

Hope its working for you now i'm not so far with scripting yet xD
(04-13-2013, 02:02 PM)ClayPigeon Wrote: [ -> ]
Code:
switch(RandInt(min, max))
{    
    case min:    
    {    
    }    
    case max:    
    {    
    }
    case ((min+max)/2):  
    {    
    }
}

It's quite easy really.
Change min and max with your own values, of course. Switch the case values with the values you want to check (1, 3, 4.56, hope you got me)

You aren't actually supposed to have those curly brackets after each case. It will cause an error.
(04-13-2013, 05:08 PM)NaxEla Wrote: [ -> ]You aren't actually supposed to have those curly brackets after each case. It will cause an error.
Yes you can, however you have to remember to add break; at the end of each one like this:

case min:
{
//do stuff
break;
}
case max:
{
//do stuff
break;
}
Huh, I never knew that. When I use a switch statement, I do something like this
Code:
int x = RandInt(1, 3);
switch(x) {
    case 1:
        // do something
        break;
    case 2:
        // do something else
        break;
    case 3:
        // do something else
        break;
}

Well, everyday's a school day! Smile
Yeah I think your way should work fine too. Personally I like mine better because I'm used to organizing "script chunks" inside brackets.

Well it comes down to habits, and whatever you prefer I suppose! Smile
You can read about the switch statement in more detail here.

The reason that there has to be a break; at the end of each case is because (1) it is possible to chain several cases together by omiting the "break", and (2) traditionally, in most other programming languages which use a similar syntax (code looks similar to this), there's no { and } surrounding each case.

So, it is possible to do this:
int x = RandInt(1, 3)
PHP Code:
switch(x)
{
case 
1:
case 
2:
    
// Code that does something for x=1 or x=2 goes here
    
break;
case 
3:
    
// Code that does something when x=3 goes here
    
break;


While this is a perfectly legitimate thing to do, it happens more often by accident, when it causes your code to misbehave, and if that happens it's somewhat hard to track down the bug and figure out why the script isn't working the way it should.

So, as a rule of thumb, always double check if all the breaks are there; in fact, after each case, put the break first, before you type in anything else.
Also, when two or more cases need to do the same thing, people usually extract the common code into a separate function, and then simply make a call to that function from each case, rather than letting them fall through (chain them).

About enclosing the cases in code blocks (between { and }): this normally isn't done, and people who have used AngelScript (the language used for Amnesia scripting), or are familiar with other languages like C, C++, C# or Java might find your code easier to read if you use the "standard" way (if they want to learn Amnesia scripting by checking out your CS or FC); however, if you prefer enclosing the cases in {}, then go for it, is not wrong or anything. Just be consistent about it.