Module plugin_base
SparkMod shared plugin base.
These functions are available to both server and client-side plugins
Functions
| Format (format [, ...]) | Alias for string.format, functionality is extended for server plugins |
| Puts (format, ...) | Prints a string to the server console prepended with the plugins name |
| GetTime () | Gets the number of seconds since the server started or changed map. |
| GetSystemTime () | Gets the current system time in seconds |
| Persistent (tbl) | Declares variables that will persist between map changes, plugin reloads and server restarts. |
| Store ( [name]) | Saves persistent variables to storage |
| Restore ( [name]) | Restores persistent variables from storage |
| Requires (...) | Requires other plugins to be loaded before initializing this plugin. |
| RegPluginLibrary (name) | Registers a global name that can be used to reference this plugin from other plugins. |
| NetworkMessageField (message_name, name, type, default_value) | Registers a network message field, creates the network message if it does not already exist |
| NetworkMessage (message_name, func) | Registers or extends a network message. |
| HookFunctionPre (method_name, callback) | Pre-hooks a function and calls the callback every time the function is about to be called. |
| HookFunction (method_name, callback) | Post-hooks a function and calls the callback every time the function has been called. |
| HookGamerulesFunctionPre (method_name, callback) | Pre-hooks a Gamerules function and calls the callback every time the function is about to be called. |
| HookGamerulesFunction (method_name, callback) | Post-hooks a Gamerules function and calls the callback every time the function has been called. |
| HookNS2GamerulesFunctionPre (method_name, callback) | Pre-hooks a NS2Gamerules function and calls the callback every time the function is about to be called. |
| HookNS2GamerulesFunction (method_name, callback) | Post-hooks a NS2Gamerules function and calls the callback every time the function has been called. |
| InjectIntoScope (..., function) | Injects a function into the scope of other functions so that you can use locals they reference. |
| HookNetworkMessage (message_name, callback) | Hooks a network message and calls the callback when the network message is received |
| SendNetworkMessage ( [target], message_name, message) | Sends a network message to one or all connected players. |
| RespondToNetworkMessage (message_name, callback) | Hooks a network message and responds with the same message. |
Fields
| SM | Shortcut to reference SparkMod core |
| self | Reference to this plugin |
Functions
- Format (format [, ...])
-
Alias for string.format, functionality is extended for server plugins
Parameters:
- format string the string to format
- ... any number of arguments to be formatted
- Puts (format, ...)
-
Prints a string to the server console prepended with the plugins name
Parameters:
- format string the string to format
- ... any number of arguments to be formatted
- GetTime ()
-
Gets the number of seconds since the server started or changed map.
The value returned is a high precision float.
Returns:
-
number
the current system timestamp in seconds
- GetSystemTime ()
-
Gets the current system time in seconds
Returns:
-
number
the current system timestamp in seconds
- Persistent (tbl)
-
Declares variables that will persist between map changes, plugin reloads and server restarts.
Persistent variables are saved to a file when the plugin is unloaded or if Store() is called.
Parameters:
- tbl tab a table containing all variables you want to persist
Usage:
Persistent { recent_map = { } } - Store ( [name])
-
Saves persistent variables to storage
Parameters:
- name string a variables name if you only want to save a single persistent variable
- Restore ( [name])
-
Restores persistent variables from storage
Parameters:
- name string a variables name if you only want to restore a single persistent variable
- Requires (...)
-
Requires other plugins to be loaded before initializing this plugin.
Must be used in the plugin body. In the case of circular dependencies, plugins will be loaded before
OnLoaded is called in this plugin but OnLoaded may not have yet been called in the dependent plugins.
Parameters:
- ... any number of other plugins names
Usage:
Requires "map_vote" Requires("map_vote", "rtv")
- RegPluginLibrary (name)
-
Registers a global name that can be used to reference this plugin from other plugins.
Must be used in the plugin body.
Parameters:
- name string the name of the global variable to be used to refer to your plugin
Usage:
RegPluginLibrary("YourPluginName") -- You can also set library_name library_name = "YourPluginName"
- NetworkMessageField (message_name, name, type, default_value)
-
Registers a network message field, creates the network message if it does not already exist
Parameters:
- NetworkMessage (message_name, func)
-
Registers or extends a network message.
The default_value is used when sending a message missing the field,
it will be used even if the server plugin is unloaded at runtime
Parameters:
- message_name string
- func func an inline function
Usage:
NetworkMessage("SM_SetKv", function() Field("key", "string (64)", "") Field("value", "string (128)", "") end)
- HookFunctionPre (method_name, callback)
-
Pre-hooks a function and calls the callback every time the function is about to be called.
Varargs are passed to the callback as a table. Other plugins can use Pre/post hook forwards to hook this kind of hook.
Parameters:
- method_name string the name of the function to be hooked
- callback func the callback which will be called before the hooked function is called
Usage:
HookFunctionPre("Server.DestroyEntity", function(entity) Puts("Entity is being destoyed: %s", entity:GetClassName()) end)
- HookFunction (method_name, callback)
-
Post-hooks a function and calls the callback every time the function has been called.
Varargs are passed to the callback as a table. Other plugins can use Pre/post hook forwards to hook this kind of hook.
Parameters:
- method_name string the name of the function to be hooked
- callback func the callback which will be called after the hooked function is called
Usage:
HookFunction("Server.DestroyEntity", function(entity) Puts "An entity has been destoyed" end)
- HookGamerulesFunctionPre (method_name, callback)
-
Pre-hooks a Gamerules function and calls the callback every time the function is about to be called.
Varargs are passed to the callback as a table. Other plugins can use Pre/post hook forwards to hook this kind of hook.
Parameters:
- method_name string the name of the Gamerules function to be hooked
- callback func the callback which will be called before the hooked Gamerules function is called
- HookGamerulesFunction (method_name, callback)
-
Post-hooks a Gamerules function and calls the callback every time the function has been called.
Varargs are passed to the callback as a table. Other plugins can use Pre/post hook forwards to hook this kind of hook.
Parameters:
- method_name string the name of the Gamerules function to be hooked
- callback func the callback which will be called after the hooked Gamerules function is called
- HookNS2GamerulesFunctionPre (method_name, callback)
-
Pre-hooks a NS2Gamerules function and calls the callback every time the function is about to be called.
Varargs are passed to the callback as a table. Other plugins can use Pre/post hook forwards to hook this kind of hook.
Parameters:
- method_name string the name of the NS2Gamerules function to be hooked
- callback func the callback which will be called before the hooked NS2Gamerules function is called
- HookNS2GamerulesFunction (method_name, callback)
-
Post-hooks a NS2Gamerules function and calls the callback every time the function has been called.
Varargs are passed to the callback as a table. Other plugins can use Pre/post hook forwards to hook this kind of hook.
Parameters:
- method_name string the name of the NS2Gamerules function to be hooked
- callback func the callback which will be called after the hooked NS2Gamerules function is called
- InjectIntoScope (..., function)
-
Injects a function into the scope of other functions so that you can use locals they reference.
Parameters:
- ... any number of functions referencing locals you want to use
- function int or string to share scope with other functions, will not be called if passed as a string
- HookNetworkMessage (message_name, callback)
-
Hooks a network message and calls the callback when the network message is received
Parameters:
- message_name string the name of the message
- callback func the callback function
- SendNetworkMessage ( [target], message_name, message)
-
Sends a network message to one or all connected players.
Parameters:
- target ServerClient or Player a single player to send the network message to
- message_name string the name of the network message
- message tab the network message
- RespondToNetworkMessage (message_name, callback)
-
Hooks a network message and responds with the same message.
Parameters:
- message_name string the network message name
- callback func a function that is passed the received message and returns the response message