View Single Post
Author Message
r4ndomz
Senior Member
Join Date: Jul 2009
Location: The Hood
Old 12-03-2010 , 16:16   [TUT] Config files
Reply With Quote #1

Config files

This is a basic example of a config that you can add to your work.
What it does basicly is just create a config file, write to it, and execute it just
like the server would execute server.cfg or autoexec.cfg.



1. In plugin_init(), add ExecuteConfigs();

PHP Code:
public plugin_init()
{
    
register_plugin("Config""1.00""Exolent")

    
ExecuteConfigs();     //if this isnt here the server wont bother what your about to do below

2. Create your ExecuteConfigs() function, and initially create the file

PHP Code:
public ExecuteConfigs()
{
    new 
sConfig[64];     //Create the variable
    
get_localinfo("amxx_configdir"sConfig63)     //get the configs directory (addons/amxmodx/configs/)
    
add(sConfig63"/tutorial.cfg"0);     //create the file being used
    
    
if( file_exists(sConfig) )     //if its already there just execute it
    
{
        
server_cmd("exec %s"sConfig);
        
server_exec();
    }
    else
    {
        
make_config(sConfig);     //if its not then create it with the information we made here
    
}

3. Pick up where you left off

PHP Code:
make_config(const sConfig[])
{
    new 
fopen(sConfig"wt");     //open the file with the flags Write and Text
    
    
fputs(f"// This is where the configs thing is.^n^n^n");     //Add a comment line with 3 spaces (Note ^n means "New line")
    
    
fprintf(f"config_cvar %i^n"get_pcvar_num(p_cvar)); //Write the cvars for your plugin in the form used to change the cvars ex. amx_cvar "sv_gravity"

    
    
fclose(f);     //close the file

Add it all together and check if it works (Note: The below example has what you need to test, above does not)

Code:
#include <amxmodx>


new p_cvar


public plugin_init()
{
    register_plugin("Config", "1.00", "r4ndomz")

    p_cvar = register_cvar("config_cvar", "1337")
    register_clcmd("say pcvar", "print_pcvar")

    ExecuteConfigs();
}


public ExecuteConfigs()
{
    new sConfig[64];
    get_localinfo("amxx_configdir", sConfig, 63)
    add(sConfig, 63, "/tutorial.cfg", 0);
    
    if( file_exists(sConfig) )
    {
        server_cmd("exec %s", sConfig);
        server_exec();
    }
    else
    {
        make_config(sConfig);
    }
}

make_config(const sConfig[])
{
    new f = fopen(sConfig, "wt");
    
    fputs(f, "// This is where the configs thing is.^n^n^n");
    
    fprintf(f, "config_cvar %i^n", get_pcvar_num(p_cvar));

    
    fclose(f);
}


public print_pcvar(id)
{
    client_print(id, print_chat, "Pcvar is currently set at %i", get_pcvar_num(p_cvar))
}
Credit to exolent, original code from him.
Don't do drugs.
__________________
HideNSeek bug fixed here:http://forums.alliedmods.net/showpos...&postcount=951

PM me if you want a nice HideNSeek shop
NEED PLUGIN TESTERS PM ME

Last edited by r4ndomz; 12-04-2010 at 13:42.
r4ndomz is offline
Send a message via Skype™ to r4ndomz