User Tools

Site Tools


hpl2:amnesia:script_language_reference_and_guide:variables

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
hpl2:amnesia:script_language_reference_and_guide:variables [2012/12/21 22:23]
thegreatcthulhu
hpl2:amnesia:script_language_reference_and_guide:variables [2012/12/30 01:41]
thegreatcthulhu
Line 13: Line 13:
  
 The concept of variables is pretty much the same as used in math – a variable is something that can take on a number of different values. To be more precise, variables represent a scripter-friendly way to access locations in computer'​s memory – because we scripters are humans, it is easier for us to declare a variable with a meaningful, human-readable name, than to poke around the internal workings of the system. ​ The concept of variables is pretty much the same as used in math – a variable is something that can take on a number of different values. To be more precise, variables represent a scripter-friendly way to access locations in computer'​s memory – because we scripters are humans, it is easier for us to declare a variable with a meaningful, human-readable name, than to poke around the internal workings of the system. ​
 +You can use variables to store various types of data.
 +
 In order for it to be used in your code, a variable must first be //​declared//​.\\ ​ In order for it to be used in your code, a variable must first be //​declared//​.\\ ​
 A //variable declaration//​ is where you introduce the variable for the first time, thus making it "​known"​ to the script engine.\\ ​ A //variable declaration//​ is where you introduce the variable for the first time, thus making it "​known"​ to the script engine.\\ ​
Line 30: Line 32:
   * ''​float''​ – represents floating-point (real) numbers (like 3.14, 0.001, -12.0, 1.41)   * ''​float''​ – represents floating-point (real) numbers (like 3.14, 0.001, -12.0, 1.41)
   * ''​string''​ – represents text; string literals are always placed in quotes ("​Amnesia", ​ "HPL2 Engine"​)   * ''​string''​ – represents text; string literals are always placed in quotes ("​Amnesia", ​ "HPL2 Engine"​)
-  * ''​bool''​ – represents Boolean values (can be either true or false)+  * ''​bool''​ – represents Boolean values (can be either ​''​true'' ​or ''​false''​)
  
-You choose the type for your variable depending on what kind of data you want to store. You wouldn'​t choose ''​int''​ if you want to store text. The feature of the language that enforces type constraints on variables is called //type safety//. This is important for several reasons. First, it can help debug your code if problems arise. Next, implicit conversions are generally not allowed among all types. Also, operations on variables of different types yield different results. For example, if you add two int numbers, you get the mathematically correct result:\\+You choose the type for your variable depending on what kind of data you want to store. You wouldn'​t choose ''​int''​ if you want to store text. The feature of the language that enforces type constraints on variables is called //type safety//. This is important for several reasons. First, it can help debug your code if problems arise. Next, implicit conversions are generally not allowed among all types. Also, operations on variables of different types yield different results. For example, if you add two ''​int'' ​numbers, you get the mathematically correct result:\\
 2 + 3 = 5\\ 2 + 3 = 5\\
  
  
-If you add two strings, each of which is a textual representation of a number, the operation results in a new string which is a combination of the original two (string //​concatenation//​):​\\+If you add two ''​string''​-s, each of which is a textual representation of a number, the operation results in a new string which is a combination of the original two (string //​concatenation//​):​\\
 "​2"​ + "​3"​ = "​23"​\\ "​2"​ + "​3"​ = "​23"​\\
 "​Amne"​ + "​sia"​ = "​Amnesia"​ "​Amne"​ + "​sia"​ = "​Amnesia"​
Line 51: Line 53:
 }</​code>​ }</​code>​
  
-Let us now add two global variables, and use them to represent the durations for the screen fade effects. We will pass these variables to the fading functions ​instead ​of the numerical constants:+Let us now add two global variables, and use them to represent the durations for the screen fade effects. We will pass these variables to the fading functions ​in place of the numerical constants:
 <​code=c++>​float fadeOutTime = 0.0f;   // in seconds <​code=c++>​float fadeOutTime = 0.0f;   // in seconds
 float fadeInTime; ​          // (uninitialized) float fadeInTime; ​          // (uninitialized)
Line 78: Line 80:
 Variables can be declared and initialized in one place, and then be reused in several other places in the code. Changing the value of the variable will affect all the code that uses it. In programming,​ it is a good practice to avoid redundant repetitions; ​ when the same thing is repeated many times in many places, it is easy to make an error, but it is hard to fix it. Variables offer one way of organizing your code to avoid such repetition.\\ Variables can be declared and initialized in one place, and then be reused in several other places in the code. Changing the value of the variable will affect all the code that uses it. In programming,​ it is a good practice to avoid redundant repetitions; ​ when the same thing is repeated many times in many places, it is easy to make an error, but it is hard to fix it. Variables offer one way of organizing your code to avoid such repetition.\\
  
