Skip to main content
Utility functions provide essential tools for string manipulation, type conversion, cryptography, time handling, and system operations.

String Manipulation

strRepl

Finds all occurrences of a search string and replaces them with a replacement string.
result = strRepl(mainString, search, replacement)
mainString
string
required
The string to search in
The substring to find
replacement
string
required
The string to replace matches with
result
string
Returns a new string with all replacements made. The original string is not modified.
Examples
text = "Hello World";
result = strRepl(text, "World", "CoD4");
// result = "Hello CoD4"

path = "/path/to/file";
result = strRepl(path, "/", "\\");
// result = "\\path\\to\\file"

strColorStrip

Removes all CoD4 color codes from a string.
result = strColorStrip(text)
text
string
required
The string containing color codes (e.g., ^1, ^2, etc.)
result
string
Returns the string with all color codes normalized to ^7 (white)
Example
coloredText = "^1Red ^2Green ^3Yellow";
plainText = strColorStrip(coloredText);
// plainText = "^7Red ^7Green ^7Yellow"

strTokByLen

Tokenizes a string into an array based on character length, attempting to preserve complete words.
array = strTokByLen(text, maxLength)
text
string
required
The string to tokenize
maxLength
int
required
The maximum character count per token
array
string[]
Returns an array of string tokens, each preserving color codes
This function intelligently breaks strings at word boundaries when possible and preserves color codes across tokens.
Example: Breaking long text
longText = "This is a very long message that needs to be split";
tokens = strTokByLen(longText, 20);

for (i = 0; i < tokens.size; i++) {
    iPrintLn(tokens[i]);
}
// Output:
// "This is a very long"
// "message that needs"
// "to be split"

toUpper

Converts a string to uppercase.
result = toUpper(text)
text
string
required
The string to convert
result
string
Returns the string in uppercase
Example
name = "john";
upper = toUpper(name);
// upper = "JOHN"

toTitle

Converts a string to title case (first letter of each word capitalized).
result = toTitle(text)
text
string
required
The string to convert
result
string
Returns the string in title case
Example
text = "call of duty 4";
title = toTitle(text);
// title = "Call Of Duty 4"

Type Conversion

toFloat

Converts a string, integer, or float to a float value.
result = toFloat(value)
value
string | int | float
required
The value to convert to float
result
float | int
Returns the value as a float. If the input is an integer, returns it as-is (CoD4 will cast automatically when needed).
Examples
str = "3.14";
floatVal = toFloat(str);
// floatVal = 3.14

intVal = 42;
floatVal = toFloat(intVal);
// floatVal = 42

floatVal = toFloat("123.456");
// floatVal = 123.456

isArray

Checks if a variable is an array.
result = isArray(variable)
variable
any
required
The variable to check
result
bool
Returns true if the variable is an array, false otherwise
Example
data = [];
if (isArray(data)) {
    data[0] = "value";
}

str = "text";
if (!isArray(str)) {
    iPrintLn("Not an array: " + str);
}

Cryptography

sha256

Calculates the SHA-256 hash of an input string.
hash = sha256(input)
input
string
required
The string to hash
hash
string
Returns the SHA-256 hash as a hexadecimal string (64 characters)
Example: Password hashing
password = "secret123";
hash = sha256(password);
iPrintLn("Hash: " + hash);
// Hash: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b

// Verify password
inputPassword = "secret123";
inputHash = sha256(inputPassword);
if (inputHash == hash) {
    iPrintLn("Password correct!");
}

Time Functions

getEpochTime

Returns the current Unix timestamp (seconds since January 1, 1970 UTC).
timestamp = getEpochTime()
timestamp
int
Returns the current Unix timestamp in seconds
Example
startTime = getEpochTime();
// ... do something ...
endTime = getEpochTime();
elapsed = endTime - startTime;
iPrintLn("Operation took " + elapsed + " seconds");

epochTimeToString

Converts a Unix timestamp to a formatted date/time string.
formatted = epochTimeToString(timestamp, timezone, format)
timestamp
int
required
The Unix timestamp to convert
timezone
int
required
  • 0 = UTC timezone
  • 1 = Local timezone
format
string
required
The format string (uses C strftime format specifiers)
formatted
string
Returns the formatted date/time string
Format specifiers:
  • %Y = Year (4 digits)
  • %m = Month (01-12)
  • %d = Day (01-31)
  • %H = Hour (00-23)
  • %M = Minute (00-59)
  • %S = Second (00-59)
  • %A = Full weekday name
  • %B = Full month name
See strftime documentation for all format codes.
Examples
time = getEpochTime();

// UTC time
utcStr = epochTimeToString(time, 0, "%Y-%m-%d %H:%M:%S");
iPrintLn("UTC: " + utcStr);
// UTC: 2026-03-03 14:30:45

// Local time
localStr = epochTimeToString(time, 1, "%A, %B %d, %Y at %H:%M");
iPrintLn("Local: " + localStr);
// Local: Tuesday, March 03, 2026 at 09:30

