Format Class Functions (SourceMod Scripting)

From AlliedModders Wiki
Revision as of 10:29, 29 December 2015 by Xerox8521 (talk | contribs) (Introduction)
Jump to: navigation, search

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:

char 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 char[] fmt, {Handle,Float,_} ...

For example, observe the following two natives:

native void Format(char[] buffer, int maxlength, const char[] fmt, {Handle,Float,_} ...);
native void PrintToClient(int client, char[] 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
    • u: Unsigned 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 example :

public formatExample(const String:myString[] , any:...)
{
	new String:myFormattedString[strlen(myString)+255];
	VFormat(myFormattedString, sizeof(myFormattedString), myString, 2);
 
	PrintToServer(myFormattedString);
}

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.