AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   format/ex question/help (https://forums.alliedmods.net/showthread.php?t=171752)

da_ciouzan_oan 11-10-2011 13:03

format/ex question/help
 
Hello,
I have a little problem with formatex.. Let's say I just used function parse, to split a text from a file.

Code:
parse(_fileData[_fileCount][0],                         _fileData[_fileCount][1],charsmax(_fileData[][]),                         _fileData[_fileCount][2],charsmax(_fileData[][]),                         _fileData[_fileCount][3],charsmax(_fileData[][]),                         _fileData[_fileCount][4],charsmax(_fileData[][]),                         _fileData[_fileCount][5],charsmax(_fileData[][]),                         _fileData[_fileCount][6],charsmax(_fileData[][]),                         _fileData[_fileCount][7],charsmax(_fileData[][]),                         _fileData[_fileCount][8],charsmax(_fileData[][]),                         _fileData[_fileCount][9],charsmax(_fileData[][]),                         _fileData[_fileCount][10],charsmax(_fileData[][]))

Variable _fileData[x][10] will contain for example "%s is %s's boss"

Next I have 1 array which holds names, _names[33][128]

Now.. when I try to format another string using formatex like this:

Code:
new msg[192] formatex(msg, charsmax(msg), _fileData[x][10], _names[0], _names[1])

I get the "Error: Array must be indexed (variable "_names") on line xx". Someone told me, function "formatex" doesn't knows that _fileData[x][10] contains 2 %s, so thats why I get that error.
I want to know how should I do so it will work as I want?

Don't know how to explain better.. if you don't understand I will post the entire code.

Thanks in advance.

Bugsy 11-10-2011 13:55

Re: format/ex question/help
 
The format functions treat a string like any other function in pawn in regards to indexing. Post the exact variable declarations and format line that gives you an error.

da_ciouzan_oan 11-10-2011 14:09

Re: format/ex question/help
 
Here is the whole code:
http://pastebin.com/Hjr2ynCq

And I put this in the file to test:

"amx_slay" b s:adminname s:targetname 0 0 0 0 0 "ADMIN: %s slaughtered %s"

Bugsy 11-10-2011 15:10

Re: format/ex question/help
 
So each _names you have in formatex have 2 %s? That may be another problem but that would not throw a compiler error, if anything just a runtime error. I don't have access to a computer so I can't look at your full code now.

da_ciouzan_oan 11-10-2011 15:18

Re: format/ex question/help
 
Uhm, lets say that I used format before:
Code:
formatex(_names[0], charsmax(_names[]), "Lul") formatex(_names[1], charsmax(_names[]), "Woot")

Variable _fileData[0][10] holds the following text "%s is now %s." (I copied from a file using parse.. just an example)

Now I want to format a message like this:
Code:
formatex(message, charsmax(message), _fileData[0][10], _names[0], _names[1])

so I can't print it in chat later to get "Lul is now Woot.".

Bugsy 11-10-2011 15:31

Re: format/ex question/help
 
Try to make a little test script and see if it works. I do not see a problem but not sure how the compiler well take it.

da_ciouzan_oan 11-10-2011 16:24

Re: format/ex question/help
 
Uhm.. my script test compiled, I guess smthing it's wrong with my variable (still don't know what).
Buuuut.. still doesn't works, now I get a runtime error in the test script.

Code:
public plugin_init() {     register_plugin(PLUGIN, VERSION, AUTHOR)     register_clcmd("say", "_formatexTest") } public _formatexTest(id) {     static command[64], msg[64], msg1[32], msg2[32], msg0[192]     read_args(command, charsmax(command))     remove_quotes(command)         formatex(msg, charsmax(msg), "%s is now %s.")     formatex(msg1, charsmax(msg1), "Lul")     formatex(msg2, charsmax(msg2), "Woot")          if(equali(command, "formatex"))     {         formatex(msg0, charsmax(msg0), msg, msg1, msg2)         client_print(0, print_chat, msg0)     } }
L 11/10/2011 - 23:21:07: String formatted incorrectly - parameter 4 (total 3)
L 11/10/2011 - 23:21:07: [AMXX] Run time error 25 (plugin "formatex_test.amxx") - debug not enabled!

ConnorMcLeod 11-10-2011 16:27

Re: format/ex question/help
 
formatex(msg, charsmax(msg), "%s is now %s.")

->

formatex(msg, charsmax(msg), "%%s is now %%s.")

If it's not working, try :

msg = "%s is now %s."

Bugsy 11-10-2011 16:34

Re: format/ex question/help
 
Or use copy() since there's no formatting anyway.

