Raised This Month: $51 Target: $400
 12% 

Is it possible to optimize this code?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Lukass
Junior Member
Join Date: Dec 2008
Old 01-04-2009 , 12:32   Is it possible to optimize this code?
Reply With Quote #1

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?
Lukass is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 01-04-2009 , 12:36   Re: Is it possible to optimize this code?
Reply With Quote #2

Quote:
Originally Posted by Lukass View Post
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).
__________________

Community / No support through PM
danielkza is offline
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 01-04-2009 , 13:15   Re: Is it possible to optimize this code?
Reply With Quote #3

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.
__________________
Bad_Bud is offline
Lukass
Junior Member
Join Date: Dec 2008
Old 01-04-2009 , 14:48   Re: Is it possible to optimize this code?
Reply With Quote #4

Thanks dude, i want exacaly it.
Lukass is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 01-04-2009 , 15:39   Re: Is it possible to optimize this code?
Reply With Quote #5

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.
SnoW is offline
Send a message via MSN to SnoW
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 01-05-2009 , 11:08   Re: Is it possible to optimize this code?
Reply With Quote #6

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;

__________________

anakin_cstrike is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 01-06-2009 , 17:01   Re: Is it possible to optimize this code?
Reply With Quote #7

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.
__________________
No support via PM.

Last edited by Lee; 01-06-2009 at 17:13.
Lee is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 01-06-2009 , 17:36   Re: Is it possible to optimize this code?
Reply With Quote #8

But if you hook them to the same function, you would have to read the command to figure out which to display.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 01-06-2009 , 17:53   Re: Is it possible to optimize this code?
Reply With Quote #9

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.
__________________
No support via PM.
Lee is offline
Reply



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 19:14.


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