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

Shadow.

  • Tester
  • Full Member
  • ****
  • Posts: 144
  • Karma: +16/-35
    • View Profile
Efficient Scripting..
« on: October 17, 2012, 12:41:54 pm »
This method that I've learned from AdTec shows a nice way to reduce else blocks memory 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.

Thijn

  • Tester
  • Sr. Member
  • ****
  • Posts: 531
  • Karma: +27/-16
    • View Profile
Re: Efficient Scripting..
« Reply #1 on: October 17, 2012, 12:48:55 pm »
I just use different functions for different kind of commands.
RegisterCommands, AdminCommands, RPGCommand, FuelCommands etc. etc.

Shadow.

  • Tester
  • Full Member
  • ****
  • Posts: 144
  • Karma: +16/-35
    • View Profile
Re: Efficient Scripting..
« Reply #2 on: October 17, 2012, 12:51:44 pm »
That's also good.. This just keeps everything sorted into a single function with lag / memory usage reduction, it's best to sort the cases in alphabetical order.

Juppi

  • Developer
  • Jr. Member
  • *****
  • Posts: 86
  • Karma: +3/-1
    • View Profile
    • Kuslahden alaste GTA:MP clan
Re: Efficient Scripting..
« Reply #3 on: October 17, 2012, 02:45:26 pm »
Checking the first character of a command will be a big help performance-wise when your script contains lots of commands. There is also a way to optimise the code even further, and it also makes it a bit simpler.

Strings in Squirrel behave much like strings in C. They're basically arrays of characters, and so you can refer to each character by using the array syntax. To get the first character in a string you would do string[0]. This lets us get rid of the relatively expensive slice() call, and each case will become a single character instead of a string.

Notice the difference - 'a' is the character a while "a" is a string containing the text a. The first one will use less memory and comparing characters will always be cheaper than comparing strings.

Code: [Select]
function onPlayerCommand( player, cmd, params )
{
switch ( cmd[0] ) // Check the first character of the command
{
case 'e': // Commands starting with 'e'
if ( cmd == "examplecmd" )
{
Message("Example command1");
}
else if ( cmd == "examplecmd2" )
{
Message("Example command2");
}
break;

case 'b': // Commands starting with 'b'
if ( cmd == "blah" )
{
Message("blahblahblah");
}
break;
}

return 1; // Returning is not really required but its a good practice to do so
}

Edit: Removed the validity check for player and cmd, LU guarantees that those two will always exist (however that's not always the case with params)
« Last Edit: October 17, 2012, 02:52:59 pm by Juppi »

Shadow.

  • Tester
  • Full Member
  • ****
  • Posts: 144
  • Karma: +16/-35
    • View Profile
Re: Efficient Scripting..
« Reply #4 on: October 17, 2012, 04:04:38 pm »
That's good to know!

 

© Liberty Unleashed Team.