(who's the best) repeat command or not?
#1

i have one doubt! who's the best? repeat command or use OnPlayerCommandText

example:

pawn Код:
public OnPlayerCommandText(playerid,cmdtext[])
{
    if(!strcmp(cmdtext,"/command",true))
    {
        //command...
    }
    return 0;
}
and now, for example, in OnPlayerKeyStateChange. who's the best? write the /command again
like this
pawn Код:
public OnPlayerKeyStateChange(playerid,newkeys,oldkeys)
{
    if(PRESSED(KEY_FIRE))
    {
        //command...
    }
    return 1;
}
or use OnPlayerCommandText ?
like this
pawn Код:
public OnPlayerKeyStateChange(playerid,newkeys,oldkeys)
{
    if(PRESSED(KEY_FIRE))
    {
        OnPlayerCommandText(playerid,"/command");
    }
    return 1;
}
Reply
#2

Well the former is more efficient (the second snippet). This is simply because less work has to be done to get to the code's execution. In the latter (the third snippet), a string comparison is done (which is redundant because at that stage you already know for a fact what code you want to execute) and depending on how far down the command is along a line of other commands in the script, it will take even longer to get down through all of the if statements.

The best way to avoid repeating code is to simply use a method (or a function) which contains the code that you need to run. For example:

pawn Код:
stock commandMethod()
{
    // Command
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(PRESSED( KEY_FIRE )) commandMethod();
    return 1;
}

public OnPlayerCommandText(playerid,cmdtext[])
{
    if(!strcmp(cmdtext,"/command",true)) commandMethod();
    return 0;
}
This way there are no redundant checks and you are avoiding having to copy and paste the same code. Not to mention if you want to change what the command does, you only have to change it in one place (the commandMethod() function).

Hope that answers your question!
Reply
#3

well, think i understand. Thank you
now, please, explain to me something.
what's the difference betwen unse stock commandMethod and use forward commandMethod(); and public commandMethod()
Reply
#4

Public functions can be used in timers, stock functions can't.
Reply
#5

Well typically you would use the public functions as callbacks, they can be called from other scripts using CallRemoteFunction and can also be called using timers.

For this situation, you would only need to use a normal function rather than a public one.
Reply
#6

Quote:
Originally Posted by spedico
Посмотреть сообщение
Public functions can be used in timers, stock functions can't.
i know
Quote:
Originally Posted by JaTochNietDan
Посмотреть сообщение
Well typically you would use the public functions as callbacks, they can be called from other scripts using CallRemoteFunction and can also be called using timers.

For this situation, you would only need to use a normal function rather than a public one.
hmm, i understand. Now, i understand

Thanks for all
Reply
#7

For example, when I use a function like GetPlayerID in on include, is better if use stock, becase is a 'library function' because i don't use always, but sometimes. But in this situation is better no use a prefix because certain i use! right?
Reply
#8

That is correct. I should have just put it as a normal function really, it doesn't make a huge difference but generally you would use stock for functions that you're not sure are going to be used. It's mostly used for libraries (includes) as the people making use of the library may not make use of all of the functions.

When you are sure a function is going to be used though, there is no need for the stock prefix. So in your case you don't need the stock prefix as you know for a fact that you are going to be using that function.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)