ChoiceScript Wiki
(Changed use of var, added note about crashes for checking undefined variables)
m (modified previous command)
Tag: sourceedit
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
  +
{{Infobox command
The '''*temp''' command is used to create a ''temporary'' variable, as opposed to a ''permanent'' (saved value) variable defined in [[mygame.js]]. To better understand the main differences between these two types of variables, read the article on [[Variable types|variable types]].
 
  +
|title= *temp
  +
|use= Creates new temporary variables within a scene file.
  +
|relatedcommands= [[create|*create]]<br>[[set|*set]]<br>[[Data types]]<br>[[Variable types]]<br>[[Scenes]]<br>[[startup.txt]]
  +
|previous= [[stat chart|*stat_chart]]
  +
|next= [[title|*title]]
  +
}}
  +
The '''*temp''' command is used to create a ''temporary'' variable, as opposed to a ''permanent'' variable defined in [[startup.txt]] using the [[create|*create]] command.
   
 
To better understand the main differences between these two types of variables, read the article on [[Variable types|variable types]].
  +
  +
== Temporary variables ==
 
As with permanent variables, a ''temporary'' variable can also be any one of the three different [[data types]], either ''numeric'' (containing a number), a ''string'' (containing "text"), or a ''boolean'' (either ''true'' or ''false'').
 
As with permanent variables, a ''temporary'' variable can also be any one of the three different [[data types]], either ''numeric'' (containing a number), a ''string'' (containing "text"), or a ''boolean'' (either ''true'' or ''false'').
   
In all three cases a new temporary variable is created in the exact same way, simply by assigning it a new ''and unique'' name:
+
In all three cases a new temporary variable is created in the exact same way, simply by assigning it a new ''and unique'' name and a default starting value:
*temp var
+
*temp var false
In the above example we have effectively created a new temporary variable named ''var'' but have yet to either properly define its '''data type''' or to assign it a specific starting '''value'''. For ''temporary'' variables, both of those important elements are governed by the first [[set|*set]] command applied to this new named variable. As a general rule, it is usually good practice to follow any '''*temp''' command with an appropriate '''*set''' immediately, or at least shortly after, as anything that checks the value of the variable (such as an [[If|*if]]) will cause a game stopping error.
+
In the above example we have effectively created a new temporary variable named ''var'' and have assigned a default starting value of '''false''', making it a ''boolean'' variable type. We could instead have assigned a number
  +
*temp var 0
  +
making it a ''numeric'' variable, or "some text" to make it a ''string'' variable:
  +
*temp var "Alex"
   
  +
In all cases the [[set|*set]] command may then be used - ''within the same scene file'' - to modify the initial default value, or we could perhaps make use of it for conditional [[if|*if]] / [[else|*else]] statements. Also, with the [[rand|*rand]] command we could immediately after randomize the initial value of any ''numeric'' temporary variable.
'''Tip''': Alternatively, the [[rand|*rand]] command can also be used to "set" the starting (randomly-generated) '''value''' and '''data type''' (''numeric'' only) of a new temporary variable, in which event it is ''not'' necessary to '''*set''' it first.
 
   
 
'''Temporary Variables ''really are'' ''Temporary!'''''
 
'''Temporary Variables ''really are'' ''Temporary!'''''
Line 13: Line 26:
 
The most important thing to bear in mind concerning '''*temp''' variables is that they are exactly that -- '''''temporary'''''. As soon as your game loads a new [[scenes|scene]] file as a result of either a [[finish|*finish]] or [[goto_scene|*goto_scene]] command, '''''all''''' of your temporary variables -- and their values -- ''are'' ''lost from memory''. In essence, temporary variables can only be used within the '''''current''''' scene file.
 
