SC2Mapster Wiki
Explore
Main Page
All Pages
Interactive Maps
navigation
Main page
Community portal
Recent changes
Random page
Help
SC2MAPSTER
News
Maps
Assets
Teams
Authors
Forums
Paste
Gamepedia
Gamepedia support
Report a bad ad
Help Wiki
Contact us
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
SC2Mapster Wiki
1,003
pages
Explore
Main Page
All Pages
Interactive Maps
navigation
Main page
Community portal
Recent changes
Random page
Help
SC2MAPSTER
News
Maps
Assets
Teams
Authors
Forums
Paste
Gamepedia
Gamepedia support
Report a bad ad
Help Wiki
Contact us
Editing
Language Overview
(section)
Back to page
Edit
VisualEditor
View history
Talk (0)
Edit Page
Language Overview
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Flow Control Statements=== Flow control statements are any statement which changes the sequence of execution. Normally programs will execute sequentially evaluating every statement in order of declaration. By using flow control it is possible to alter this order by making the code executed depend on the evaluation of certain tests. ====Conditional Selection Statement (if)==== Galaxy supports conditional selection statements in the form of the usual "if" statement. Unlike C conditional statements the jump is made based on a bool type (instead of int) and always requires a code block (no inlined code). Conditional statements are commonly used when scripting as they allow the conversion of a logical test (a bool value) into a branch in the executed code. Multiple conditional branches and even a failure branch can be defined greatly improving the dynamic nature of your code. For example when a player earns a point one might test the number of points he has against a constant and if the test passes then end the game otherwise play some voiceover to everyone. The syntax of the conditional selection statement is defined as... <syntaxhighlight lang="c"> if (bool runblock) { code block } optional repeatable else if (bool ''runblock'') { ''code block'' } ''optional'' '''else''' { ''code block'' } ''end'' </syntaxhighlight> The functionality is such that if ''runblock'' is true then run the following code block otherwise advance to next '''else if''' statement if any and repeat until no more "else if" statements remain. If none of the checked statements pass then run the code block following the '''else''' statement if any. After running a code block or if the no suitable code block was found then resume program execution from ''end''. When a block is executed any following '''else if''' statements will not have their ''runblock'' value evaluated. The conditional selection statement can be used like... <syntaxhighlight lang="c"> // Single selection for random chance. if (Random(1, 5) <= 1) { // Code has a 1 in 5 chance to execute. } // Multiple selection for common choice and less common choices. if (Random(1, 5) <= 4) { // Code has a 4 in 5 chance to execute. } else if (Random(1, 5) <= 4) { // Code has a 16 in 25 chance to execute. } else if (Random(1, 5) <= 4) { // Code has a 64 in 125 chance to execute. } // Multiple selection with failure case for rare choices and failure result. if (Random(1, 64) <= 1) { // Code has a 1 in 64 chance to execute. } else if (Random(1, 8) <= 1) { // Code has a 63 in 512 chance to execute. } else // Code has a 441 in 512 chance to execute. } </syntaxhighlight> Some hints for using the conditional selection statement... * If you are testing the same value for multiple specific cases a case statement should rather be used. Case statements might compile to perform decision making more efficiently than multiple selection. * If you are testing multiple mutually exclusive events then it is recommended to test the one most likely to occur first. On average it will result in fewer tests being performed and so execute faster. * The '''if''' and '''else if''' statements expect a bool value. Although this is usually the result of a comparison operation it can also come from a function return value or from evaluating a bool type variable. This is a subtle feature unavailable in the standard StarCraft II Editor GUI which forces you to always use a comparison. ====Conditional Repetition Statement (while)==== Galaxy supports conditional repetition statements in the form of the usual "while" statement. Unlike C conditional statements the loop is run based on a bool type (instead of int) and always requires a code block (no inlined code). Conditional repetition is used to repeat jobs a number of times until the condition is failed. The condition used is usually influenced by the code block such that after a finite number of iterations it will fail and execution will continue after the loop. It is possible to escape from a loop by executing the break statement. The syntax of the conditional repetition statement is defined as... <syntaxhighlight lang="c"> '''while''' ( bool ''runblock'' ) { ''code block'' } ''end'' </syntaxhighlight> The functionality is such that ''runblock'' is evaluated and if true the following code block is executed and the sequence repeated. If either ''runblock'' evaluates false or a '''break''' is used inside the code block then execution resumes from end. The conditional repetition statement can be used like... <syntaxhighlight lang="c"> // A loop which never runs its block. while ( false ) { // This never runs! } // A loop that never stops running its block. while ( true ) { // This never stops running! An infinite loop. } // A simple loop that runs 5 times. // Variable i is of type int. i = 0; while ( i < 5 ) { // Code here will run 5 times. i+= 1; } // The variable i has the value of 5. // Using an infinite loop with a test performed inside the code block. i = 0; while ( true ) { i+= 1; // Code here will be repeated 6 times. // End when i is greater than 10. if ( i > 10 ) { break; } // Code here will be repeated 5 times. i+= 1; } // The variable i has the value of 11. </syntaxhighlight> Some hints for using the conditional repetition statement... * Computers have finite computational resources so cannot execute infinite loops. An infinite loop will either cause an operation limit thread crash or make the game become unresponsive. * Even a simple code block can cause performance problems if repeated enough. Such blocks are prime targets for optimization. * You can use a '''break''' statement inside the code block to end the loop at any time. You can use this to end the code block pre-maturely. * With an infinite loop and an '''if''' statement the '''break''' statement can be used to move the test expression inside the code block. Useful if you need the code block to always run at least partly. ====Conditional Repetition Statement (for)==== <syntaxhighlight lang="c"> int i = 0; for(; i < 10;i+=1) { UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("Integer: "+IntToString(i))); } </syntaxhighlight>
Summary:
Please note that all contributions to the SC2Mapster Wiki are considered to be released under the CC BY-NC-SA
Cancel
Editing help
(opens in new window)
Follow on IG
TikTok
Join Fan Lab