Raised This Month: $344 Target: $400
 86%   Almost there!

Solved Need Code For CFG FILE


Post New Thread Reply   
 
Thread Tools Display Modes
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-24-2022 , 21:11   Re: Need Code For CFG FILE
Reply With Quote #31

Quote:
Originally Posted by Begineer Programmer View Post
yeah i am working on what you give me !!!
Here i Find Another link too: https://forums.alliedmods.net/showpo...93&postcount=5
That one uses an old file reading method. Just use mine, very little needs done to make it work for your function. There are probably situations where it can be useful but not in this case.
__________________

Last edited by fysiks; 07-24-2022 at 21:13.
fysiks is offline
Begineer Programmer
Member
Join Date: Nov 2021
Old 07-24-2022 , 21:23   Re: Need Code For CFG FILE
Reply With Quote #32

Quote:
Originally Posted by fysiks View Post
That one uses an old file reading method. Just use mine, very little needs done to make it work for your function. There are probably situations where it can be useful but not in this case.
Bro can you come to discord can i share you my discord id ?
Begineer Programmer is offline
lexzor
Senior Member
Join Date: Nov 2020
Old 07-24-2022 , 21:28   Re: Need Code For CFG FILE
Reply With Quote #33

i hope you will understand better
PHP Code:
getCvarValue(const szMap[])
{
    new 
szFile[128], szConfigsDir[128], szMapName[32], iValue;    
    
get_configsdir(szConfigsDircharsmax(szConfigsDir));
    
formatex(szFilecharsmax(szFile), "%s/%s_amxx.cfg"szConfigsDirszMap);

    if(
file_exists(szFile))
    {
        new 
iFile fopen(szFile"r");
        new 
szCvar[32], szValue[32], szCfgData[64];
        
        while(
fgets(iFileszCfgDatacharsmax(szCfgData)))
        {
            
parse(szDataszCvarcharsmax(szCvar), szValuecharsmax(szValue));

            if(
equali(szCvar"mp_maxrounds"))
            {
                
iValue str_to_num(szValue);
                break;
            }
        }

        
fclose(iFile);
    }

    return 
iValue == ? -iValue;
}