The most important thing to bear in mind concerning '''*temp''' variables is that they are exactly that -- '''''temporary'''''. As soon as your game loads a new [[scenes|scene]] file as a result of either a [[finish|*finish]] or [[goto_scene|*goto_scene]] command, '''''all''''' of your temporary variables -- and their values -- ''are'' ''lost from memory''. In essence, temporary variables can only be used within the '''''current''''' scene file.
   
While you can of course create new '''*temp''' variables of the same name in the ''next'' '''scene''' file, those would count as ''brand new'' variables and all previously-held '''values''' would still have been lost. Where this may cause some continuity problems in either your story or your scripting, those particular variables should instead be defined in [[mygame.js]] as ''permanent'' (saved value) variables.
+
While you can of course create new '''*temp''' variables of the same name in the ''next'' '''scene''' file, those would count as ''brand new'' variables and all previously-held '''values''' would still have been lost. Where this may cause some continuity problems in either your story or your scripting, those particular variables should instead be defined in [[startup.txt]] as ''permanent'' variables, using the [[create|*create]] command.
 
'''Where & When to use Temporary Variables'''
 
   
  +
== Where & when to use temporary variables ==
 
Temporary variables are specifically intended to carry short-term information, and for virtually any purpose. They are typically used to carry ''conditional'' data for later use in such as [[if|*if]], [[selectable_if|*selectable_if]], and perhaps [[hide_reuse|*hide_reuse]] conditions: '''''if''''' the player met Colonel Mustard; '''''if''''' the player kissed Miss Scarlett; '''''if''''' the player visited the library; '''''if''''' the player saw the bloodstained candlestick . . . or indeed everything and anything that you might need to record for only a short period of time in order to determine ''how'' -- or indeed, ''if at all'' -- that recent event will impact something else ''in the near future''.
 
Temporary variables are specifically intended to carry short-term information, and for virtually any purpose. They are typically used to carry ''conditional'' data for later use in such as [[if|*if]], [[selectable_if|*selectable_if]], and perhaps [[hide_reuse|*hide_reuse]] conditions: '''''if''''' the player met Colonel Mustard; '''''if''''' the player kissed Miss Scarlett; '''''if''''' the player visited the library; '''''if''''' the player saw the bloodstained candlestick . . . or indeed everything and anything that you might need to record for only a short period of time in order to determine ''how'' -- or indeed, ''if at all'' -- that recent event will impact something else ''in the near future''.
   
  +
== Error: "Non-existent variable [name]" ==
[[Index|Back to the Index.]]
 
  +
As discussed in the Wiki article [[Basic Scene Scripting]], "'''''non-existent variable'''''" errors are all too common when first learning ChoiceScript, as it can at first be difficult to grasp the logic behind what precisely causes such an error to occur.
 
'''Related Articles:'''
 
   
  +
If not a simple misspelling of the variable name (a single letter wrong - or missing - is all it takes) the cause is almost always due to ''where'' the '''*temp''' command actually appears in your scripting, as explained in the ''Basic Scene Scripting'' article.
*[[finish]]
 
*[[goto_scene]]
 
*[[hide_reuse]]
 
*[[if]]
 
*[[selectable_if]]
 
   
  +
The simplest solution, and the easiest way of avoiding many such errors, is to always list all '''*temp''' commands''' ''at'' ''the very top''''' of each scene file in which they are intended to be used, with a ''default starting value'' appropriate to that '''data type''', and which you would simply [[set|*set]] accordingly later in your story scripting to suit. Examples of ''default starting values'' placed at the start of each '''scene''' file would be:
*[[Set|set]]
 
  +
*temp var1 0 [''a '''numeric''' default value of zero'']
*[[Data types|Data Types]]
 
  +
*[[mygame.js]]
 
  +
*temp var2 "" [''a blank '''string''' value using double quotes with no content'']
*[[Scenes]]
 
  +
  +
*temp var3 false [a'' default '''boolean''', which must always be either '''true''' or''' false''''']
  +
