string

From NWN Lexicon
Jump to navigationJump to search

A sequence of characters of arbitrary length. The default value is an empty string (""). Strings can be concatenated (that is, combined) by using the plus ("+") sign. A string can be compared for equality against another string by using the double equals ("==") equality tester, or for inequality by using the "!=" equality tester. When testing for string equality, strings are case-sensitive (that is "SoME CHaRaCTeR NaMe" does not equal "some character name").

Note a single defined string can have up to 511 characters before the compiler stops working. To get around this limitation you can use concatenated strings or pull the strings from other sources outside the script (anything that returns a string value won't have this limit imposed).

To make static strings in larger scripts more efficient at runtime consider using the const keyword. Strings tend to be the worst performance-wise for large loops and complicated manipulations.

There are a number of String Functions that can help manipulate, test, and alter strings.

Example

string sHero = "Galryx";

string sConcat = "Hello" + " there!"; // "Hello there!"

string sName = GetName(GetFirstPC()); // Gets the name of the first PC and stores it in sName

Escaped Characters

Some characters may be preceded by a backslash (\) to generate special characters. This is called escaping the character. For example, \t results in a tab character and \n results in a newline character.


Warning icon orange.png Caution: This section documents changes arising in NWN: Enhanced Edition content. The content is available in both the classic version and the Enhanced Edition of the game. However, the behavior may vary in each version. More details may be available in the NWN Enhanced Edition patch notes.

As of NWN:EE version 1.74.8168, strings may now contain double-quote characters if the double-quote is escaped using a backslash (\"). For example:

void main()
{
    // The NPC says: So I respond, "That's what she said!"
    SpeakString("So I respond, \"That's what she said!\"");
}

As of NWN:EE version 1.85.8193.31, the sript compiler supports \xFF style escape sequences to put arbitrary bytes into a string literal. NB: \x00 will terminate the string, do not use.

This can be used to generate more colour tokens then you'd usually be able to using nwscript, since the compiler would not like some of the extended range of ASCII. Of course the non-printable characters do not show up if used in a string you print to view.

This function can now show almost any colour text, except entirely black since \x00 is not available.

// COLOR_TOKEN originally by rdjparadis. Used to generate colors from RGB values. NEVER modify this string.
// This requires a later version of NWN:EE to use the \x escape character
// For more information: https://nwn.wiki/display/NWN1/Colour+Tokens
// NB: First character is "nearest to 00" since we can't use \x00 itself (means you can't get completely black is all)
const string COLOR_TOKEN = "\x01\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF";

// Gets a suitable <cXXX> token to use at the start of a block of colored text. Must be terminated by </c>
// - nRed - Red amount (0-255)
// - nGreen - Green amount (0-255)
// - nBlue - Blue amount (0-255)
string GetColorCode(int nRed=255, int nGreen=255, int nBlue=255)
{
    return "<c" + GetSubString(COLOR_TOKEN, nRed, 1) + GetSubString(COLOR_TOKEN, nGreen, 1) + GetSubString(COLOR_TOKEN, nBlue, 1) + ">";
}

void main()
{
    SpeakString(GetColorCode(255, 20, 147) + "Speaking in Deep Pink!" + "</c>");
}

See Also

functions:

String Functions

constants:

EFFECT_TYPE_* Constants