Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Help RandInt and Switch Statements.
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#9
RE: RandInt and Switch Statements.

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: (Select All)
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.
(This post was last modified: 04-13-2013, 07:37 PM by TheGreatCthulhu.)
04-13-2013, 07:29 PM
Find


Messages In This Thread
RE: RandInt and Switch Statements. - by NaxEla - 04-13-2013, 05:08 PM
RE: RandInt and Switch Statements. - by Kreekakon - 04-13-2013, 05:23 PM
RE: RandInt and Switch Statements. - by NaxEla - 04-13-2013, 05:48 PM
RE: RandInt and Switch Statements. - by Kreekakon - 04-13-2013, 05:57 PM
RE: RandInt and Switch Statements. - by TheGreatCthulhu - 04-13-2013, 07:29 PM



Users browsing this thread: 1 Guest(s)