-
Notifications
You must be signed in to change notification settings - Fork 4
Strings
All text in Angelscript, and computing in general, is represented by strings. In Angelscript, the string class is what we use for that. This class uses an interface that differs from the one provided by the default Angelscript add-on, so you cannot rely on that for Sven Co-op.
Most of the string documentation is fairly straightforward, so this documentation will only cover a few items.
string_t is a special string type used to refer to strings allocated from the game's string pool. This type will automatically convert to and from the string type when needed, but you may sometimes have to manually convert it using value type conversion syntax:
// Convert to a string:
string szString = string( string_t_value );
string_t szString_t = string_t( "a string" );
One of the features available in our API is a printf style formatting system. This allows you to provide a single string that contains placeholders where a varying number of arguments can be placed in them.
The standalone snprintf function allows you to format input into a specified output string:
string szResult;
if ( snprintf( szResult, "this %1 a %2 string\n", "is", "format" ) )
{
g_Game.AlertMessage( at_console, szResult );
}
The syntax is fairly simple: every % symbol is considered a placeholder, with a number, starting at 1 for the first argument, referring to the argument that was passed in. If you want to use % itself, simply use %%. You can use the same argument multiple times, and it is not required to use each argument. Using a non-existent argument triggers a warning and causes formatting to fail.
You can pass in arguments that are primitives (boolean, integer, floating point) or strings (including string_t). Objects are output as memory addresses. Enums are also supported.
All functions that support the use of this formatting note this in their documentation. It is also possible to use this simply to copy strings by not specifying any placeholders, but that should be avoided due to the increased overhead in parsing the string.