[GLOBAL] How to use Conditional Statements (if, while)

Tutorial forum for Tony Hawk games.
Forum rules
No requests, use other sections for that. TAG your titles [THAW][THUG2] etc.
Anteara
Posts: 268
Joined: Thu Oct 14, 2010 2:55 am
Contact:

[GLOBAL] How to use Conditional Statements (if, while)

Postby Anteara » Thu Jan 26, 2012 2:49 pm

This tutorial will show you how to use basic conditional statements. Conditional statements are good because they allow you to reduce the amount of lines in your code while maintaining efficiency.

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 endif


Code: Select all

:i if NOT $condition$
     :i $Your_function$
 :i endif


Code: 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.

Image

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_to

Code: 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.
Last edited by Anteara on Fri Jan 27, 2012 12:41 am, edited 2 times in total.
WhoElseButMe
Posts: 420
Joined: Tue Aug 04, 2009 12:50 am
Location: FL - USA
Contact:

Re: [GLOBAL] How to use Conditional Statements (if, while)

Postby WhoElseButMe » Thu Jan 26, 2012 8:13 pm

Nice addition to the tutorials section, here's my contribution.
Anteara wrote:If statements are to be used when all you need to do is check if something is true or not.

While the underlying result MUST equate to a Boolean value (true or false) you can also perform arithmetic to get your Boolean value.

Code: Select all

:i $my_value1$ = %i(1,00000001)
:i $my_value2$ = %i(5,00000005)
:i if ($my_value1$ < $my_value2$)
     :i $do_something$
:i endif

Code: Select all

:i $my_value1$ = %i(1,00000001)
:i $my_value2$ = %i(5,00000005)
:i if (($my_value1$ + $my_value2$) > %i(10,0000000A))
     :i $do_something$
:i endif
Image
WhoElseButMe on Nov 26, 2009 wrote:It's that lack of respect amongst their peers and ignorance towards modding etiquette that keeps us who know this stuff well from spreading it like wild fire. We do still enjoy playing the game and if you need to cheat to play a game PLAY SOMETHING ELSE YOU DON'T SUCK AT.
Morten1337
Posts: 132
Joined: Tue Jun 08, 2010 11:11 pm
Contact:

Re: [GLOBAL] How to use Conditional Statements (if, while)

Postby Morten1337 » Fri Jan 27, 2012 11:20 pm

We also have AND, & OR statements.

Example:

Code: Select all

      :i if ( (%GLOBAL%$angle$ > %f(0.000000)) AND (%GLOBAL%$angle$ < %f(22.500000)))
         :i return $new_angle$ = %f(0.000000)
      :i endif


Code: Select all

      :i if ((%GLOBAL%$index_float$ > %GLOBAL%$max_float$) OR (%GLOBAL%$index_float$ = %GLOBAL%$max_float$))
         #// [...]
      :i endif

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 1 guest