AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [TUT] Config files (https://forums.alliedmods.net/showthread.php?t=144411)

r4ndomz 12-03-2010 16:16

[TUT] Config files
 
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.

fysiks 12-03-2010 17:03

Re: [TUT] Config files
 
Ummm . . . what? I see not logic for something like this (what ever it is).

r4ndomz 12-03-2010 20:38

Re: [TUT] Config files
 
Quote:

Originally Posted by fysiks (Post 1362303)
Ummm . . . what? I see not logic for something like this (what ever it is).

The logic is to make a configuration file. Did you even read it?

A while ago i wanted to make a config file..but i did not know how to do it. I searched and didnt find anything related to this matter.

This is a clear-cut way of making a config file. You wont need to look through a plugin that already has a config file in its code and try to find out how it works.

Kreation 12-03-2010 21:57

Re: [TUT] Config files
 
So did this come from your brain? Or someone else's( Exolent ). You know what I mean, give credit where credit is due please. Thanks.

fysiks 12-03-2010 23:20

Re: [TUT] Config files
 
Quote:

Originally Posted by r4ndomz (Post 1362442)
The logic is to make a configuration file. Did you even read it?

A while ago i wanted to make a config file..but i did not know how to do it. I searched and didnt find anything related to this matter.

This is a clear-cut way of making a config file. You wont need to look through a plugin that already has a config file in its code and try to find out how it works.

Yes, I read the post but I also have this very rarely used and highly secret program that I use called Notepad to create a config file. And, I can document it as well instead of it just being a plain list of cvars.

r4ndomz 12-03-2010 23:28

Re: [TUT] Config files
 
Quote:

Originally Posted by fysiks (Post 1362560)
Yes, I read the post but I also have this very rarely used and highly secret program that I use called Notepad to create a config file. And, I can document it as well instead of it just being a plain list of cvars.

Then this is an example of doing that in code.


Quote:

Originally Posted by Kreation (Post 1362490)
So did this come from your brain? Or someone else's( Exolent ). You know what I mean, give credit where credit is due please. Thanks.

Thanks for the reminder.

wrecked_ 12-03-2010 23:37

Re: [TUT] Config files
 
Quote:

Originally Posted by r4ndomz (Post 1362562)
Then this is an example of doing that in code.

This is an example of someone defending a cause without reasoning, only because their elite role model had done it in the past.

Excalibur.007 12-03-2010 23:49

Re: [TUT] Config files
 
Use get_localinfo, so you wouldn't need amxmisc :).

r4ndomz 12-04-2010 00:47

Re: [TUT] Config files
 
Quote:

Originally Posted by wrecked_ (Post 1362566)
This is an example of someone defending a cause without reasoning, only because their elite role model had done it in the past.


And who exactly is my elite role model?

shuttle_wave 12-04-2010 04:25

Re: [TUT] Config files
 
Quote:

Originally Posted by r4ndomz (Post 1362562)
Then this is an example of doing that in code.

Example to Copy and Paste from Exolent's Code


All times are GMT -4. The time now is 12:32.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.