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.
PutraenusAlivius Offline
Posting Freak

Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation: 119
#1
RandInt and Switch Statements.

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

"Veni, vidi, vici."
"I came, I saw, I conquered."
(This post was last modified: 04-13-2013, 10:59 AM by PutraenusAlivius.)
04-13-2013, 10:26 AM
Find
ClayPigeon Offline
Member

Posts: 214
Threads: 13
Joined: Mar 2012
Reputation: 8
#2
RE: RandInt and Switch Statements.

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)
(This post was last modified: 04-13-2013, 02:23 PM by ClayPigeon.)
04-13-2013, 02:02 PM
Find
PutraenusAlivius Offline
Posting Freak

Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation: 119
#3
RE: RandInt and Switch Statements.

(04-13-2013, 02:02 PM)ClayPigeon Wrote:
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?
switch(RandInt(1, 3))
{    
    case 1:    
    {    
    }    
    case 3:    
    {    
    }
    case ((1+3)/2):  
    {    
    }
}

"Veni, vidi, vici."
"I came, I saw, I conquered."
04-13-2013, 03:09 PM
Find
VeNoMzTeamHysterical Offline
Member

Posts: 240
Threads: 36
Joined: Dec 2012
Reputation: 3
#4
RE: RandInt and Switch Statements.

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

http://www.frictionalgames.com/forum/thread-21719.html
Evil Awaking Work In Progress!
Hours Spend 472.
04-13-2013, 04:16 PM
Website Find
NaxEla Offline
Senior Member

Posts: 415
Threads: 5
Joined: Dec 2012
Reputation: 28
#5
RE: RandInt and Switch Statements.

(04-13-2013, 02:02 PM)ClayPigeon Wrote:
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.

In Ruins [WIP]
04-13-2013, 05:08 PM
Find
Kreekakon Offline
Pick a god and pray!

Posts: 3,063
Threads: 70
Joined: Mar 2012
Reputation: 124
#6
RE: RandInt and Switch Statements.

(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;
}

[Image: Tv0YgQb.gif]
Image by BandyGrass
04-13-2013, 05:23 PM
Find
NaxEla Offline
Senior Member

Posts: 415
Threads: 5
Joined: Dec 2012
Reputation: 28
#7
RE: RandInt and Switch Statements.

Huh, I never knew that. When I use a switch statement, I do something like this
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

In Ruins [WIP]
(This post was last modified: 04-13-2013, 05:48 PM by NaxEla.)
04-13-2013, 05:48 PM
Find
Kreekakon Offline
Pick a god and pray!

Posts: 3,063
Threads: 70
Joined: Mar 2012
Reputation: 124
#8
RE: RandInt and Switch Statements.

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

[Image: Tv0YgQb.gif]
Image by BandyGrass
04-13-2013, 05:57 PM
Find
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




Users browsing this thread: 1 Guest(s)