Raised This Month: $ Target: $400
 0% 

How do you read from a cfg file? [SOLVED]


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
mateo10
Veteran Member
Join Date: Jan 2006
Old 11-28-2006 , 09:40   How do you read from a cfg file? [SOLVED]
Reply With Quote #1

Hello I wonder what is wrong with this, and I wonder how do you read
from a cfg file? My version (read_from_file) has a few leaks (LOL).
What is the right way to do this?


Code:
#include <amxmodx> #include <fun> new cvar public plugin_init() {     register_plugin("Spelltime", "1.0", "MaTTe")     cvar = register_cvar("amx_spelltime", "1")     register_clcmd("say /spelltime", "cmd_spelltime")     register_clcmd("say_team /spelltime", "cmd_spelltime")     new basedir[64]     new cfgfile[64]     format(cfgfile, 63, "%s/addons/amxmodx/configs/spelltime/spelltime.cfg", basedir)     if(file_exists(cfgfile))     {         log_amx("[Spelltime] Loading cfgfile [spelltime.cfg]...")         server_cmd("exec %s", cfgfile)         log_amx("[Spelltime] Cfgfile loaded successfully.")     }else{         log_amx("[Spelltime] Cfgfile not found. [Pausing plugin]")         server_cmd("amxx pause spelltime")     }     return PLUGIN_CONTINUE } public cmd_spelltime(id) {     if(!get_pcvar_num(cvar))         return PLUGIN_HANDLED     new sound = read_from_file(cfgfile)     new sound2 = read_from_file(cfgfile)     new sound3 = read_from_file(cfgfile)     new sound4 = read_from_file(cfgfile)     new sound5 = read_from_file(cfgfile)     new rand = random_num(1, 5)     switch(rand)     {         case 1:         {             client_cmd(id, "play spelltime/%s.wav", sound)         }         case 2:         {             client_cmd(id, "play spelltime/%s.wav", sound2)         }         case 3:         {             client_cmd(id, "play spelltime/%s.wav", sound3)         }         case 4:         {             client_cmd(id, "play spelltime/%s.wav", sound4)         }         case 5:         {             client_cmd(id, "play spelltime/%s.wav", sound5)         }     }     new text[200]     read_args(text, 199)     if(rand==1)     {         if(containi(text, "i guess it is %s", sound)==1)         {             client_print(id, print_chat, "You guessed right! +5 health")             set_user_health(id, get_user_health(id)+5)         }else{             client_print(id, print_chat, "You guessed wrong! -5 health")             set_user_health(id, get_user_health(id)-5)         }     } else if(rand==2)     {         if(containi(text, "i guess it is %s", sound2)==1)         {             client_print(id, print_chat, "You guessed right! +5 health")             set_user_health(id, get_user_health(id)+5)         }else{             client_print(id, print_chat, "You guessed wrong! -5 health")             set_user_health(id, get_user_health(id)-5)         }     } else if(rand==3)     {         if(containi(text, "i guess it is %s", sound3)==1)         {             client_print(id, print_chat, "You guessed right! +5 health")             set_user_health(id, get_user_health(id)+5)         }else{             client_print(id, print_chat, "You guessed wrong! -5 health")             set_user_health(id, get_user_health(id)-5)         }     } else if(rand==4)     {         if(containi(text, "i guess it is %s", sound4)==1)         {             client_print(id, print_chat, "You guessed right! +5 health")             set_user_health(id, get_user_health(id)+5)         }else{             client_print(id, print_chat, "You guessed wrong! -5 health")             set_user_health(id, get_user_health(id)-5)         }     } else if(rand==5)     {         if(containi(text, "i guess it is %s", sound5)==1)         {             client_print(id, print_chat, "You guessed right! +5 health")             set_user_health(id, get_user_health(id)+5)         }else{             client_print(id, print_chat, "You guessed wrong! -5 health")             set_user_health(id, get_user_health(id)-5)         }     }     return PLUGIN_CONTINUE }

Last edited by mateo10; 12-01-2006 at 10:11.
mateo10 is offline
The Specialist
BANNED
Join Date: Nov 2006
Old 11-28-2006 , 09:53   Re: How do you read from a cfg file?
Reply With Quote #2

Have you read the tutorial on reading and writing to files with the new read and write system? If not i suggest you go read it before you procced.. Also I would suggest making the second part of your code into a switch statement. It looks horable lol .
The Specialist is offline
Send a message via AIM to The Specialist
mateo10
Veteran Member
Join Date: Jan 2006
Old 11-28-2006 , 09:56   Re: How do you read from a cfg file?
Reply With Quote #3

And where can i find that tutorial
mateo10 is offline
The Specialist
BANNED
Join Date: Nov 2006
Old 11-28-2006 , 10:01   Re: How do you read from a cfg file?
Reply With Quote #4

most likely in the tutorial section lol.
http://forums.alliedmods.net/showthread.php?t=46218

if you need help cleaning your code up let me know ill clean it for you
The Specialist is offline
Send a message via AIM to The Specialist
mateo10
Veteran Member
Join Date: Jan 2006
Old 11-28-2006 , 10:17   Re: How do you read from a cfg file?
Reply With Quote #5

