Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-06-2007, 03:59 PM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Efficiency!

Debate between Kuji and I :Which method is more efficient? My example first then his.

PHP Code:
function ChatBar.onAction()
{
  if (
ChatBar.text != "")
  {
    
temp.toks ChatBar.text.tokenize();
    
temp.args temp.toks;
    
temp.args.delete(0);
    switch(
temp.toks[0])
    {
    
/* Clientside Commands */
      
case "/tailor":
        
Tailor.showTailor();
        return;
      case 
"/reconnect":
        
serverwarp("Theed");
        return;

        ... 

        
/* Serverside Commands */
        
case "mass":
        case 
"admin":
        case 
"test":
          
          ...
          
          
triggerserver("gui"this.nametemp.toks[0], temp.args);
          return;
    }
  }

Clientside commands would be done before the triggerserver while all serverside commands will be triggered.

Compared to:

PHP Code:
function ChatBar.onAction()
{
  if (
ChatBar.text.starts("/"))
  {
    
temp.toks ChatBar.text.tokenize();
    
    
temp.args temp.toks;
    
temp.args.delete(0);
    
    if (
temp.toks[0].substring(1in this.clientcmd)
    {
      
onDoClientCommand(temp.toks[0].substring(1), temp.args);
    }
      else
    {
      
triggerserver("gui"this.nametemp.toks[0], temp.args);
    }
    
    
ChatBar.text "";
  }
}

function 
onDoClientCommand(actionargs)
{
  switch (
action)
  {
    case 
"reconnect":
    {
      
serverwarp("Theed");
    
      return 
true;
    }
  } 
The battle here is between switches and if/else and index() . My method may look longer now but his would have alot of clientside commands as well.

Of course a possibility might be to switch the clientside commands. If not there, check if the command is in an array of serverside commands: if so, serverside. Else displays an error message This may server only make the script look nicer and may have no benefits in efficiency

Last edited by Twinny; 01-06-2007 at 04:11 PM..
Reply With Quote
  #2  
Old 01-06-2007, 05:54 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
I like Example 1 more, and Id say its the most efficient way to do it.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote
  #3  
Old 01-06-2007, 06:25 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
I think example 1 looks more orginazed, and most efficient.
__________________
Reply With Quote
  #4  
Old 01-06-2007, 08:55 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
In all technicality, example 2 is the most efficient in that it is not evaluating everything that is passed from ChatBar. However, it could still be improved, such as calling temp.toks[0].substring(1) only once and using a variable the multiple times it is needed. In all actuality, the substring isn't necessary.

Another note is I don't think example 2 is working like it is supposed to be, but that is merely speculation. If the serverside commands do not start with "/", they are not being evaluated in that example, and if they do begin with "/", then the "/" is being passed along to the server. Doesn't make much of a difference in script performance, but I must remind that removing the "/" from the command on the serverside as well is silly.

If example 1 could be tweaked to filter out non-commands from ChatBar, I would go with that one.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #5  
Old 01-06-2007, 09:08 PM
Crono Crono is offline
:pluffy:
Join Date: Feb 2002
Location: Sweden
Posts: 20,000
Crono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond repute
well, which one takes the least CPU usage?

yeah im a noob whatever
__________________
Reply With Quote
  #6  
Old 01-06-2007, 09:54 PM
excaliber7388 excaliber7388 is offline
Banned
excaliber7388's Avatar
Join Date: Jul 2005
Location: US
Posts: 5,229
excaliber7388 can only hope to improve
Send a message via AIM to excaliber7388
Not that bad of a question though, I've made scripts that increased actual cu usage.
Reply With Quote
  #7  
Old 01-07-2007, 04:30 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Quote:
Originally Posted by Tolnaftate2004 View Post
In all technicality, example 2 is the most efficient in that it is not evaluating everything that is passed from ChatBar.
When you think about it, it's very similar to if/else. Starts at the first case and checks. If it's the case, it performs it then ends the whole script. If not, goes down. Once it meets it's case, doesn't have to evaluate any others.For the serverside part, it's just reading the case "example": but i'm not even sure if it's doing that =p
Reply With Quote
  #8  
Old 01-07-2007, 05:05 AM
Rapidwolve Rapidwolve is offline
Registered User
Join Date: Jul 2006
Posts: 1,241
Rapidwolve is an unknown quantity at this point
For more options, I would go for #1, for less #2
Reply With Quote
  #9  
Old 01-07-2007, 05:14 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Twinny View Post
When you think about it, it's very similar to if/else. Starts at the first case and checks. If it's the case, it performs it then ends the whole script. If not, goes down. Once it meets it's case, doesn't have to evaluate any others.For the serverside part, it's just reading the case "example": but i'm not even sure if it's doing that =p
I know what switch does, and I would argue that it's not particularly similar to if/else.

What I was getting at is that say ChatBar calls onAction. The second one is weeding out right off the bat all parameters from ChatBar that could never be the case. Option one loops through each of the cases for everything passed from ChatBar, regardless of if it might be a case. That is not particularly desired; it runs up CPU usage.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #10  
Old 01-07-2007, 05:24 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Thus the major thing I've been wondering about. I was thinking cases would be similar to checking an array for a match since it's only comparing the case "blah": line. I think it would be very close in terms of efficiency. You could argue that once a match is found serverside, it has to go through all cases below it but does it actually process cases since it has found it's match already?

Also, my method doesn't send incorrect commands serverside . Kuji would need another .index() for this thus less efficiency clientside .
Reply With Quote
  #11  
Old 01-07-2007, 05:32 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Twinny View Post
Thus the major thing I've been wondering about. I was thinking cases would be similar to checking an array for a match since it's only comparing the case "blah": line. I think it would be very close in terms of efficiency. You could argue that once a match is found serverside, it has to go through all cases below it but does it actually process cases since it has found it's match already?

Also, my method doesn't send incorrect commands serverside . Kuji would need another .index() for this thus less efficiency clientside .
No, it stops processing cases after it has found a match and runs the code held therein regardless of case (that is, without breaking the loop). I agree in that they are close in efficiency, however from the post, #2 is still more efficient. And as I stated in my first post, simple changes could make both even better, eventually making #1 the more efficient.

An array would do the trick.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #12  
Old 01-07-2007, 05:53 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Maybe this?

PHP Code:
function ChatBar.onAction()
{
  if (
ChatBar.text.starts("/"))
  {
    
temp.toks ChatBar.text.tokenize();
    
    
temp.args temp.toks;
    
temp.args.delete(0);
    switch(
temp.toks[0])
    {
       case 
"client1":
         
//stuff
         
return;
       case 
"client2":
         
//stuff
         
return;
       case 
"client3":
         
//stuff
         
return;
    }
    
triggerserver("gui"this.nametemp.toks[0], temp.args);
  }

Kuji made the point that there could be a fair amount of serverside commands so a case "": for all of them could get tedious...
Reply With Quote
  #13  
Old 01-07-2007, 06:07 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Cases are a lot cleaner and easier to modify, especially when you want multiple cases doing the same thing, without the need of calling the same function multiple times.
Reply With Quote
  #14  
Old 01-07-2007, 06:35 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Twinny View Post
PHP Code:
switch(temp.toks[0])
{
  
//blah
}
triggerserver("gui"this.nametemp.toks[0], temp.args); 
That, or you could do

PHP Code:
switch(temp.toks[0]) {
  
// blah
  
default:
    
triggerserver(so-and-so);
    return;

__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #15  
Old 01-07-2007, 07:54 AM
Angel_Light Angel_Light is offline
Varia Developer
Angel_Light's Avatar
Join Date: Nov 2005
Location: Knoxville, TN
Posts: 1,684
Angel_Light is on a distinguished road
Send a message via AIM to Angel_Light Send a message via MSN to Angel_Light
Combination of the two XD
__________________
Deep into the Darkness peering...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 +2. The time now is 10:22 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.