Format Class Functions (SourceMod Scripting)
Contents
Introduction
Format-class functions are variable argument functions in SourceMod which allow you to format a string. A simple example of this is the Format() function, which looks like:
decl String:buffer[512]; Format(buffer, sizeof(buffer), "Your name is: %s", userName);
If userName contains "Mark," the contents of buffer will then be: "Your name is: Mark." The prototype of these functions almost always contains the following parameters:
const String:fmt[], {Handle,Float,_}:...
For example, observe the following two natives:
native Format(String:buffer[], maxlength, const String:fmt[], {Handle,Float,_}:...); native PrintToClient(client, String:fmt[], {Handle,Float,_}:...);
Thus, PrintToClient is a format-class function. It can be used exactly as shown earlier:
PrintToClient(client, "Your name is: %s", userName);
Format Specifiers
A format specifier is a code that allows you to specify what data-type to print. The most common specifiers are:
- Numerical
- d or i: Integer number as decimal
- b: Binary digits in the value
- f: Floating-point number
- x or X: Hexadecimal representation of the binary value (capitalization affects hex letter casing)
- Character(s)
- s: String
- t or T: Translates a phrase (explained in Translations)
- c: Prints one character (UTF-8 compliant)
- Special
- L: Requires a client index; expands to 1<2><3><> where 1 is the player's name, 2 is the player's userid, and 3 is the player's Steam ID. If the client index is 0, the string will be: Console<0><Console><Console>
- N: Requires a client index; expands to a string containing the player's name. If the client index is 0, the string will be: Console
Usage
Format specifiers are denoted with a '%s' symbol. For example, to print a float, a number, and a string, you might use this code:
new Float:fNum = 5.0; new iNum = 5 new String:str[] = "5" PrintToClient(client, "Number: %d Float: %f String: %s", iNum, fNum, str);
Note: Using the wrong data type with a specifier can be very dangerous. Always make sure you are printing as the right type. For example, specifying a string and passing a number can crash the server.
Advanced Formatting
Format specifiers have an extended syntax for controlling various aspects of how data is printed. The full syntax is: %[flags][width][.precision]specifier
Each bracketed section is an optional extension. Explanations of supported SourceMod format extensions:
- %: Obviously, this is always required.
- flags:
- -: Left-justify (right-justify is set by default)
- 0: Pads with 0s instead of spaces when needed (see width below).
- width: Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
- precision:
- For integers: specifies the minimum number of digits to print (or pad with spaces/zeroes if below the minimum).
- For strings: specifies the maximum number of characters to print.
- For floats: specifies the number of digits to be printed after the decimal point.
- For all other types: no effect.
- specifier: character specifying the data type (always required).
todo: examples
For more information, see printf from the C Standard Library, although not all modes are supported from C.
Making your function Format-Class
Sourcemod allows you to make your function Format-class, ie. pass parameters to format string variables. Here's an exemple :
public formatExemple(const String:myString[] , any:...) { new String:myFormatedString[strlen(myString)+255]; VFormat(myFormatedString, sizeof(myFormatedString), myString, 2); PrintToServer(myFormatedString); }
Using the parameter "any: ...", we can pass data(s) to format our string. Now, in order to replace the Format Specifiers by our data(s), we use the API "VFormat", which documentation can be found here : [1].
The three first parameters passed in VFormat are pretty obvious since they are the as in the Format(..) API.
The 4th parameter indicate the "any: ..." parameter position in your function prototype.