-Another point of interest is //where// the variables are declared. In this example, they are declared //​globally//,​ outside of any function. //Global variables// are accessible from every part of your script file (from every function).+=== Global vs Local Scope === 
 + 
 +Another point of interest is //where// the variables are declared. In the previous ​example, they were declared //​globally//,​ outside of any function. //Global variables// are accessible from every part of your script file (from every function).
 If a variable is declared //inside the body// of a function, it is called a //local// variable. //Local variables// are available for use only from within that same function (and only in lines that come after the one where the variable is declared). Trying to use such a variable from a different function would result in an error. ​ If a variable is declared //inside the body// of a function, it is called a //local// variable. //Local variables// are available for use only from within that same function (and only in lines that come after the one where the variable is declared). Trying to use such a variable from a different function would result in an error. ​
 +
 <​code=c++>​void OnEnter() <​code=c++>​void OnEnter()
 { {
Line 103: Line 108:
 void OnLeave() void OnLeave()
 { {
-    // The next line should result in a compilation ​error!+    // The next line should result in a compilation ​ERROR!
     fadeInTime = 1.0f;     fadeInTime = 1.0f;
 }</​code>​ }</​code>​
Line 128: Line 133:
 } }
 </​code>​ </​code>​
 +
 +<note tip>​Since the engine exposes the SetGlobalVar//​Typename//​() & SetLocalVar//​Typename//​() functions (and their Add- and Get- counterparts) for seting //game variables//,​ where the terms global and local have a slightly different meaning than what was presented here for //script variables//,​ here's a table that explains how these predefined functions and script-declared variables compare:</​note>​
 +
 +{{ :​hpl2:​amnesia:​script_language_reference_and_guide:​variablescope_table.jpg?​direct |}}
 +
 +<note tip>The functions which manipulate global game variables, SetGlobalVar//​Typename//​(),​ AddGlobalVar//​Typename//​() and GetGlobalVar//​Typename//​() [where //​Typename//​ can be either Int, Float, or String], provide a way for you to store and use a value in a storage location which is maintained for the duration of your game (or custom story). It can be accessed from more than one map script, and any changes made in one map will be visible from the others. They are global //relative to the maps//​.</​note>​
 +
 +<note tip>​Functions which manipulate local game variables, SetLocalVar//​Typename//​(),​ AddLocalVar//​Typename//​() and GetLocalVar//​Typename//​(),​ allow you to define and use storage locations for the current map script. They are visible from every part of the map script, once set for the first time. So they are //local to that particular map script// (can't use them from a different map), but are //global relative to functions// in that script. In this respect, they are similar to the script variables declared globally (outside any function, or code block).</​note>​
 +
 +=== Simultaneous Declarations ===
  
 It is possible to declare more than one variable in a single line, by separating variable names with commas. For example, the code\\ It is possible to declare more than one variable in a single line, by separating variable names with commas. For example, the code\\
Line 136: Line 151:
 <​code>​int a = 1, b = 2; <​code>​int a = 1, b = 2;
 int x = 5, y;</​code>​ int x = 5, y;</​code>​
 +
 +
 +=== Using Variables to Help Others (And Yourself!) ===
 +
 +Those of you who write code snippets, or even longer scripts, for your fellow forum members can use variables to make your code easier to use and understand. ​
 +Often, the same name or value appears in several places. Also, the script usually contains a lot of placeholder values, and when the person you are helping takes over the code, a lot of these placeholder values need to be replaced by the ones which are adequate in the context of their custom story or full conversion. The replacement process can be tedious and error prone, but variables come to the rescue (although search & replace tools can mitigate the problem).
 +
 +Instead of hard coding the names and values, you can assign them to variables at the beginning of your script, and then use variables instead.
 +This way, if any changes need to be made, it only needs to happen in //one place// - at the start, where the variable declarations are. This eliminates the need to dig through the code, trying to find every single appearance of some value.
 +
 +Also, remember to give your variables descriptive names, so that others (and you) can quickly figure out what are they used for!
 +You can indicate where the placeholder values should be replaced with a comment or two.
 +
 +<code c++>
 +// Example: Fictional Help-Script
 +// -----------------------------------
 +
 +// Replace these assignments to match the names in your map
 +string scriptArea_jumpScare1 = "​Area_JumpScare1";​
 +string scriptArea_jumpScare2 = "​Area_JumpScare2";​
 +string scriptArea_startMusic = "​Area_StartMusic";​
 +string scriptArea_lookAtTarget = "​Area_LookAt";​
 +string particleSys_Orbs = "​PS_Orbs";​
 +
 +// You can adjust these values as well, if you want
 +float screenFadeInTime = 6.0f;
 +float musicFadeInTime = 3.5f;
 +
 +// ...
 +
 +// Script that uses the variables above (omitted).
 +// Note: the script would use a variable wherever the corresponding value would otherwise appear. E.g.:
 +// FadeIn(screenFadeInTime); ​        // ---> instead of the hard-coded FadeIn(6.0f);​
 +</​code>​
 +
  
 ==== Naming Convention Tips ==== ==== Naming Convention Tips ====
Line 161: Line 211:
  
 Which approach to use is entirely your choice, but once you decide upon one, it is a good idea to be consistent and stick to it. This will make it easier for you and other people to read and understand your code. Which approach to use is entirely your choice, but once you decide upon one, it is a good idea to be consistent and stick to it. This will make it easier for you and other people to read and understand your code.
 +
 +See Also: [[hpl2:​amnesia:​script_language_reference_and_guide:​types|Types]].
  
hpl2/amnesia/script_language_reference_and_guide/variables.txt · Last modified: 2012/12/30 01:44 by thegreatcthulhu