AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Is it possible to optimize this code? (https://forums.alliedmods.net/showthread.php?t=83116)

Lukass 01-04-2009 12:32

Is it possible to optimize this code?
 
Code:


    register_clcmd("say /noriuvip","noriu_vip",0,"- Jeigu nori VIP")
    register_clcmd("say /viprules","vip_rules",0,"- VIP taisykles")
    register_clcmd("say /rules","rules",0,"- Taisykles")


public noriu_vip(id) {

   
    show_motd(id,"noriuvip.txt","Nori VIP?")
 
}

public vip_rules(id) {

   
    show_motd(id,"viprules.txt","VIP taisykles")
 
}



public rules(id) {

   
    show_motd(id,"rules.txt","Taisykles")
 
}

It`s possible to use one function instead these three?

danielkza 01-04-2009 12:36

Re: Is it possible to optimize this code?
 
Quote:

Originally Posted by Lukass (Post 738267)
Code:


    register_clcmd("say /noriuvip","noriu_vip",0,"- Jeigu nori VIP")
    register_clcmd("say /viprules","vip_rules",0,"- VIP taisykles")
    register_clcmd("say /rules","rules",0,"- Taisykles")


public noriu_vip(id) {

   
    show_motd(id,"noriuvip.txt","Nori VIP?")
 
}

public vip_rules(id) {

   
    show_motd(id,"viprules.txt","VIP taisykles")
 
}



public rules(id) {

   
    show_motd(id,"rules.txt","Taisykles")
 
}

It`s possible to use one function instead these three?

Yes, but it would make the code bigger, not smaller, and would bring you no benefit unless there is some common logic among the 3 commands (actually there's no logic at all).

Bad_Bud 01-04-2009 13:15

Re: Is it possible to optimize this code?
 
PHP Code:

register_clcmd("say","rules")
 
public 
rules(id)
{
     new 
arg[33]
     
read_argv(1,arg,32)
 
     if(
equali(arg,"/noriuvip"))
     {
          
show_motd(id,"noriuvip.txt","Nori VIP?")
     }
     else if(
equali(arg,"/viprules"))
     {
          
show_motd(id,"viprules.txt","VIP taisykles")
     }
     else if(
equali(arg,"/rules"))
     {
          
show_motd(id,"rules.txt","Taisykles")
     }
     else
     {
           return 
PLUGIN_CONTINUE
     
}
 
     return 
PLUGIN_HANDLED


Not sure how/what you're doing with flags on the clcmd, because I've never used them, but here's what I would do if you had to have it in a single function to satisfy yourself.

If the flags meant anything important, you'll have to add another check to the if statements.

Lukass 01-04-2009 14:48

Re: Is it possible to optimize this code?
 
Thanks dude, i want exacaly it.

SnoW 01-04-2009 15:39

Re: Is it possible to optimize this code?
 
That way it will check every single command or text what you say to chat. Ofc it won't matter if you do it only in this plugin. But just notice that if you have 20 plugins that checks all say commands: Always when someone says something to chat it will run to 20 function. That's why it's -well someone would say- better to use exact chat checks.

anakin_cstrike 01-05-2009 11:08

Re: Is it possible to optimize this code?
 
Or try this, i like it more
PHP Code:

#include <amxmodx>

new g_Commands[ ][ ] =
{
    
"/noriuvip",
    
"/viprules",
    
"/rules"
};

new 
g_Motds[ ][ ] =
{
    
"noriuvip.txt",
    
"viprules.txt",
    
"rules.txt"
};

new 
g_Messages[ ][ ] =
{
    
"Nori VIP?",
    
"VIP taisykles",
    
"Taisykles"
};

public 
plugin_init() 
{
    
register_clcmd"say""hook_say" );
    
register_clcmd"say_team""hook_say" );
}

public 
hook_sayid )
{
    new 
args32 ];
    
read_argsargscharsmaxargs ) );
    
remove_quotesargs );
    
    for( new 
0sizeof g_Commandsi++ )
    {
        if( 
equaliargsg_Commands] ) )
            
show_motdidg_Motds], g_Messages] );
    }
    
    return 
0;



Lee 01-06-2009 17:01

Re: Is it possible to optimize this code?
 
anakin_cstrike, if you iterate over g_Commands in plugin_init() and use said loop to call register_clclmd() and still retain one public fuction, you keep the extensibility without having to pass through the VM every time someone uses chat.

Exolent[jNr] 01-06-2009 17:36

Re: Is it possible to optimize this code?
 
But if you hook them to the same function, you would have to read the command to figure out which to display.

Lee 01-06-2009 17:53

Re: Is it possible to optimize this code?
 
Yes, of course, but he's currently doing the same thing whether it's a command we're interested in or someone talking about their cat that just got run over.


All times are GMT -4. The time now is 23:11.

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