CabConModding
Facebook
Twitter
youtube
Discord
Contact us
RSS
Menu
CabConModding
Home
New
Top
Premium
Rules
FAQ - Frequently Asked Questions
Games
Fornite
Call of Duty: Black Ops 3
Clash of Clans
Grand Theft Auto 5
Apex Legends
Assassin’s Creed Origins
Forums
Premium
Latest posts
What's new
Latest posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Log in
Register
What's new
Premium
Latest posts
Menu
Log in
Register
Navigation
Install the app
Install
More options
Dark Theme
Contact us
Close Menu
Forums
Gaming
Call of Duty Classics
Tutorial Section
Basic GSC Coding: Switch
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="CabCon" data-source="post: 581" data-attributes="member: 1"><p>Hello CCM,</p><p>In this tutorial I am going to explain <strong>how does the function "switch" works</strong> and how to use it. It is used when you have to use a lot of "if"s, to reduce the ammount of "if"s you use this switch. See the comparison below:</p><p></p><p>[Code]</p><p>number = randomIntRange( 1, 5 );</p><p>if( number == 1 )</p><p> a = 1;</p><p>if( number == 2 )</p><p> a = 2;</p><p>if( number == 3 )</p><p> a = 3;</p><p>if( number == 4 )</p><p> a = 4;</p><p>if( number == 5 )</p><p> a = 5;</p><p>[/Code]</p><p></p><p>You can do:</p><p>[Code]</p><p>number = randomIntRange( 1, 5 );</p><p>switch( number )</p><p>{</p><p> case 1:</p><p> a = 1;</p><p> break;</p><p></p><p> case 2:</p><p> a = 2;</p><p> break;</p><p></p><p> case 3:</p><p> a = 3;</p><p> break;</p><p></p><p> case 4:</p><p> a = 4;</p><p> break;</p><p></p><p> case 5:</p><p> a = 5;</p><p> break;</p><p>}</p><p>[/Code]</p><p></p><p>The switch structure is like this:</p><p>[Code]</p><p>switch( something (variable for example) )</p><p>{</p><p> case 0: //After the "case" name add a ":"</p><p> HERE ADD CASE CODE</p><p> break; //Add break to stop the switch "searching"</p><p>}</p><p>[/Code]</p><p></p><p><strong>You can add as many cases as you want</strong>, is not necesary to call cases with numbers, you can call them with different names, the names go beetween " ".</p><p>[Code]</p><p>switch( colour )</p><p>{</p><p> case "Green":</p><p> SOMETHING</p><p> break;</p><p></p><p> case "Blue":</p><p> SOMETHING</p><p> break;</p><p>}</p><p>[/Code]</p><p></p><p>"switch" command also <strong>allows you to make some kind of "else" function</strong>, in this case is called "default", you use it like this:</p><p>[Code]</p><p>number = RandomInt( 5 )</p><p>switch( number )</p><p>{</p><p> case 0:</p><p> a = 0;</p><p> break;</p><p></p><p> case 1:</p><p> a = 1;</p><p> break;</p><p></p><p> default:</p><p> a = 6;</p><p> break;</p><p>}</p><p>[/Code]</p><p></p><p>In the previous example, number will be a random number beetween 0 and 5, if the number is equal to 0: a = 0. If the number is 1: a = 1, If number is not 0 or 1, it will be the default, in this case: a = 7.</p><p></p><p>This kind of functions allow you to give the same effect to different "case"s like this, it helps to save space (in the example case 0, 1 and 2 will have the same effect):</p><p>[Code]</p><p>number = RandomInt( 5 )</p><p>switch( number )</p><p>{</p><p> case 0:</p><p> case 1:</p><p> case 2:</p><p> SOMETHING</p><p> break;</p><p></p><p> case 3:</p><p> SOMETHING</p><p> break;</p><p>}</p><p>[/Code]</p><p></p><p>Lets put a decent example (is a code I released sometime ago):</p><p>[Code]</p><p>WaitT( unit, ammount )</p><p>{</p><p> switch( unit )</p><p> {</p><p> case "minutes":</p><p> wait ( ammount * 60 );</p><p> break;</p><p></p><p> case "hours":</p><p> wait ( ammount * 3600 );</p><p> break;</p><p></p><p> case "miliseconds":</p><p> wait ( ammount / 1000 );</p><p> break;</p><p></p><p> case "deciseconds":</p><p> wait ( ammount / 10 );</p><p> break;</p><p></p><p> case "centiseconds":</p><p> wait ( ammount / 100 );</p><p> break;</p><p></p><p> case "microseconds":</p><p> wait ( ammount * 0.000001 );</p><p> break;</p><p></p><p> case "frames":</p><p> wait ( 0.05 * ammount );</p><p> break;</p><p></p><p> case "days":</p><p> wait ( ammount * 86400 );</p><p> break;</p><p></p><p> case "weeks":</p><p> wait ( ammount * 604800 );</p><p> break;</p><p></p><p> case "tenminutes":</p><p> wait ( 600 * ammount );</p><p> break;</p><p> }</p><p>}</p><p>[/Code]</p><p></p><p>In the previous example you add on the function the variable unit, which has to match in name with any of "case"s names (minutes, centiseconds, weeks...), then, after you assigned the unit, the switch will make your mod wait the ammount of time units you specified on the function name.</p><p></p><p>Thanks for reading. [USER=1]@CabCon[/USER]. Credits are going to Yamato.</p></blockquote><p></p>
[QUOTE="CabCon, post: 581, member: 1"] Hello CCM, In this tutorial I am going to explain [B]how does the function "switch" works[/B] and how to use it. It is used when you have to use a lot of "if"s, to reduce the ammount of "if"s you use this switch. See the comparison below: [Code] number = randomIntRange( 1, 5 ); if( number == 1 ) a = 1; if( number == 2 ) a = 2; if( number == 3 ) a = 3; if( number == 4 ) a = 4; if( number == 5 ) a = 5; [/Code] You can do: [Code] number = randomIntRange( 1, 5 ); switch( number ) { case 1: a = 1; break; case 2: a = 2; break; case 3: a = 3; break; case 4: a = 4; break; case 5: a = 5; break; } [/Code] The switch structure is like this: [Code] switch( something (variable for example) ) { case 0: //After the "case" name add a ":" HERE ADD CASE CODE break; //Add break to stop the switch "searching" } [/Code] [B]You can add as many cases as you want[/B], is not necesary to call cases with numbers, you can call them with different names, the names go beetween " ". [Code] switch( colour ) { case "Green": SOMETHING break; case "Blue": SOMETHING break; } [/Code] "switch" command also [B]allows you to make some kind of "else" function[/B], in this case is called "default", you use it like this: [Code] number = RandomInt( 5 ) switch( number ) { case 0: a = 0; break; case 1: a = 1; break; default: a = 6; break; } [/Code] In the previous example, number will be a random number beetween 0 and 5, if the number is equal to 0: a = 0. If the number is 1: a = 1, If number is not 0 or 1, it will be the default, in this case: a = 7. This kind of functions allow you to give the same effect to different "case"s like this, it helps to save space (in the example case 0, 1 and 2 will have the same effect): [Code] number = RandomInt( 5 ) switch( number ) { case 0: case 1: case 2: SOMETHING break; case 3: SOMETHING break; } [/Code] Lets put a decent example (is a code I released sometime ago): [Code] WaitT( unit, ammount ) { switch( unit ) { case "minutes": wait ( ammount * 60 ); break; case "hours": wait ( ammount * 3600 ); break; case "miliseconds": wait ( ammount / 1000 ); break; case "deciseconds": wait ( ammount / 10 ); break; case "centiseconds": wait ( ammount / 100 ); break; case "microseconds": wait ( ammount * 0.000001 ); break; case "frames": wait ( 0.05 * ammount ); break; case "days": wait ( ammount * 86400 ); break; case "weeks": wait ( ammount * 604800 ); break; case "tenminutes": wait ( 600 * ammount ); break; } } [/Code] In the previous example you add on the function the variable unit, which has to match in name with any of "case"s names (minutes, centiseconds, weeks...), then, after you assigned the unit, the switch will make your mod wait the ammount of time units you specified on the function name. Thanks for reading. [USER=1]@CabCon[/USER]. Credits are going to Yamato. [/QUOTE]
Verification
Post reply
Forums
Gaming
Call of Duty Classics
Tutorial Section
Basic GSC Coding: Switch
CabConModding is now on facebook! Check the latest Updates, the Site Status and much more now!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top