Difference between revisions of "String Formatting"
imported>Odessa m (→FOSE and NVSE: linked to scan codes) |
imported>Odessa (added note on nvse alternative) |
||
Line 101: | Line 101: | ||
</pre> | </pre> | ||
==NVSE Alternative To Formatting== | |||
As an alternative to string formatting, with NVSE functions such as [[Print]], you can use string concatanation and the [[ToString|ToString($)]] function. | |||
<pre> | |||
int iVal | |||
ref rActor | |||
PrintToConsole "Value of iVal is %g and name of rActor is %n" iVal, rActor | |||
; can instead be written as: | |||
Print "Value of iVal is " + $iVal + " and name of rActor is " + $rActor | |||
</pre> | |||
See the [[Tutorial: String Variables|tutorial on string variables]] for more information. | |||
==See Also== | ==See Also== | ||
*[[ShowMessage]] | *[[ShowMessage]] | ||
*[[MessageEx]], [[MessageBoxEx]] | *[[MessageEx]], [[MessageBoxEx]] | ||
*[[PrintToConsole]], [[DebugPrint]] | *[[PrintToConsole]], [[DebugPrint]] | ||
*[[String Variable]], [[Print]] | |||
[[Category:NVSE]] | [[Category:NVSE]] | ||
[[Category:String Variables]] | [[Category:String Variables]] |
Revision as of 11:04, 20 July 2014
Some commands take a format string as a parameter. Format strings are actually a collection of arguments consisting of a string followed by zero to twenty variables. The string specifies how the command should use the rest of the arguments to construct a new string. Within the format string, percent signs are used to indicate special characters. The number of format specifiers in the string must match exactly the provided arguments
Vanilla Game Options
%X.Yf - This formats a number with minimum width X and maximum decimal points Y, replacing X and Y with that desired, for example:
%.2f - This means format the variable with 2 decimal places.
%.0f - This will format the variable with 0 decimal places, so is the normal choice for integers.
%5.0f - The number in front of the point specifies the minimum width of the number. In this case, there will always be enough space in front of the number for 5 digits:
Number 12 wins Number 1234 wins
%g - This usually works just like "%.0f", displaying 0 decimal places. When the number is 1000000 or larger, though, the game diplays it in scientific notation (1E+006)
%.3e - Shows numbers in scientific notation (123000 = 1.23E+005)
%% - Use this to display a percent-sign in the string
&sUActn... - Allows you to insert the key used for a given control, such as the Activate button, the Always Run button, or the Block button. See the sUActn article for the list of control codes.
Example
int i float f set i to 42 set f to 3.1415926535 "i = %g and f = %.2f" i, f ; formats to: "i = 42 and f = 3.14"
Formatting switches
The following formatting switches can be used in strings. Put them in any sequence right after the '%'
Switch | Function |
---|---|
+ | Display + in front of positive numbers |
<Space> | Leave a leading space in front of positive numbers |
- | Use left-aligned formation instead of right alligned. |
0 | The filling-char used for formatting is '0' instead of ' ' |
FOSE and NVSE
%a - Prints the character corresponding to the specified ASCII code. Passing codes for unprintable characters (such as 0) may have unpredictable (though occasionally useful) results. Passing the code for a percent sign will most likely crash the game as literal percent signs must come in pairs.
%B - toggles blue text on for console output %b - toggles blue text off for console output. e.g. "%BBlue%b suede shoes": when printed to the console, the word "Blue" will be printed in blue text.
%c - Prints the specified component of the specified reference or object. Takes two arguments - a reference variable set to the spell or faction, and an index. Behaves differently depending on the passed reference:
- Faction: Prints the nth male rank title
- Magic Item: Prints the nth Magic Effect
%e - Provides a workaround for the script compiler's refusal to accept an empty string as a command argument.
- Example:
SetName "" object ; attempts to remove an object's name, but won't compile SetName "%e" object ; sets the name to an empty string
%i - Prints the formID of the specified reference or object. GOTCHA : if a refVar is passed which points to a ref not loaded in memory %i will print 00000000 even if the refVar is not empty.
%k - Prints the name of the key for the specified DirectInput scancode.
%n - Prints the name of the specified reference or object.
%p - Displays a pronoun based on the gender of the object parameter:
- %po - objective (him, her, it)
- %pp - possessive (his, her, its)
- %ps - subjective (he, she, it)
%q - Prints a double quote character.
%r - Prints a carriage return, ending the current line and starting at the next.
%v - Prints the actor value (i.e. an attribute or skill) associated with the passed actor value code.
%x - replaced with an integer in hexadecimal format. An optional digit from 0-9 immediately following this specifier indicates the minimum width of the displayed value. For example, MessageEx "%x4" 255 will display "00FF".
%z - replaced by the contents of a String Variable (NVSE only)
%{ .. %} - Conditionally omits a portion of the format string based on a boolean evaluation of argument. The left bracket accepts a variable; if the value of the variable is zero, all text up to the right bracket will be ignored, and any parameters supplied to format specifiers within the omitted substring will be skipped.
Note that the %e, %q, and %r specifiers can be used within any string literal and will be replaced by an empty string, a double quote, or a carriage return respectively.
Example
("Name is %n" SunnyREF) == "Name is Sunny Smiles"
NVSE Alternative To Formatting
As an alternative to string formatting, with NVSE functions such as Print, you can use string concatanation and the ToString($) function.
int iVal ref rActor PrintToConsole "Value of iVal is %g and name of rActor is %n" iVal, rActor ; can instead be written as: Print "Value of iVal is " + $iVal + " and name of rActor is " + $rActor
See the tutorial on string variables for more information.