AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Checking Syntax (https://forums.alliedmods.net/showthread.php?t=17521)

SentryIII 09-03-2005 02:39

Checking Syntax
 
Could someone show me a more efiicient way to write the following conditional statement?

Code:
if (!equali(check, "somevalue")||!equali(check, "somevalue")||!equali(check, "somevalue")||!equali(check, "somevalue")||!equali(check, "somevalue")||!equali(check, "somevalue")||!equali(check, "somevalue"))   return PLUGIN_HANDLED

SentryIII 09-03-2005 23:07

Reason I ask is because Im trying to block certain commands through amx_rcon and it seems to be blocking everything even if its not one of the items here.

pdoubleopdawg 09-03-2005 23:13

if(!equali(a,b)) means if it's not this command.
Remove the !

There's not much you can do to make it shorter. Maybe make a stock for it, but I don't think it's worth simplifying a few characters shorter.

knekter 09-03-2005 23:18

well you could do something like:
Code:
new const g_szCommands[4][] = {     "Command1",     "Command2",     "Command3",     "Command4" }; for(new iCmd = 0; iCmd < 4; iCmd++)     if(equali(check, g_szCommands[iCmd]))         return PLUGIN_HANDLED;
Or something like that to make the code look cleaner/nicer.

colby 09-03-2005 23:25

While we're on the subject, those || between the statements mean 'or' right?

knekter 09-03-2005 23:27

Quote:

Originally Posted by colby
While we're on the subject, those || between the statements mean 'or' right?

yes

SentryIII 09-03-2005 23:34

Quote:

Originally Posted by knekter
well you could do something like:
Code:
new const g_szCommands[4][] = {     "Command1",     "Command2",     "Command3",     "Command4" }; for(new iCmd = 0; iCmd < 4; iCmd++)     if(equali(check, g_szCommands[iCmd]))         return PLUGIN_HANDLED;
Or something like that to make the code look cleaner/nicer.

Perfect, thankyou!
Just for the record, Im listing the commands Im allowing, not trying to block. I decided that trying to block every possible rcon command would take much more time/space than just listing a few commands.

SentryIII 09-04-2005 00:57

Ok, this is what Im using and it does not see to be working. It seems to be blocking every command.
Code:
  new const cmd[5][] =   {       "changelevel",       "exec",       "kick",       "sv_password",       "sv_restart"   }   for(new i = 0; i < 5; i++)     if(!equali(check,cmd[i]))       return PLUGIN_HANDLED

I have just realised there is no OR (||) conditions using this. So basically if the argument isnt changelevel it stops there. This is not what I want.

I may have a solution to this by counting the total number of commands here and checking to see if any of them matched by checking totals, kinda like a tally system. For example since I have 5 possible commands here I can do a check at the end of the "for" loop to see if it counted 5 mismatches and if so, then to stop there.

Heres my question:
To do what I mentioned above I was wondering if there was a way to count how many items are in my array and replacing the 5 with that variable.

jsauce 09-04-2005 06:00

What about something like this.
Code:
new const cmd[5][] =     {         "changelevel",         "exec",         "kick",         "sv_password",         "sv_restart"     }     for(new i = 0; i < 5; i++)     {         if(!equali(check,cmd[i])) continue                 if (equal(check,cmd[i]))         {             server_cmd("%s",cmd[i])             break         }     }     return PLUGIN_HANDLED

Xanimos 09-04-2005 10:58

your supposed to use it like so:
Code:
#include <amxmodx> new const cmd[5][] =     {         "changelevel",         "exec",         "kick",         "sv_password",         "sv_restart"     } public plugin_init() { //.... } public YourFunction() {     for(new i = 0; i < 5; i++)     {         if(!equal(check,cmd[i])) continue                 if (equal(check,cmd[i]))         {             server_cmd("%s",cmd[i])             break         }     } }


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

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