Difference between revisions of "AutoConfigs (AMX Mod X Scripting)"

From AlliedModders Wiki
Jump to: navigation, search
m (Fixed some typos)
(Adjusted spaces and added some examples)
Line 12: Line 12:
 
=== Native ===
 
=== Native ===
  
:{{margin|20px 0|{{tt|AutoExecConfig(bool:{{h:title||autoCreate}} <nowiki>=</nowiki> true, const {{h:title||name}}[] <nowiki>=</nowiki> "", const {{h:title||folder}}[] <nowiki>=</nowiki> "")|opt=large}}}}
+
:{{margin|20px 0 15px 0|{{tt|AutoExecConfig({{color|green|bool}}:{{h:title||autoCreate}} <nowiki>=</nowiki> true, const {{h:title||name}}[] <nowiki>=</nowiki> "", const {{h:title||folder}}[] <nowiki>=</nowiki> "")|opt=large}}}}
 +
::Specifies that the given config file should be executed after plugin load.
  
:Specifies that the given config file should be executed after plugin load.
+
::{|
 
 
:{|
 
 
|-
 
|-
 
| {{tt|autoCreate}} || {{ns}} If true, AMX Mod X will dump all cvars created by the plugin into a config file if the specified config file does not exist.
 
| {{tt|autoCreate}} || {{ns}} If true, AMX Mod X will dump all cvars created by the plugin into a config file if the specified config file does not exist.
Line 25: Line 24:
 
|}
 
|}
  
:{{alert|info|Note:|It is possible to write nested folders; AMX Mod X will attempt to create each one.|opt=full-border}}
+
::{{alert|info|Note:|It is possible to write nested folders; AMX Mod X will attempt to create each one.|opt=full-border}}
:{{alert|info|Note:|If you have multiple {{tt|AutoExecConfig|opt=nobg}} calls marked with {{tt|autoCreate|opt=nobg}} being true, the first file to be auto created will prevent any others from being created.  Thus, there is no way to automatically split cvars between multiple files.|opt=full-border}}
+
::{{alert|info|Note:|If you have multiple {{tt|AutoExecConfig|opt=nobg}} calls marked with {{tt|autoCreate|opt=nobg}} being true, the first file to be auto created will prevent any others from being created.  Thus, there is no way to automatically split cvars between multiple files.|opt=full-border}}
 +