client_command(id)
{
    new 
szMapName[32];
    
get_mapname(szMapNamecharsmax(szMapName));

    new 
iValue getCvarValue(szMapName);

    if(
iValue == -1)
    {
        
client_print_color(idprint_team_default"^4[CVAR]^1 No settings for this map");
    }
    else 
    {
        
client_print_color(idprint_team_default"^4[CVAR]^1 On this map will pe played^3 %i rounds^1."iValue);
    }


Last edited by lexzor; 07-24-2022 at 21:33.
lexzor is offline
Begineer Programmer
Member
Join Date: Nov 2021
Old 07-24-2022 , 21:30   Re: Need Code For CFG FILE
Reply With Quote #34

Quote:
Originally Posted by lexzor View Post
i hope you will understand better
PHP Code:
getCvarValue(const szMap[])
{
    new 
szFile[128], szConfigsDir[128], szMapName[32], iValue;    
    
get_configsdir(szConfigsDircharsmax(szConfigsDir));
    
formatex(szFilecharsmax(szFile), "%s/%s_amxx.cfg"szConfigsDirszMap);

    if(
file_exists(szFile))
    {
        new 
iFile fopen(szFile"r");
        new 
szCvar[32], szValue[32], szCfgData[64];
        
        while(
fgets(iFileszCfgDatacharsmax(szCfgData)))
        {
            
parse(szDataszCvarcharsmax(szCvar), szValuecharsmax(szValue));

            if(
equali(szCvar"mp_maxrounds"))
            {
                
iValue str_to_num(szValue);
                break;
            }
        }
    }

    return 
iValue == ? -iValue;
}

client_command(id)
{
    new 
szMapName[32];
    
get_mapname(szMapNamecharsmax(szMapName));

    new 
iValue getCvarValue(szMapName);

    if(
iValue == -1)
    {
        
client_print_color(idprint_team_default"^4[CVAR]^1 No settings for this map");
    }
    else 
    {
        
client_print_color(idprint_team_default"^4[CVAR]^1 On this map will pe played^3 %i rounds^1."iValue);
    }

Thx mate !!! <3
Begineer Programmer is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-24-2022 , 21:55   Re: Need Code For CFG FILE
Reply With Quote #35

If I thought he wasn't capable of doing this himself, I would have just done it (he said he wanted to learn).

Why did you add the "_amxx" to the cfg file? That will prevent this from working at all. Also, you should remove the break because if the cvar is defined twice or more in the config file then only the last one will actually be accurate.

The other thing is that just because a file exists doesn't mean it will be accessible. So, you don't need to see if it exists, just try to open if and if you don't get a valid file pointer then you can't execute any of the file reading code (this method is shown by the code I've referenced).
__________________

Last edited by fysiks; 07-24-2022 at 21:59.
fysiks is offline
Begineer Programmer
Member
Join Date: Nov 2021
Old 07-24-2022 , 22:05   Re: Need Code For CFG FILE
Reply With Quote #36

Quote:
Originally Posted by fysiks View Post
If I thought he wasn't capable of doing this himself, I would have just done it (he said he wanted to learn).

Why did you add the "_amxx" to the cfg file? That will prevent this from working at all. Also, you should remove the break because if the cvar is defined twice or more in the config file then only the last one will actually be accurate.

The other thing is that just because a file exists doesn't mean it will be accessible. So, you don't need to see if it exists, just try to open if and if you don't get a valid file pointer then you can't execute any of the file reading code (this method is shown by the code I've referenced).
PHP Code:
fgets(iFileszCfgDatacharsmax(szCfgData)) 
And
Parse
i need to understand this what going that there ?
Begineer Programmer is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-24-2022 , 22:37   Re: Need Code For CFG FILE
Reply With Quote #37

Quote:
Originally Posted by Begineer Programmer View Post
PHP Code:
fgets(iFileszCfgDatacharsmax(szCfgData)) 
And
Parse
i need to understand this what going that there ?
It gets the next line of the file and stores it in the szCfgData variable.
__________________

Last edited by fysiks; 07-24-2022 at 22:38.
fysiks is offline
Begineer Programmer
Member
Join Date: Nov 2021
Old 07-24-2022 , 22:37   Re: Need Code For CFG FILE
Reply With Quote #38

Quote:
Originally Posted by fysiks View Post
If I thought he wasn't capable of doing this himself, I would have just done it (he said he wanted to learn).

Why did you add the "_amxx" to the cfg file? That will prevent this from working at all. Also, you should remove the break because if the cvar is defined twice or more in the config file then only the last one will actually be accurate.

The other thing is that just because a file exists doesn't mean it will be accessible. So, you don't need to see if it exists, just try to open if and if you don't get a valid file pointer then you can't execute any of the file reading code (this method is shown by the code I've referenced).
PHP Code:
get_map_maxrounds(szMapname[])
{
    new 
szConfigsDir[64], szFilePath[128]
    
get_configsdir(szConfigsDircharsmax(szConfigsDir))
    
format(szFilePathcharsmax(szFilePath), "%s/maps/%s.cfg",szConfigsDirszMapname)
    new 
fopen(szFilePath"rt")
    if( 
)
    {
        new 
szCvar[32], szValue[32], szCfgData[64],iValue;
        
        while(
fgets(fszCfgDatacharsmax(szCfgData)))
        {
            
parse(szCfgDataszCvarcharsmax(szCvar), szValuecharsmax(szValue));

            if(
equali(szCvar"mp_maxrounds"))
            {
                
iValue str_to_num(szValue)
                return 
iValue;
            }
        }
        
fclose(f)
    }
    

Quote:
Also, you should remove the break because if the cvar is defined twice or more in the config file then only the last one will actually be accurate.
check this one ?
Begineer Programmer is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-24-2022 , 22:43   Re: Need Code For CFG FILE
Reply With Quote #39

Quote:
Originally Posted by Begineer Programmer View Post
PHP Code:
get_map_maxrounds(szMapname[])
{
    new 
szConfigsDir[64], szFilePath[128]
    
get_configsdir(szConfigsDircharsmax(szConfigsDir))
    
format(szFilePathcharsmax(szFilePath), "%s/maps/%s.cfg",szConfigsDirszMapname)
    new 
fopen(szFilePath"rt")
    if( 
)
    {
        new 
szCvar[32], szValue[32], szCfgData[64],iValue;
        
        while(
fgets(fszCfgDatacharsmax(szCfgData)))
        {
            
parse(szCfgDataszCvarcharsmax(szCvar), szValuecharsmax(szValue));

            if(
equali(szCvar"mp_maxrounds"))
            {
                
iValue str_to_num(szValue)
                return 
iValue;
            }
        }
        
fclose(f)
    }
    


check this one ?
An important part of learning to code is to learn how to test and debug your own plugins. There very least that you could do is to test your code on your server to see if it works. It can be done really quickly and will tell you if the basics is working.

Regarding the code, it looks mostly correct. However, I would move the return to the end of the function so that it takes into account for cases where it's defined multiple times.

EDIT: You also can't return before fclose() because it will cause issues if you don't properly close the file (as pointed out by +ARUKARI-).
__________________

Last edited by fysiks; 07-24-2022 at 22:49.
fysiks is offline
+ARUKARI-
AlliedModders Donor
Join Date: Jul 2004
Location: Japan
Old 07-24-2022 , 22:47   Re: Need Code For CFG FILE
Reply With Quote #40

Quote:
Code:
        while(fgets(f, szCfgData, charsmax(szCfgData)))         {             parse(szCfgData, szCvar, charsmax(szCvar), szValue, charsmax(szValue));             if(equali(szCvar, "mp_maxrounds"))             {                 iValue = str_to_num(szValue)
                return iValue;
            }         }         fclose(f)
Returned before fclose.
__________________
GitHub
SteamWishlist

六四天安門事件
+ARUKARI- is offline
Reply


Thread Tools
Display Modes

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 13:48.


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