// Custom format
custom = epochTimeToString(time, 0, "%Y/%m/%d");
iPrintLn("Date: " + custom);
// Date: 2026/03/03

System Operations

system

Executes a system command on the server.
system(command)
command
string
required
The system command to execute
This function executes arbitrary system commands and can be dangerous. Only use it with trusted input and ensure your server runs with minimal privileges. This function does not return the command output.
Example: Creating a backup
system("cp -r main backup_" + getTime());
The system() function does not return command output. Use with caution in production environments.

Configuration Variables (CVars)

getCvar

Gets the string value of a server console variable (cvar).
value = getCvar(cvarName)
cvarName
string
required
The name of the cvar to retrieve
value
string
Returns the cvar value as a string
Example
mapName = getCvar("mapname");
maxClients = getCvar("sv_maxclients");

getCvarInt

Gets the integer value of a server console variable.
value = getCvarInt(cvarName)
cvarName
string
required
The name of the cvar to retrieve
value
int
Returns the cvar value as an integer
Example
maxClients = getCvarInt("sv_maxclients");

getCvarFloat

Gets the floating-point value of a server console variable.
value = getCvarFloat(cvarName)
cvarName
string
required
The name of the cvar to retrieve
value
float
Returns the cvar value as a float
Example
gravity = getCvarFloat("g_gravity");

setCvar

Sets the value of a server console variable.
setCvar(cvarName, value)
cvarName
string
required
The name of the cvar to set
value
string
required
The new value for the cvar
Example
setCvar("g_gametype", "dm");
setCvar("sv_hostname", "My CoD4 Server");
Changes to some cvars may require a map restart to take effect.

Vector Operations

vectorAdd

Adds two 3D vectors together.
result = vectorAdd(vector1, vector2)
vector1
vector
required
The first vector (x, y, z)
vector2
vector
required
The second vector to add
result
vector
Returns the sum of the two vectors
Example
pos1 = (100, 200, 300);
offset = (10, 20, 30);
newPos = vectorAdd(pos1, offset);
// newPos = (110, 220, 330)

vectorScale

Multiplies a vector by a scalar value.
result = vectorScale(vector, scalar)
vector
vector
required
The vector to scale
scalar
float
required
The scalar multiplier
result
vector
Returns the scaled vector
Example
direction = (1, 0, 0);
scaled = vectorScale(direction, 100);
// scaled = (100, 0, 0)

// Double a position vector
pos = (50, 75, 100);
doubled = vectorScale(pos, 2);
// doubled = (100, 150, 200)

Best Practices

Validate Input

Always validate and sanitize user input before using it with system() or file operations.

Use Type Checking

Use isArray() and other type checkers to prevent runtime errors.

Format Timestamps

Use epochTimeToString() for user-friendly date displays instead of raw timestamps.

Hash Sensitive Data

Use sha256() to hash passwords and sensitive data before storing or comparing.

Complete Example

Example: Player session logging system
logPlayerSession(player) {
    // Get current time
    timestamp = getEpochTime();
    dateStr = epochTimeToString(timestamp, 0, "%Y-%m-%d %H:%M:%S");
    
    // Clean player name
    cleanName = strColorStrip(player.name);
    cleanName = strRepl(cleanName, "^", "");
    cleanName = strRepl(cleanName, "7", "");
    
    // Generate session ID from player GUID and timestamp
    sessionData = player getGuid() + ":" + timestamp;
    sessionId = sha256(sessionData);
    
    // Create log entry
    logEntry = dateStr + " | ";
    logEntry += cleanName + " | ";
    logEntry += player getIp() + " | ";
    logEntry += "Session: " + sessionId;
    
    // Write to log file
    fh = fsOpen("sessions.log", "append");
    if (fh) {
        fsWriteLine(fh, logEntry);
        fsClose(fh);
    }
    
    return sessionId;
}

formatPlayerStats(player) {
    // Convert stats to proper formats
    kdr = 0.0;
    if (player.deaths > 0) {
        kdr = toFloat(player.kills) / toFloat(player.deaths);
    }
    
    // Format player name
    displayName = toTitle(strColorStrip(player.name));
    
    // Create formatted stats string
    stats = "";
    stats += "Player: " + displayName;
    stats += " | K/D: " + kdr;
    stats += " | Score: " + player.score;
    
    return stats;
}

processChatMessage(player, message) {
    // Replace profanity
    clean = strRepl(message, "badword", "***");
    
    // Convert commands to uppercase for processing
    if (isSubStr(clean, "!")) {
        tokens = strTok(clean, " ");
        if (tokens.size > 0) {
            command = toUpper(tokens[0]);
            
            if (command == "!TIME") {
                time = getEpochTime();
                timeStr = epochTimeToString(time, 1, "%H:%M:%S");
                player iPrintLn("Server time: " + timeStr);
                return;
            }
        }
    }
    
    // Broadcast cleaned message
    cleanName = strColorStrip(player.name);
    iPrintLn(cleanName + ": " + clean);
}

Build docs developers (and LLMs) love