AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How do you read from a cfg file? [SOLVED] (https://forums.alliedmods.net/showthread.php?t=47857)

mateo10 11-28-2006 09:40

How do you read from a cfg file? [SOLVED]
 
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 }

The Specialist 11-28-2006 09:53

Re: How do you read from a cfg file?
 
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 . :shock:

mateo10 11-28-2006 09:56

Re: How do you read from a cfg file?
 
And where can i find that tutorial

The Specialist 11-28-2006 10:01

Re: How do you read from a cfg file?
 
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 :wink:

mateo10 11-28-2006 10:17

Re: How do you read from a cfg file?
 
Well OK, I would appreciate if you cleaned it.

The Specialist 11-28-2006 10:36

Re: How do you read from a cfg file?
 
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 }

mateo10 11-28-2006 10:45

Re: How do you read from a cfg file?
 
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

The Specialist 11-28-2006 10:50

Re: How do you read from a cfg file?
 
nope

[ --<-@ ] Black Rose 11-28-2006 12:49

Re: How do you read from a cfg file?
 
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")

The Specialist 11-28-2006 13:43

Re: How do you read from a cfg file?
 
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 . :up:


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

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