Skip to content

Utilities

This page documents miscellaneous functions that don't belong to any library.

getVersion function

Returns the current version of Ratchet.

Syntax:

lua
string getVersion()

getSecret function

Reads a secret from ratchet/secrets.txt to avoid storing passwords or tokens in plugin code. Empty string will be returned if a secret by the specified key is not found.

Syntax:

lua
string getSecret( string key )

isServerLoaded function

Checks whether the server is fully loaded (mods and plugins are loaded, players can connect). This is mostly useful for a custom REST API to check whether the server is still starting.

Syntax:

lua
bool isServerLoaded()

getType function

Returns the name of an object type for a specific object.

Syntax:

lua
string getType( mixed object )

base64Encode function

Syntax:

lua
string base64Encode( string input )

base64Decode function

Syntax:

lua
string base64Decode( string input )

friendlyTime function

Converts number of seconds into human-friendly and readable format.

Syntax:

lua
string friendlyTime( int seconds )

Example:

lua
local time = friendlyTime(3600)
print(time) -- "1 hour"

getRealTime function

Retrieves a table of various useful values regarding current real time.

Syntax:

lua
table getRealTime( [ bool localTimezone = false ] )
FieldTypeRangeDescription
timestampnumber-Unix epoch in seconds
hournumber0-23Hour (24-hour format)
minutenumber0-59Minute
secondnumber0-60Second (60 for leap seconds)
yearnumber-Full year (e.g. 2025)
monthnumber1-12Month
daynumber1-31Day of month
weekdaynumber0-6Day of week (0=Sunday)
weekdayisonumber1-7ISO weekday (1=Monday, 7=Sunday)
yeardaynumber1-366Day of year
isdstboolean-Daylight saving time
datestring-ISO 8601 date (YYYY-MM-DD)
timestring-ISO 8601 time (HH:MM:SS)
datetimestring-ISO 8601 datetime (YYYY-MM-DDTHH:MM:SS)
hour12number1-12Hour (12-hour format)
ampmstring-"AM" or "PM"

Example:

lua
print(getRealTime().time) -- "18:04:23"

traceLine function

Trace a line between two points, detecting blocking object.

Syntax:

lua
bool, HitResult traceLine( Vector origin, Vector destination [, int|table<string> filter = 0, bool complex = false, table<Actor> ignoreList = {} ] )

Filter can be either channel index from the table below or a table of strings representing object types from the table even further below.

IndexChannel
0Visibility
1Camera
2Trace_BuildingStructure
3Trace_ProjectilePrecision
4Trace_BuildingGhost
5SandStormMonsterSpawn
6Trace_Interactable
7DamagePrevention_BlockNothing

Object Types

WorldStatic
WorldDynamic
Pawn
PhysicsBody
Vehicle
Destructible
Attack
Defense
Hitbox
Building_Structure
Projectile
WaterPhysics
Resource
Sandstorm
CollisionTrigger
Hazard
AudioTrigger
LootBag

printDebug function

Same as the native print function, except for prepending plugin name and highlighting the message with gray color in the Ratchet console. This message will also be saved to the ratchet.log file located in the server's folder.

Syntax:

lua
void printDebug( [ string input, ... ] )

printInfo function

Same as the native print function, except for prepending plugin name and highlighting the message with blue color in the Ratchet console. This message will also be saved to the ratchet.log file located in the server's folder.

Syntax:

lua
void printInfo( [ string input, ... ] )

printSuccess function

Same as the native print function, except for prepending plugin name and highlighting the message with green color in the Ratchet console. This message will also be saved to the ratchet.log file located in the server's folder.

Syntax:

lua
void printSuccess( [ string input, ... ] )

printWarning function

Same as the native print function, except for prepending plugin name and highlighting the message with gold color in the Ratchet console. This message will also be saved to the ratchet.log file located in the server's folder.

Syntax:

lua
void printWarning( [ string input, ... ] )

printError function

Same as the native print function, except for prepending plugin name and highlighting the message with red color in the Ratchet console. This message will also be saved to the ratchet.log file located in the server's folder.

Syntax:

lua
void printError( [ string input, ... ] )

dumpAllObjects function

Writes a list of all persistent actors and all their actor components into a ratchet/dump.txt file. This is potentially heavy operation and mostly only useful for advanced debugging.

Syntax:

lua
void dumpAllObjects()

defer function

Defer execution of a function to one of the next ticks, giving it a very tiny delay (~50ms on default config). This also ensures that no two deferred functions are executed at the same time (they are executed in order as they were requested).

You can use this function to improve performance of heavy operations, by atomizing them into multiple functions that are chain-linked with defer.

Syntax:

lua
void defer( function func [, ... ] )

batch function

Execute multiple game thread operations in bulk without blocking Lua thread with round-trips. For example, changing 20 RPR stats at once without batch can take ~100ms, while with batch it might only take ~5ms.

This might be one of the most useful functions when it comes to improving performance of your heavy plugins, as long as you know what you're doing. Keep in mind that this is a double-edged sword and can also cause issues by overloading the game thread tick.

Only use this for writing/executing, never read data or expect a return. All operations within batch function are executed in bulk at the same time. Without batch, every function influencing game thread is called in order and waiting to be fully executed before allowing Lua code to progress further, this is called a round-trip.

Syntax:

lua
void batch( function func )

Example:

lua
batch(function()
    RPR.ModifyStat(player, "Stat1", 1)
    RPR.ModifyStat(player, "Stat2", 1)
    RPR.ModifyStat(player, "Stat3", 1)
    -- many more calls, all executing at once
end)
-- all stats are set by here, back to synchronous code, safe to read