Well OK, I would appreciate if you cleaned it.
mateo10 is offline
The Specialist
BANNED
Join Date: Nov 2006
Old 11-28-2006 , 10:36   Re: How do you read from a cfg file?
Reply With Quote #6

now this wont compile cause your code had like 10 errors, im assuming this is un-finished code. But this will make it a little cleaner hope it helps

Code:
#include <amxmodx> #include <fun> new cvar new read_from_file public plugin_init() {  register_plugin("Spelltime", "1.0", "MaTTe")    cvar = register_cvar("amx_spelltime", "1")    register_clcmd("say /spelltime", "cmd_spelltime")  register_clcmd("say_team /spelltime", "cmd_spelltime")    new basedir[64]  new cfgfile[64]  format(cfgfile, 63, "%s/addons/amxmodx/configs/spelltime/spelltime.cfg", basedir)  if(file_exists(cfgfile))  {   log_amx("[Spelltime] Loading cfgfile [spelltime.cfg]...")   server_cmd("exec %s", cfgfile)   log_amx("[Spelltime] Cfgfile loaded successfully.")   }else{   log_amx("[Spelltime] Cfgfile not found. [Pausing plugin]")   server_cmd("amxx pause spelltime")  }   } public cmd_spelltime(id) {  if(!get_pcvar_num(cvar))   return PLUGIN_HANDLED    new sound   =  read_from_file(cfgfile)  new sound2 = read_from_file(cfgfile)  new sound3 = read_from_file(cfgfile)  new sound4 = read_from_file(cfgfile)  new sound5 = read_from_file(cfgfile)    new rand = random_num(1, 5)    switch(rand)  {   case 1:   {    client_cmd(id, "play spelltime/%s.wav", sound)   }   case 2:   {    client_cmd(id, "play spelltime/%s.wav", sound2)   }   case 3:   {    client_cmd(id, "play spelltime/%s.wav", sound3)   }   case 4:   {    client_cmd(id, "play spelltime/%s.wav", sound4)   }   case 5:   {    client_cmd(id, "play spelltime/%s.wav", sound5)   }  }    new text[200]  read_args(text, 199)    switch(rand)  {   case 0 : containi(text, "i guess it is %s", sound)==1)   case 1:   {    client_print(id, print_chat, "You guessed right! +5 health")    set_user_health(id, get_user_health(id)+5)   }else{    client_print(id, print_chat, "You guessed wrong! -5 health")    set_user_health(id, get_user_health(id)-5)   }   case 2:   {    if(containi(text, "i guess it is %s", sound2)==1)    {     client_print(id, print_chat, "You guessed right! +5 health")     set_user_health(id, get_user_health(id)+5)    }else{     client_print(id, print_chat, "You guessed wrong! -5 health")     set_user_health(id, get_user_health(id)-5)    }   }   case 3:   {    if(containi(text, "i guess it is %s", sound3)==1)    {     client_print(id, print_chat, "You guessed right! +5 health")     set_user_health(id, get_user_health(id)+5)    }else{     client_print(id, print_chat, "You guessed wrong! -5 health")     set_user_health(id, get_user_health(id)-5)    }   }   case 4:   {    if(containi(text, "i guess it is %s", sound4)==1)    {     client_print(id, print_chat, "You guessed right! +5 health")     set_user_health(id, get_user_health(id)+5)    }else{     client_print(id, print_chat, "You guessed wrong! -5 health")     set_user_health(id, get_user_health(id)-5)    }   }   case 5:   {    if(containi(text, "i guess it is %s", sound5)==1)    {     client_print(id, print_chat, "You guessed right! +5 health")     set_user_health(id, get_user_health(id)+5)    }else{     client_print(id, print_chat, "You guessed wrong! -5 health")     set_user_health(id, get_user_health(id)-5)    }  }  return PLUGIN_CONTINUE }
The Specialist is offline
Send a message via AIM to The Specialist
mateo10
Veteran Member
Join Date: Jan 2006
Old 11-28-2006 , 10:45   Re: How do you read from a cfg file?
Reply With Quote #7

But that link you gave me does it have to be that hard?
Isn't there an easier way? If there isn't please tell me how to to it.

Thanks
mateo10 is offline
The Specialist
BANNED
Join Date: Nov 2006
Old 11-28-2006 , 10:50   Re: How do you read from a cfg file?
Reply With Quote #8

nope
The Specialist is offline
Send a message via AIM to The Specialist
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 11-28-2006 , 12:49   Re: How do you read from a cfg file?
Reply With Quote #9

Code:
 new basedir[64]  new cfgfile[64]  format(cfgfile, 63, "%s/addons/amxmodx/configs/spelltime/spelltime.cfg", basedir) //
Does this look clean?
nope.
Code:
new cfgfile[64] get_configsdir(cfgfile, 63) add(cfgfile, 63, "/spelltime/spelltime.cfg")
[ --<-@ ] Black Rose is offline
The Specialist
BANNED
Join Date: Nov 2006
Old 11-28-2006 , 13:43   Re: How do you read from a cfg file?
Reply With Quote #10

well then its a good thing i told him i would only make a switch statement for him then. If your so concerned clean his whole code .
The Specialist is offline
Send a message via AIM to The Specialist
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 06:52.


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