This page will attempt to explain the basic facts about different statements that might be needed in order to get started. For a more in depth explanation when you are more confident with UnrealScript you can simply search the Unreal Wiki or UDN for any topics about Statements.
Iteration/Flow-Control Statements
While Loops: These cycle through loops while the expression at the start is true. For example.
function WhileLoop() { local int i; //local integer variable declared as "i". Integers default to 0. while( i < 10 ) //The expression that is evaluated. { //"i" starts at 0. 0 is less than 10 so the block of code contained within the square brackets is allowed to execute. i = i + 1; //"i" = "i" + 1. So now "i" = 1 because 0 + 1 = 1. The expression at the top is checked again. "i" is now 1, which is still } //less than 10, so the process repeats, it repeats until "i" = 10. //Now the loop has finished the code can continue from this point, after the while loop. }
Do Loops: These are very similar to while loops, the only difference is they cycle through loops while an expression at the end is true. Because of this, the block of code within the square brackets will always execute atleast once. For example.
function DoLoop() { local int i; do { i = i + 1; } until( i == 10 );//The expression is evaluated at the end. }
For Loops: These are again similar to while loops, but allow for more complicated loops. They also allow you to set some initial properties of the loop. “For Loops” will continue to loop aslong as the condition at the top is met. “For Loops” need 3 properties initialized before it will work. The first property is the initialization of the variable being used. In the example the local integer “i” is set to 0. The second property is the condition that needs to be met, in the example “i” must be less than 10. The third property is what to do if the condition is met. In this case “i” is incremented (i = i + 1).For example.
function ForExample() { local int i; for( i=0; i<10; i++ ) //So once again, aslong as i is less than 10 it will loop. { //Do something. //Place code to do here if the condition is met. //After this code is done the For loop cycles again and "i" is incremented by 1. } }
Selection/Conditional Statements
Selection statements execute different blocks of code depending on the result of an expression. These do not loop.
If, Else If, Else: An “if” statement evaluates an expression and executes one of two possible blocks of code. For example.
function IfExample() { if(i < 10)//The expression to evaluate goes here, within brackets. { //If "i" was less than 10 then do something here. //(If the expression was true). } else //else. { //if "i" was greater than 10 then do something else. //(If the expression was false). } //continue with code. }
If “else” is omitted, then nothing would happen if the expression was false. And the code would continue after the if statement. “else if” is simply an extension of the “if” statement. For example.
function ElseIfExample() { if(i < 10)//The first "if". { //If "i" is less than 10 then execute this code. } else if(i < 50)//Else if "i" is less than 50. { //Then execute this code. } else//Last else to catch all other possibilities of i. { //else "i" must be greater than or equal to 50. So execute this code. //If you omitt this last else, then if "i" is greater than or equal to 50 nothing //would happen and the rest of the function would exectue as normal. } //Continue the rest of the function. }
Switch: A Switch statement selects some code to execute based on the value of the expression. For example.
function SwitchExample() { switch(i) //Evaluate "i". { case 0: //if "i" matches this case (if "i" equals 0). //Do something. break; //The special "break" statement stops the rest of the switch statement from being executed, the code preceeds past the switch statement. case 1: //if "i" matches this case. //Do something. break; case 2: //if "i" matches this case. //Do something. break; default: //if "i" matches none of the above then the case default to this section. //Do something. Note that if this default option is omitted then nothing will happen if "i" matched no cases. The code //would just continue past the switch statement. break; } //Continue with the rest of the code. }
Jump/Command Statements
Jump statements jump to different sections of the function or state.
Break: The “break” command is used to jump out of/or cancel a switch statement or loops. They allow you to cancel the loop or switch when something happens. In the case of the switch statement, “break” prevents other cases from being executed. If there was no “break” then all of the cases would execute. For example.
function BreakExample() { switch(i) { case 0: //Do Something. break; //Stop executing further switch code. //The code then jumps to the end of the switch statement. default://If there was no "break" then this default case would be executed also. //Do Something. break; } //Continue with the rest of the code. }
This is an example of a “break” statement being used to exit a “for” loop.
function ForBreakExample() { for(//Some Expression...) { //Execute this code. if(Done)//If we are finished here. { //Do Something. break;//Then "break" and exit the loop on the next iteration. } } //Continue... this is where the "break" takes us. }
Continue: The “continue” command is used to skip code and jump to the beginning of the loop. For example.
function WhileContinueExample() { local bool ShouldSkip; //Booleans default to false. while(//Something is true/false.) { //Do something. //Something happens here that makes us want to skip the end of the while loop code. //So we set ShouldSkip (which is a boolean) to true. if(ShouldSkip) { //Continue from the beginning of the while loop. continue; } //This section gets skipped. } }
Return: The “return” command is used to jump out of a statement and if needed returns a value. For example.
//For functions that do not require a return value you just put "return" when you want to stop the code from continuing. function ReturnExample() { //Do something... if something happens and we don't want anymore code to execute we use the "return" command. Return; //Code after the "return" will not be executed. The function is simply ended. } //For functions that return a value, the "Return" command must be followed by the relevant expression. function int ReturnExample() { //For functions that do require return value you just put "return" followed by a relevant expression. //In this case the function is expecting an integer value to be returned. Return 10; //Code after the "return" will not be executed. The function is simply ended. } //An example of a function expecting a boolean to be returned. function bool ReturnExample() { if(i < 10) { //Return true if this condition is met. Return True: } else { //Else return false. Return False; } }
Goto: The “goto” command is used to jump to a specific section of code that is labelled. For example.
//In this Goto example the code can be directed to the "YellowCode" label via 2 different routes. //Labels can be anything you want, I am using colours in this example. function GotoExample() { if(i < 10) { //goto "label". goto RedCode; } else { goto BlueCode } GreenCode://Green label. //Do Something... goto YellowCode; RedCode://Red label. //Do Something... goto GreenCode; BlueCode://Blue label. //Do Something... goto YellowCode YellowCode://Yellow label. //Continue with code... }
TIP – Never ending loops are very bad and will crash the game.
Recent Comments