Difference between revisions of "Scripting Tutorial: Creating a Simple Timer"

imported>Shademe
(→‎New Article: Ported a tutorial of mine to the wiki)
 
 
(9 intermediate revisions by 5 users not shown)
Line 3: Line 3:
For this exercise, we shall be creating a simple timer that displays a message after a certain amount of time passes.
For this exercise, we shall be creating a simple timer that displays a message after a certain amount of time passes.


{{Tools|req0=[[The Elder Scrolls Construction Set|Construction Set]]}}


== Requirements ==
== Requirements ==
Basic knowledge of:
Basic knowledge of:
* Scripting in Oblivion
* Coding
* The Construction Set and it's usage
* The [[Tescs|Construction Set]] and its usage
 


== Commands and Functions used ==
== Commands and Functions used ==
Line 20: Line 20:
== Tutorial ==
== Tutorial ==
* Start the Construction Set. We need not load the game's master file('''Oblivion.esm''') for this exercise for we will not be using any resources from it.
* Start the Construction Set. We need not load the game's master file('''Oblivion.esm''') for this exercise for we will not be using any resources from it.
* Open the script window and use the File menu to create a new one. Set the script type to '''Quest Script'''
* Open the [[Edit Scripts|script window]] and use the File menu to create a new one. Set the script type to [[Quest script]]
* Start with the script name:
* Start with the script name:
  scn <name of the script>
  scn <name of the script>
We shall call it '''timerTestScript'''.
We shall call it '''timerTestScript'''.
* Declare a ''float'' variable. We shall call it '''timer'''
* Declare a ''float'' [[variable]]. We shall call it '''timer'''
<pre>scn timerTestScript
<pre>scn timerTestScript


float timer</pre>
float timer</pre>
Float is used as the data type because '''getSecondsPassed''' returns a floating point value,i.e., the time that has passed since the last time the function was called
Float is used as the data type because [[getSecondsPassed]] returns a [[floating point]] value, i.e., the time that has passed since the last time the function was called
* We shall now define the timer.
* We shall now define the timer.
<pre>scn timerTestScript
<pre>scn timerTestScript
Line 45: Line 45:
end</pre>
end</pre>
The duration of the timer is set in the if condition. [[fQuestDelayTime]] is a special variable used in quest scripts to change their processing speeds.
The duration of the timer is set in the if condition. [[fQuestDelayTime]] is a special variable used in quest scripts to change their processing speeds.
* Save the script. Create a quest with the '''Start Game Enabled''' flag and attach the timer script to it
* Save the script. Create a [[quest]] with the '''Start Game Enabled''' flag and attach the timer script to it
* Finally, save the plugin. Launch the game with it activated
* Finally, save the plugin. Launch the game with it activated




== Mechanics ==
== Mechanics ==
The quest script starts when the game is loaded. The gameMode block runs every frame. On each run ( in this case, every frame ), the timer variable is checked for the condition ( i.e., if it's less than 5 ). If the condition evaluates true, the timer variable is incremented with the value returned by the '''getSecondsPassed''' function.
The quest script starts when the game is loaded. The [[gameMode]] block runs every frame. On each run ( in this case, every frame ), the timer variable is checked for the condition ( i.e., if it's less than 5 ). If the condition evaluates true, the timer variable is incremented with the value returned by the '''getSecondsPassed''' function.


When the condition evaluates false i.e., when the timer variable's value equals or exceeds 5, our message is displayed. Since '''getSecondsPasssed''' returns values that are comparable to realtime seconds, the script has effectively run for 5 seconds.
When the condition evaluates false i.e., when the timer variable's value equals or exceeds 5, our message is displayed. Since '''getSecondsPasssed''' returns values that are comparable to realtime seconds, the script has effectively run for 5 seconds.


But the script doesn't end here - it keeps calling the message since the if condition checks only if the timer's value equals or exceeds 5. The If statement's condition can be modified to walk through this issue by adding a do-once catch.
But the script doesn't end here - it keeps printing the message since the if condition checks only if the timer's value equals or exceeds 5. The If statement's condition can be modified to walk through this issue by adding a do-once catch.
<pre>scn timerTestScript
<pre>scn timerTestScript


Line 66: Line 66:


     if ( doOnce )        ; the == operator isn't necessary when checking boolean values....
     if ( doOnce )        ; the == operator isn't necessary when checking boolean values....
         return       ; ... the above expression is equivalent to ( doOnce != 0 )
         return           ; ... the above expression is equivalent to (doOnce != 0)
     elseif( timer > 5 )     
     elseif( timer > 5 )     
         set doOnce to 1;do things else   
         set doOnce to 1
        Message "Your 5 seconds are up" 
     else
     else
         set timer to timer + getSecondsPassed
         set timer to timer + getSecondsPassed
Anonymous user