IF Statements
IF statements are good because they can allow you to check a IF, or IF NOT a condition is true.
IF statements can be structured to either check IF a condition is true (a), or if a condition is false (b). It can also be structured using an else, which will allow you to run your script if the condition isn't met (c).
An else statement is completely optional.
For our example, we want to check if the skater is currently bailing. This may be useful as some functions crash when called when the skater is bailing.
Syntax
These are the three ways you can structure an IF statement, you set a condition, and whether or not the condition is met you tell it to perform a task.
Syntax for the IF statement is very easy, it is structured as follows:Code: Select all
:i if $condition$
:i $Your_function$
:i endifCode: Select all
:i if NOT $condition$
:i $Your_function$
:i endifCode: Select all
:i if $condition$
:i $Your_function$
:i else
:i $Other_function$
:i endif
Here is an if statement written by neversoft.Code: Select all
:i if NOT $GameIsPaused$
:i %GLOBAL%$CameraId$.$UnPause$
:i endif
This simply says, if condition (game is NOT paused), unpause the current camera.
IF statements can also have an IF inside an IF, etc.
For example:Code: Select all
:i if $condition$
:i if $condition_2$
:i $perform_tasks$
:i else
:i $perform_other_tasks$
:i if NOT $condition_3$
:i $something$
:i endif
:i endif
:i endif
If statements are to be used when all you need to do is check if something is true or not, if you need something more advanced you should look at using a while statement.
WHILE Statements
While statements are slightly more complex, they allow you to run your scripts WHILE a condition is met. Because this may be confusing, I will provide you with an example in psuedocode.Code: Select all
WHILE condition_is_met:
Do stuff
ENDWHILE
This is how a while statement works, if your condition is met, you can tell the function to perform tasks.
While statements are structured weird in THPS, you start your while statement with no conditions, then proceed to create an if statement which will check for your condition. you must also end your while statement with loop_toCode: Select all
:i while
:i if $condition_is_met$
:i $perform_tasks$
:i endif
:i loop_to
Simply writing loop_to means that the statement will be looped constantly. You can add an amount of gameframes or seconds at the end of the loop_to to make it loop for that many seconds/frames.Code: Select all
:i $wait$%i(1,00000001)$gameframe$
:i loop_to %i(80,00000050)
The above tells the while statement to loop for 80 gameframes.
Here is a while statement written by neversoft:Code: Select all
:i while
:i $wait$%i(1,00000001)$gameframe$
:i if call $held$ arguments
$up$
:i continue
:i endif
:i if $Crouched$
:i continue
:i endif
:i loop_to
What this does is check while up is held, continue what it's doing, alternatively while crouched, continue what it's doing. it simply says loop_to, which means that the conditions inside the loop should be constantly looped.
While statements are used to perform very complex tasks, if you only need to check if a condition is met, you should use an if statement. If you have something that you need to be looped, or constantly checked, then a while statement is suggested.
To see how complex, and well both while and if statements can be when used together, consult the code below that I wrote for myself a while back.
Code: Select all
:i $previously_held$ = %i(0,00000000)
:i while
:i if (%GLOBAL%$previously_held$ = %i(1,00000001))
:i if call $released$ arguments
$white$
:i if ($Anteara_TestingButton$ = %i(3,00000003))
:i call $change$ arguments
$Anteara_TestingButton$ = ($Anteara_TestingButton$ - %i(2,00000002))
:i $skater$.call $PlayAnim$ arguments
$Anim$ = $CoffinGrind_Out$
:i $skater$.$WaitAnimFinished$
:i $anteara_key_exit_luge$
:i else
:i call $change$ arguments
$Anteara_TestingButton$ = ($Anteara_TestingButton$ + %i(1,00000001))
:i endif
:i $previously_held$ = %i(0,00000000)
:i endif
:i else
:i if call $held$ arguments
$white$
:i $previously_held$ = %i(1,00000001)
:i endif
:i endif
:i $wait$%i(1,00000001)$gameframe$
:i loop_to %i(80,00000050)
:i call $change$ arguments
$Anteara_TestingButton$ = ($Anteara_TestingButton$ - %i(2,00000002))
If you can grasp the concept of while and If statements, then your code will be much simpler, smaller, and easier to read.