Raised This Month: $51 Target: $400
 12% 

How to save cvars to .txt file and how to read from .txt file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Nezo
New Member
Join Date: Nov 2023
Old 11-26-2023 , 14:45   How to save cvars to .txt file and how to read from .txt file
Reply With Quote #1

Hello.

I want to know how to save cvars to .txt or .cfg file and than how to read this cvars from this files. Why? I made my first plugin ever (simple weapon menu). I know how to program in php, little bit in JS but Pawn is something else for me so i had help from old friend CHAT GPT. It took me around 2-3 days to make it work as i wanted, like show menu exactly at players spawn, show menu only once per round, select from menu only once per round. I used a lot of materials from this forum THX for that. Now i started to adding cvars so admin can change almost everything in plugin and it works! But cvars will reset to default (defined in plugin.amxx) after server restarts. And i saw that other plugins have .cfg files and its read values from this files. So i want to ask anybody if you can show me / teach me how to write these cvars to .cfg and than how to read this cvars from file. As i sayed i am begginer in Pawn and Amx Mod X Api so you can show easy method or it can by hard method but with descriptions what everything does and how it does.

Thank you a lot. Helps is appreciated. 🧐
Nezo is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-26-2023 , 18:25   Re: How to save cvars to .txt file and how to read from .txt file
Reply With Quote #2

Simply put your cvars in "addons/amxmodx/configs/amxx.cfg" and they will be loaded at the beginning of the map.

P.S. Don't use ChatGPT, actually learn.
__________________
fysiks is offline
mlibre
Veteran Member
Join Date: Nov 2015
Location: return PLUGIN_CONTINUE
Old 11-26-2023 , 20:43   Re: How to save cvars to .txt file and how to read from .txt file
Reply With Quote #3

Quote:
Originally Posted by Nezo View Post
I know how to program in php...
knowing that you use, you could say that it is similar to Pawn...

Quote:
Originally Posted by fysiks View Post
...Don't use ChatGPT, actually learn.
that's how it is

Quote:
Originally Posted by mlibre View Post
In practice the master is born
With reference to what has already been mentioned, you can choose to add a command to execute this file in parallel with amxx.cfg just open this file in case you don't want to write it directly there, add

PHP Code:
exec my_file_name.cfg 
and as for reading the file is another story, you can apply these natives file.inc
__________________
mlibre is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-26-2023 , 22:48   Re: How to save cvars to .txt file and how to read from .txt file
Reply With Quote #4

If the settings are implemented as cvars, don't create any new, special files, just let the end user put them in the appropriate place. Creating and executing a file specific to the plugin is unnecessary and reading a file with cvars in is even more unnecessary.
__________________
fysiks is offline
Nezo
New Member
Join Date: Nov 2023
Old 11-29-2023 , 11:09   Re: How to save cvars to .txt file and how to read from .txt file
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
Simply put your cvars in "addons/amxmodx/configs/amxx.cfg" and they will be loaded at the beginning of the map.

P.S. Don't use ChatGPT, actually learn.
So i did it like you said and it works. Now i would like to know how to make it to update cvar saved in amxx.cfg.
Nezo is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 11-29-2023 , 19:05   Re: How to save cvars to .txt file and how to read from .txt file
Reply With Quote #6

a suggestion:

read amxx.cfg and save every line, even it is a blank one

hook the event when the cvar is changed, search it in the array where you saved amxx.cfg and replace it with the new value then rewrite the file using the array with the changes.

Last edited by lexzor; 11-29-2023 at 19:05.
lexzor is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-01-2023 , 00:55   Re: How to save cvars to .txt file and how to read from .txt file
Reply With Quote #7

Quote:
Originally Posted by Nezo View Post
So i did it like you said and it works. Now i would like to know how to make it to update cvar saved in amxx.cfg.
You can set cvar values in your plugin with the cvar functions e.g. set_cvar_num(), set_cvar_string(), etc. Depending on where the server owner sets this cvar, it may update to the value in the file either on server start or on every map change.

You shouldn't be trying to change the value of the cvar in the file (because you don't know which file it's actually in; it doesn't have to be in amxx.cfg).

I think you need to explain what you're actually trying to do because it may be that you need to use something other than a cvar.
__________________
fysiks is offline
Nezo
New Member
Join Date: Nov 2023
Old 12-02-2023 , 19:31   Re: How to save cvars to .txt file and how to read from .txt file
Reply With Quote #8

Quote:
Originally Posted by fysiks View Post
You can set cvar values in your plugin with the cvar functions e.g. set_cvar_num(), set_cvar_string(), etc. Depending on where the server owner sets this cvar, it may update to the value in the file either on server start or on every map change.

You shouldn't be trying to change the value of the cvar in the file (because you don't know which file it's actually in; it doesn't have to be in amxx.cfg).

I think you need to explain what you're actually trying to do because it may be that you need to use something other than a cvar.
Yes you are right i should explain what i am trying to do. So currently i am trying to find and open file with fopen. Here is actual code:

PHP Code:
public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
// Add your code here...
    
register_clcmd("say /otvor""FindAndProcessConfigFile");
    
log_amx("----");
    
log_amx("Plugin started.");
    
log_amx("----");
}

public 
FindAndProcessConfigFile(playerid) {
    
client_print(playeridprint_chat"Command /otvor has runned.");    
    new 
file_handle fopen("/cstrike/addons/amxmodx/configs/og.ini""r");
    new 
buffer[256];
    
    if (
file_handle) {
        
// Súbor "og_nastavenie.ini" bol nájdený
        
while (fgets(file_handlebuffersizeof(buffer)) != 0) {
            
// Tu môžete pracovať s prečítaným reťazcom v bufferi
            
client_print(playeridprint_chatbuffer);
        }
        
fclose(file_handle);
        
log_amx("USPECH: File 'og.ini' was found.");
    } else {
        
// Súbor nebol nájdený
        
client_print(playeridprint_chat"File 'og.ini' was not found.");
        
log_amx("CHYBA: File 'og.ini' was not found.");
    }

So after writing /otvor in game, function runs and return error message "File 'og.ini' was not found." same in logs. I thing there is some error in getting directory of og.ini. I also tried "addons/amxmodx/configs/og.ini", "og.ini". But same. I am total begginer in Pawn and Amx Mod X Api so....

Something must by wrong with definig file path cuz in amx mod x api fgets is this:
PHP Code:
fgets(filebuffer[], maxlength); 
And i deffinded buffer[256] so as i am correct this means that one line from og.ini will be storred into buffer and 255 is expected number of characters and 0 will be added at end of line and "zero" is counted also as one character and maxlength is obtained using sizeof(buffer). File "og.ini" contains only one word = test
File has rw-r--r--, i tried also rwx------ but same result. 😥
Nezo is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-02-2023 , 23:27   Re: How to save cvars to .txt file and how to read from .txt file
Reply With Quote #9

I asked you to explain what you are trying to with your plugin regarding wanting to write cvar values. What you posted is essentially the XY Problem. I want to know why you can't just use cvars and stick with that so that you can have the most efficient use of features as possible in your plugin.

If you're dead set on writing/reading to/from a file, you should look at the many tutorials and threads that talk about this. Also, I would highly recommend not using client_print() functions to send data to a player when debugging these types of things. These types of for/while loops go way too fast to reliably send all of the data as client_print messages. You should print to the server console for this type of thing and send commands from and watch the output in the server console. It's also much quicker because you don't have to be in-game at all.
__________________
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:33.


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