or

new szStr[] = "%s says hi to %s"

da_ciouzan_oan 11-10-2011 16:42

Re: format/ex question/help
 
Quote:

Originally Posted by ConnorMcLeod (Post 1594132)
formatex(msg, charsmax(msg), "%s is now %s.")

->

formatex(msg, charsmax(msg), "%%s is now %%s.")

If it's not working, try :

msg = "%s is now %s."

It worked with '%%s'. Thanks.
But I still have a little problem at my script, not the test one.

I have this:

Code:
public client_command(id) {     new command[32], argument = 1, paramsplit[11][5][64], message[192], count = 0, Float:floatmsg[10], nummsg[10], strmsg[10][64], cont = 1     read_argv(0, command, charsmax(command))         if(get_cvar_num("amx_show_activity") == 0)     {         for(new i; i < _fileCount; i++)         {             if(equali(command, _fileData[i][1]))             {                 log_amx("[debug] Command '%s' detected", _fileData[i][1])                 if(get_user_flags(id) & read_flags(_fileData[i][2]))                 {                     log_amx("[debug] Access '%s' detected", _fileData[i][2])                     for(new a; a < 11; a++)                     {                         if(_fileData[i][a][1] == ':')                         {                             log_amx("[debug] Delimiter ':' detected")                             paramsplit[a][4][0] = -1                             split(_fileData[i][a], paramsplit[a][0], 63, paramsplit[a][1], 63, ":")                                                         log_amx("[debug] String splited into '%s' and '%s'", paramsplit[a][0], paramsplit[a][1])                             if(paramsplit[a][0][0] == 's') // string                                 paramsplit[a][4][0] = 1                             else if(paramsplit[a][0][0] == 'n') // integer (num)                                 paramsplit[a][4][0] = 2                             else if(paramsplit[a][0][0] == 'f') // float                                 paramsplit[a][4][0] = 3                                                             if(equali(paramsplit[a][1], "targetname"))                             {                                 read_argv(argument, paramsplit[a][1], charsmax(paramsplit[][]))                                 paramsplit[a][3][0] = cmd_target(id, paramsplit[a][1], CMDTARGET_OBEY_IMMUNITY | CMDTARGET_NO_BOTS | CMDTARGET_ALLOW_SELF)                                                                 if(!paramsplit[a][3][0])                                 {                                     log_amx("[debug] Invalid target.")                                     cont = 0                                     break;                                 }                                                                       if(!is_user_alive(paramsplit[a][3][0]))                                 {                                     log_amx("[debug] Target is dead.")                                     cont = 0                                     break;                                 }                                                                 get_user_name(paramsplit[a][3][0], paramsplit[a][0], charsmax(paramsplit[][]))                                 log_amx("[debug] A target name has been found")                                 log_amx("[debug] Name returned: '%s'", paramsplit[a][0])                                 argument++;                             }                             else if(equali(paramsplit[a][1], "adminname"))                             {                                 log_amx("[debug] An admin name has been found")                                 get_user_name(id, paramsplit[a][0], charsmax(paramsplit[][]))                                 log_amx("[debug] Name returned: '%s'", paramsplit[a][0])                             }                             else                             {                                 log_amx("[debug] Argument %s has been found !", paramsplit[a][1])                                 read_argv(argument, paramsplit[a][1], charsmax(paramsplit[][]))                                                                 if(strlen(paramsplit[a][1]) == 0)                                 {                                     log_amx("[debug] No argument inserted.")                                     cont = 0                                     break;                                 }                                                                   formatex(paramsplit[a][0], charsmax(paramsplit[][]), "%s", paramsplit[a][1])                                 log_amx("[debug] Message returned: '%s'", paramsplit[a][0])                                 argument++;                             }                                                             if(paramsplit[a][4][0] == 2)                                 nummsg[a] = str_to_num(paramsplit[a][1])                             else if(paramsplit[a][4][0] == 3)                                 floatmsg[a] = str_to_float(paramsplit[a][1])                             else                                 copy(strmsg[a], charsmax(strmsg[]), paramsplit[a][0])                             count++;                         }                     }                     if(cont == 1)                     {                         log_amx("[debug] Loop stopped at %d arguments!", count)                         switch(count)                         {                             case 1:                             {                                 formatex(message, charsmax(message), _fileData[i][10],                                     (paramsplit[3][4][0] == 3) ? floatmsg[3] : (paramsplit[3][4][0] == 2) ? nummsg[3] : strmsg[3])                                 log_amx("[debug] Message '%s' has been formatted!", message)                                 }                             case 2:                             {                                   formatex(message, charsmax(message), _fileData[i][10],                                     (paramsplit[3][4][0] == 3) ? floatmsg : (paramsplit[3][4][0] == 2) ? nummsg : paramsplit[3][0],                                     (paramsplit[4][4][0] == 3) ? floatmsg : (paramsplit[4][4][0] == 2) ? nummsg : paramsplit[4][0])                                 log_amx("[debug] Message '%s' has been formatted!", message)                                 }                             case 3:                             {                                 formatex(message, charsmax(message), _fileData[i][10],                                     (paramsplit[3][4][0] == 3) ? floatmsg : paramsplit[3][0],                                     (paramsplit[4][4][0] == 3) ? floatmsg : paramsplit[4][0],                                     (paramsplit[5][4][0] == 3) ? floatmsg : paramsplit[5][0])                                 log_amx("[debug] Message '%s' has been formatted!", message)                             }                             case 4:                             {                                 formatex(message, charsmax(message), _fileData[i][10],                                     (paramsplit[3][4][0] == 3) ? floatmsg : paramsplit[3][0],                                     (paramsplit[4][4][0] == 3) ? floatmsg : paramsplit[4][0],                                     (paramsplit[5][4][0] == 3) ? floatmsg : paramsplit[5][0],                                     (paramsplit[6][4][0] == 3) ? floatmsg : paramsplit[6][0])                                 log_amx("[debug] Message '%s' has been formatted!", message)                             }                             case 5:                             {                                   formatex(message, charsmax(message), _fileData[i][10],                                     (paramsplit[3][4][0] == 3) ? floatmsg : paramsplit[3][0],                                     (paramsplit[4][4][0] == 3) ? floatmsg : paramsplit[4][0],                                     (paramsplit[5][4][0] == 3) ? floatmsg : paramsplit[5][0],                                     (paramsplit[6][4][0] == 3) ? floatmsg : paramsplit[6][0],                                     (paramsplit[7][4][0] == 3) ? floatmsg : paramsplit[7][0])                                 log_amx("[debug] Message '%s' has been formatted!", message)                             }                             case 6:                             {                                 formatex(message, charsmax(message), _fileData[i][10],                                     (paramsplit[3][4][0] == 3) ? floatmsg : paramsplit[3][0],                                     (paramsplit[4][4][0] == 3) ? floatmsg : paramsplit[4][0],                                     (paramsplit[5][4][0] == 3) ? floatmsg : paramsplit[5][0],                                     (paramsplit[6][4][0] == 3) ? floatmsg : paramsplit[6][0],                                     (paramsplit[7][4][0] == 3) ? floatmsg : paramsplit[7][0],                                     (paramsplit[8][4][0] == 3) ? floatmsg : paramsplit[8][0])                                 log_amx("[debug] Message '%s' has been formatted!", message)                             }                             case 7:                             {                                 formatex(message, charsmax(message), _fileData[i][10],                                     (paramsplit[3][4][0] == 3) ? floatmsg : paramsplit[3][0],                                     (paramsplit[4][4][0] == 3) ? floatmsg : paramsplit[4][0],                                     (paramsplit[5][4][0] == 3) ? floatmsg : paramsplit[5][0],                                     (paramsplit[6][4][0] == 3) ? floatmsg : paramsplit[6][0],                                     (paramsplit[7][4][0] == 3) ? floatmsg : paramsplit[7][0],                                     (paramsplit[8][4][0] == 3) ? floatmsg : paramsplit[8][0],                                     (paramsplit[9][4][0] == 3) ? floatmsg : paramsplit[9][0])                                 log_amx("[debug] Message '%s' has been formatted!", message)                             }                         }                         client_print(0, print_chat, "%s", message, paramsplit[3][0], paramsplit[4][0])                         log_amx("[debug] Message '%s' has been printed into chat", message, paramsplit[3][0], paramsplit[4][0])                     }                     else                         log_amx("[debug] Something went wrong and the loop stopped at argument %d.", argument)                 }             }         }     } }


Error: Array must be indexed (variable "strmsg") on line 180
Line 180:
Code:
formatex(message, charsmax(message), _fileData[i][10], (paramsplit[3][4][0] == 3) ? floatmsg[3] : (paramsplit[3][4][0] == 2) ? nummsg[3] : strmsg[3])

The other cases are not modified.. tried a lot of things, that's why the other case looks diferent (3 to 7)

LE: I know.. there are a lot of useless variables, because I tried a lot of things ^.^. I'll clean up my code after I fix this error.


All times are GMT -4. The time now is 14:22.

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