March 28, 2024, 08:52:54 am

Author Topic: Efficient Scripting..  (Read 1289 times)

0 Members and 1 Guest are viewing this topic.

Shadow

  • Guest
Efficient Scripting..
« on: October 17, 2012, 10:48:06 am »
Quote from: Shadow.
This method that I've learned from AdTec shows a nice way to reduce else blocks memory usage and this is how I do it, maybe results are not visible, but it's certainly an improvement.

Code: [Select]
function onPlayerCommand( player, cmd, params ) // the actual function
{ // the opening bracket
if( player && cmd ) // Check if player and command exist, not really mandatory..
{ // opening bracket of above 'if'


local delim = cmd.slice( 0, 1 ); // get the first letter from the "cmd" string
switch(delim) // create a switch in which we have our cases.
{// opening bracket of the switch

case "e": // first case

                       if( cmd == "examplecmd" )
                       {
                                 Message("Example command1");
                        }
                        else if( cmd == "examplecmd2" )
                         {
                                Message("Example command2");
                          }
                          break; // break first case to start another one, dunno if it's mandatory

                         case "b":
                                   if( cmd == "blah" )
                                   {
                                               Message("blahblahblah");
                                    }
                     break;
            } // end switch
} // end first bracket
} // end function bracket


Sorry for the awful identation.
Pwnt Pwnt x 1 (list)
Cannot rate own posts


Skirmant

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • Posts: 681
  • Country: il
  • Ignorance is bliss.
    • View Profile
Re: Efficient Scripting..
« Reply #1 on: October 17, 2012, 12:17:37 pm »
I don't think it makes it efficient, but a bit more categorized :p
The speeds of comparing 2 strings are so rapid and commands are used so rarely you simply won't notice any performance difference.

Events & timers is where serious memory is being used.
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

"C++ is a horrible language" - Linus Torvalds, creator of Linux

Quote
<SLC> nope. changed my mind. squirrel still sux dix
<SLC> it's just a piece of sh!t
* SLC has quit (Connection closed)