By this means, the variable itself would always exist throughout that [[scenes|scene]] file and "'''''non-existent variable'''''" type errors will occur far less often.
   
  +
{{footer commands}}
*[[Variable types|Variable Types]]
 
  +
[[Category:Commands]]

Latest revision as of 17:40, 17 November 2016

    The *temp command is used to create a temporary variable, as opposed to a permanent variable defined in startup.txt using the *create command.

To better understand the main differences between these two types of variables, read the article on variable types.

Temporary variables

As with permanent variables, a temporary variable can also be any one of the three different data types, either numeric (containing a number), a string (containing "text"), or a boolean (either true or false).

In all three cases a new temporary variable is created in the exact same way, simply by assigning it a new and unique name and a default starting value:

*temp var false

In the above example we have effectively created a new temporary variable named var and have assigned a default starting value of false, making it a boolean variable type. We could instead have assigned a number

*temp var 0

making it a numeric variable, or "some text" to make it a string variable:

*temp var "Alex"

In all cases the *set command may then be used - within the same scene file - to modify the initial default value, or we could perhaps make use of it for conditional *if / *else statements. Also, with the *rand command we could immediately after randomize the initial value of any numeric temporary variable.

Temporary Variables really are Temporary!

The most important thing to bear in mind concerning *temp variables is that they are exactly that -- temporary. As soon as your game loads a new scene file as a result of either a *finish or *goto_scene command, all of your temporary variables -- and their values -- are lost from memory. In essence, temporary variables can only be used within the current scene file.

While you can of course create new *temp variables of the same name in the next scene file, those would count as brand new variables and all previously-held values would still have been lost. Where this may cause some continuity problems in either your story or your scripting, those particular variables should instead be defined in startup.txt as permanent variables, using the *create command.

Where & when to use temporary variables

Temporary variables are specifically intended to carry short-term information, and for virtually any purpose. They are typically used to carry conditional data for later use in such as *if, *selectable_if, and perhaps *hide_reuse conditions: if the player met Colonel Mustard; if the player kissed Miss Scarlett; if the player visited the library; if the player saw the bloodstained candlestick . . . or indeed everything and anything that you might need to record for only a short period of time in order to determine how -- or indeed, if at all -- that recent event will impact something else in the near future.

Error: "Non-existent variable [name]"

As discussed in the Wiki article Basic Scene Scripting, "non-existent variable" errors are all too common when first learning ChoiceScript, as it can at first be difficult to grasp the logic behind what precisely causes such an error to occur.

If not a simple misspelling of the variable name (a single letter wrong - or missing - is all it takes) the cause is almost always due to where the *temp command actually appears in your scripting, as explained in the Basic Scene Scripting article.

The simplest solution, and the easiest way of avoiding many such errors, is to always list all *temp commands at the very top of each scene file in which they are intended to be used, with a default starting value appropriate to that data type, and which you would simply *set accordingly later in your story scripting to suit. Examples of default starting values placed at the start of each scene file would be:

*temp var1 0  [a numeric default value of zero]

*temp var2 ""  [a blank string value using double quotes with no content]

*temp var3 false  [a default boolean, which must always be either true or false]

By this means, the variable itself would always exist throughout that scene file and "non-existent variable" type errors will occur far less often.


More commands / functions
Choice *choice, *fake_choice, *disable_reuse, *hide_reuse, *allow_reuse, *selectable_if
Variable *create, *temp, *set, Arithmetic operators, *delete, *input_number, *input_text, *print, *rand
Conditional *if, *elseif, *else, Multireplace
Goto *label, *goto, *goto_scene, *goto_random_scene, *gosub, *gosub_scene, *finish
Formatting Bold text, Italic text, *image, *line_break, *page_break, *link, *stat_chart
Miscellaneous *comment, *scene_list, *title, *author, *achieve, *achievement, *check_achievements, *bug, *ending, *more_games, *share_this_game, *show_password, *script, Implicit Control Flow