::{{alert|success|Example:|{{tt|{{color|green|// Default values. /configs/plugins/plugin-hat.cfg will be created (if not present) and executed.}}<br />AutoExecConfig();<br />{{color|green|// plugin-hat.cfg will executed only.}}<br />AutoExecConfig(.autoCreate <nowiki>=</nowiki> false);<br />{{color|green|// plugin-MyHat.cfg will be created (if not present) and executed.}}<br />AutoExecConfig(.name <nowiki>=</nowiki> "MyHat");<br />{{color|green|// /configs/my-plugins/plugin-hat.cfg will be created (if not present) and executed.}}<br />AutoExecConfig(.folder <nowiki>=</nowiki> "my-plugins");|opt=nobg}}|opt=full-border}}
  
 
=== Forward ===
 
=== Forward ===
  
:{{margin|20px 0|{{tt|OnConfigsExecuted()|opt=large}}}}
+
:{{margin|20px 0 15px 0|{{tt|OnConfigsExecuted()|opt=large}}}}
  
:Called when the map has loaded, and all configs are done executing.
+
::Called when the map has loaded, and all configs are done executing.
:This includes {{tt|servercfgfile|opt=nobg}} ({{tt|server.cfg|opt=nobg}}), {{tt|amxx.cfg|opt=nobg}}, plugin's config, and per-map config.
+
::This includes {{tt|servercfgfile|opt=nobg}} ({{tt|server.cfg|opt=nobg}}), {{tt|amxx.cfg|opt=nobg}}, plugin's config, and per-map config.
  
:{{alert|info|Note:|This is best place to initialize plugin functions which are based on cvar data.|opt=full-border}}
+
::{{alert|info|Note:|This is best place to initialize plugin functions which are based on cvar data.|opt=full-border}}
:{{alert|info|Note:|This will always be called once and only once per map.  It will be called few seconds after {{tt|plugin_cfg()|opt=nobg}}.|opt=full-border}}
+
::{{alert|info|Note:|This will always be called once and only once per map.  It will be called few seconds after {{tt|plugin_cfg()|opt=nobg}}.|opt=full-border}}
 +
::{{alert|success|Example:|{{tt|public OnConfigsExecuted()<br />{<br />{{ns|4}}new const value <nowiki>=</nowiki> get_cvar_num("mp_timelimit");<br />{{ns|4}}...<br />}|opt=nobg}}|opt=full-border}}
  
:{{margin|30px 0 20px 0|{{tt|OnAutoConfigsBuffered()|opt=large}}}}
+
:{{margin|30px 0 15px 0|{{tt|OnAutoConfigsBuffered()|opt=large}}}}
  
:Called when the map has loaded, right after {{tt|plugin_cfg()|opt=nobg}} but any time before {{tt|OnConfigsExecuted()|opt=nobg}}.   
+
::Called when the map has loaded, right after {{tt|plugin_cfg()|opt=nobg}} but any time before {{tt|OnConfigsExecuted()|opt=nobg}}.   
:It's called after {{tt|amxx.cfg|opt=nobg}} and  all {{tt|AutoExecConfig|opt=nobg}} exec commands have been added to the server command buffer.
+
::It's called after {{tt|amxx.cfg|opt=nobg}} and  all {{tt|AutoExecConfig|opt=nobg}} exec commands have been added to the server command buffer.
  
:{{alert|info|Note:|This will always be called once and only once per map.|opt=full-border}}
+
::{{alert|info|Note:|This will always be called once and only once per map.|opt=full-border}}
  
 
== Example ==
 
== Example ==

Revision as of 15:07, 17 June 2017

Language: English  • français


Purpose

AMX Mod X provides a system for simple plugins to automatically generate config files which get executed on load. This is done via the AutoExecConfig native in scripting/include/amxmodx.inc.

Once all configuration files are executed, OnConfigsExecuted is called. This forward will always be called, even if your plugin had no configs or if it was loaded late.

Usage

Native

AutoExecConfig(bool:autoCreate = true, const name[] = "", const folder[] = "")
Specifies that the given config file should be executed after plugin load.
autoCreate     If true, AMX Mod X will dump all cvars created by the plugin into a config file if the specified config file does not exist.
name     Name of the config file (excluding the .cfg extension). If empty, the plugin's name will be used instead, with plugin- prepended. For example, hat.amxx becomes plugin-hat.cfg.
folder     Optionally change the folder under the main configs folder. By default configs go in /amxmodx/configs/.
Note:It is possible to write nested folders; AMX Mod X will attempt to create each one.
Note:If you have multiple AutoExecConfig calls marked with autoCreate being true, the first file to be auto created will prevent any others from being created. Thus, there is no way to automatically split cvars between multiple files.
Example:// Default values. /configs/plugins/plugin-hat.cfg will be created (if not present) and executed.
AutoExecConfig();
// plugin-hat.cfg will executed only.
AutoExecConfig(.autoCreate = false);
// plugin-MyHat.cfg will be created (if not present) and executed.
AutoExecConfig(.name = "MyHat");
// /configs/my-plugins/plugin-hat.cfg will be created (if not present) and executed.
AutoExecConfig(.folder = "my-plugins");

Forward

OnConfigsExecuted()
Called when the map has loaded, and all configs are done executing.
This includes servercfgfile (server.cfg), amxx.cfg, plugin's config, and per-map config.
Note:This is best place to initialize plugin functions which are based on cvar data.
Note:This will always be called once and only once per map. It will be called few seconds after plugin_cfg().
Example:public OnConfigsExecuted()
{
    new const value = get_cvar_num("mp_timelimit");
    ...
}
OnAutoConfigsBuffered()
Called when the map has loaded, right after plugin_cfg() but any time before OnConfigsExecuted().
It's called after amxx.cfg and all AutoExecConfig exec commands have been added to the server command buffer.
Note:This will always be called once and only once per map.

Example

#include <amxmodx>
 
public plugin_init()
{
    register_plugin("Hat", "User", "1.0");
 
    create_cvar("mysqlk_database", "", .description = "MySQL database");
    create_cvar("mysqlk_host", "localhost", .description = "MySQL host, use this to configure various^nthings for your server.");
 
    AutoExecConfig();
}

That would export a config file that looks like this:

// This file was auto-generated by AMX Mod X (v1.9)
// Cvars for plugin "Hat" by "User" (hat.amxx, v1.0)
 
 
// MySQL database
// -
// Default: ""
mysqlk_database ""
 
// MySQL host, use this to configure various
// things for your server.
// -
// Default: "localhost"
mysqlk_host "localhost"