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

Read Cvar From File


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BaD CopY
Senior Member
Join Date: Oct 2014
Location: Home
Old 03-10-2018 , 19:25   Read Cvar From File
Reply With Quote #1

How can I read value after 'something', like in example:

if file.ini looks like this:

Code:
; This will be ignored

some_value "VALUE"
How can I get "VALUE" for 'some_value' ?
BaD CopY is offline
Send a message via Yahoo to BaD CopY Send a message via Skype™ to BaD CopY
D3XT3R
AlliedModders Donor
Join Date: Nov 2016
Location: Lithuania, Bomb A (Kauna
Old 03-11-2018 , 05:19   Re: Read Cvar From File
Reply With Quote #2

you cant and maybe you can but best way is
Code:
public plugin_cfg() {     new ConfDir[32], File[192];         get_configsdir( ConfDir, charsmax( ConfDir ) );     formatex( File, charsmax( File ), "%s/value.cfg", ConfDir );         if( !file_exists( File ) )     {         server_print( "File %s doesn't exist!", File );         write_file( File, " ", -1 );     }     else     {            server_print( "%s successfully loaded.", File );         server_cmd( "exec %s", File );     } }

and in value.cfg you can do like this:

Quote:
/* This will be ignored */

some_value "VALUE"
__________________

Last edited by D3XT3R; 03-11-2018 at 05:20.
D3XT3R is offline
Send a message via Skype™ to D3XT3R
BaD CopY
Senior Member
Join Date: Oct 2014
Location: Home
Old 03-11-2018 , 08:22   Re: Read Cvar From File
Reply With Quote #3

This is not what I'm looking for.. How can I read "VALUE" for specific 'some_value' ? What if my cfg file looks like this:
Code:
some_value1 "VALUE"
some_value2 "VALUE"
some_value3 "VALUE"
How can I get only "VALUE" for 'some_value2' ?
BaD CopY is offline
Send a message via Yahoo to BaD CopY Send a message via Skype™ to BaD CopY
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 03-11-2018 , 08:57   Re: Read Cvar From File
Reply With Quote #4

get_pcvar_num / get_pcvar_string
__________________
Relaxing is offline
BaD CopY
Senior Member
Join Date: Oct 2014
Location: Home
Old 03-11-2018 , 09:07   Re: Read Cvar From File
Reply With Quote #5

get_cvar_num("some_value2") returns 0
get_pcvar_num("some_value2") gets error 035: argument type mismatch (argument 1)

EDIT: VALUE of "some_value2" is "12345" (num)

Last edited by BaD CopY; 03-11-2018 at 09:08.
BaD CopY is offline
Send a message via Yahoo to BaD CopY Send a message via Skype™ to BaD CopY
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 03-11-2018 , 09:57   Re: Read Cvar From File
Reply With Quote #6

Are this actually cvars or are you trying to create a settings file and read the values in some variables?
__________________
HamletEagle is offline
BaD CopY
Senior Member
Join Date: Oct 2014
Location: Home
Old 03-11-2018 , 10:02   Re: Read Cvar From File
Reply With Quote #7

Create settings file and read the values in some variables
BaD CopY is offline
Send a message via Yahoo to BaD CopY Send a message via Skype™ to BaD CopY
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 03-11-2018 , 10:07   Re: Read Cvar From File
Reply With Quote #8

First of all, use the scripting section for help about scripting...

Take a look at my plugins, almost all of them use .ini files to read settings. See this one for example - https://forums.alliedmods.net/showthread.php?t=304480 - take a look at the custom ReadFile() function.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
BaD CopY
Senior Member
Join Date: Oct 2014
Location: Home
Old 03-11-2018 , 10:29   Re: Read Cvar From File
Reply With Quote #9

I tried like this but again value is 0

Code:
new some_value2;
			
new File = fopen(cfgFile, "rt")
			
if(File) {
	new szData[96], szValue[64], szKey[32];
				
	while(!feof(File)) {
		fgets(File, szData, charsmax(szData));
		trim(szData);
					
		switch(szData[0]) {
			case EOS, '#', ';': continue
						
			default: {
				strtok(szData, szKey, charsmax(szKey), szValue, charsmax(szValue), '=');
				trim(szKey); trim(szValue);
							
				if(equal(szKey, "some_value2"))
					some_value2 = str_to_num(szKey);
			}
		}
	}
}

console_print(0, "%i", some_value2);
BaD CopY is offline
Send a message via Yahoo to BaD CopY Send a message via Skype™ to BaD CopY
Natsheh
Veteran Member
Join Date: Sep 2012
Old 03-11-2018 , 10:32   Re: Read Cvar From File
Reply With Quote #10

some_value2 = str_to_num(szValue);

In file should be like this

some_value2 = 5
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 03-11-2018 at 10:33.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
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:59.


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