Difference between revisions of "String Variables"

Jump to navigation Jump to search
2,234 bytes added ,  12:55, 30 November 2014
Reorganized and added details
imported>QQuix
m (QQuix moved page "String Variables" to String Variables: Removing quotes from page title)
imported>QQuix
(Reorganized and added details)
 
Line 1: Line 1:
__NOTOC__
A variable type added by [[:Category:Oblivion Script Extender|Oblivion Script Extender (OBSE)]].
A variable type added by [[:Category:Oblivion Script Extender|Oblivion Script Extender (OBSE)]].


Line 19: Line 21:
Similarly, string variables should only be used to store strings, and the result of a string-returning variable should only (and always) be assigned to a string variable.  
Similarly, string variables should only be used to store strings, and the result of a string-returning variable should only (and always) be assigned to a string variable.  


Note that direct assignment of one string variable to another causes both variables to refer to the same string. For instance, in the following code:


<pre>
==Creating and using string variables==
string_var string1
 
string_var string2
Strings are created when you assign a string to a variable of type 'string_var'.
 
string_var string1
Let string1 = "Hello World" 
 
 
String variables, actually, hold an internal ID of the string. The string itself is stored somewhere else in memory and is internally connected to the ID stored in the string_var. Understanding this structure may help in some situations.
 
 
Using LET to assign a string to a non-initialized string_var creates a new string in memory
  Let string1 := “Hello”
      • string1 points to a newly created  “Hello” string
 
Using LET to assign a string to an initialized string_var replaces the original string with the new string
  Let string1 := “World”
      • Destroys the original “Hello” string
      • Creates a “World” string
      • string1 points to the “World” string


set string1 to sv_Construct "First string"
Using LET to assign a String_var to another string_var creates a new copy of the string
set string2 to string1
  Let string2 := string1
set string1 to sv_Construct "Second string" ; modifies both string1 and string2
      • Creates a new “World” string
</pre>
      • string2 points to the newly created  “World” string
      • string1 still points to the same “World” string as before
      • string1 and string2 may be changed without affecting each other (as they are different strings)


both string1 and string2 end up containing "Second string." If this is not desired behavior, use sv_Construct to copy the contents of one string to another, i.e.:
===Concatenating strings===
Strings may be concatenated using the '+' operator.


<pre>
You may use any combination of string_vars and literals
set string2 to sv_Construct "%z" string1 ; copies string1's contents to string2
set string2 to player.GetName ; modifies only string2
</pre>


Let string1 = "Hello"
Let string2 = "World" 
let string3 = string1 + " Wide " + string2




As of OBSE v0017, string variables can be passed to any command expecting a string literal as an argument by prefacing the name of the variable with a dollar sign.
===Using string vars in place of literals===


This deprecates many of the Set...EX commands.  
String variables can be passed to any command expecting a string literal as an argument by prefacing the name of the variable with a dollar sign.  


The variable can be a quest variable, specified as $quest.varname, or local to the calling script specified as $varname. Example:
The variable can be a quest variable, specified as $quest.varname, or local to the calling script specified as $varname. Example:
Line 52: Line 75:
MessageBox $msg
MessageBox $msg
</pre>
</pre>
This deprecates many of the Set...EX commands.


Additionally in v0017, support for string operations has been integrated into the language via OBSE expressions, which leaves functions such as sv_Construct, sv_Substring, etc. mostly deprecated.
Additionally in v0017, support for string operations has been integrated into the language via OBSE expressions, which leaves functions such as sv_Construct, sv_Substring, etc. mostly deprecated.
===Warning: using SET with string_vars===
Using SET to assign a String_var to another string_var copies the ID from one var to the other causing both variables to refer to the same string. For instance, in the following code:
string_var string1
string_var string2
set string1 to sv_Construct "First string"
set string2 to string1
set string1 to sv_Construct "Second string" ; modifies both string1 and string2
both string1 and string2 end up containing "Second string." If this is not desired behavior, use LET to copy the contents of one string to another.
The problem is that SET was developed before strings were implemented, so it does not handle them well, so it is not prepared to destroy the previous string pointed by the string2.
Like in this example:
  Let string1 := “Hello”
  Let string2 := “World”
  Set string2 to string1
      • DOES NOT destroy the original “World” string originally pointed by string2 >> BLOAT
That “World” will stay in the savegame, unused and unreachable, until the mod is removed.
So, if you need to have two variables pointing to the same string, make sure you explicitly destroy the original string before assigning a new string with LET, like this:
  Let string1 := “Hello”
  Let string2 := “World”
  sv_destruct string2
  Set string2 to string1


==Destroying string variables==
==Destroying string variables==
String variables persist in the savegame until they are explicitly destroyed or until the mod from which they originate is removed from the user's mod list.  
String variables persist in the savegame until they are explicitly destroyed or until the mod from which they originate is removed from the user's mod list.  


Anonymous user

Navigation menu