SA-MP Forums Archive
What is the best way? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: What is the best way? (/showthread.php?tid=502084)



What is the best way? - Vanchesta - 22.03.2014

Hello,

I came up here with a question. What would you prefer to use? See codes below:

PHP код:
public OnPlayerStateChange(playeridnewstateoldstate)
{
    if(
newstate == PLAYER_STATE_DRIVER)
    {
        
#define vehicle GetPlayerVehicleID(playerid)
        
if(vehicle == Player[playerid][pCar]) SendClientMessage(playerid"It's your vehicle");
        
// codes to be used with vehicle
    
}
    return 
1;

PHP код:
public OnPlayerStateChange(playeridnewstateoldstate)
{
    if(
newstate == PLAYER_STATE_DRIVER)
    {
        new 
vehicle GetPlayerVehicleID(playerid);
        if(
vehicle == Player[playerid][pCar]) SendClientMessage(playerid"It's your vehicle");
        
// codes to be used with vehicle
    
}
    return 
1;

So point of my question is to know what is the best way to use, #define or new? Please don't criticize if it's really a silly question.


Re: What is the best way? - Hanuman - 22.03.2014

2nd one is best


Re: What is the best way? - MP2 - 23.03.2014

Quote:
Originally Posted by Hanuman
Посмотреть сообщение
2nd one is best
Why don't you explain to him why?


The first method is definately not the way to do things. In fact, you don't really need a variable at all, just do this if you only need it once:

pawn Код:
if(GetPlayerVehicleID(playerid) == Player[playerid][pCar]) SendClientMessage(playerid, "It's your vehicle");
@Vanchesta (OP): Please use [ pawn ] tags, not php.


Re: What is the best way? - Lloyde - 23.03.2014

try to use 2nd codes


Re: What is the best way? - MP2 - 23.03.2014

Quote:
Originally Posted by Lloyde
Посмотреть сообщение
try to use 2nd codes
There have been two replies. One saying exactly this, and mine, which gave a reason why. Was your post really necessary?


Re: What is the best way? - Hanuman - 23.03.2014

Quote:
Originally Posted by MP2
Посмотреть сообщение
There have been two replies. One saying exactly this, and mine, which gave a reason why. Was your post really necessary?
Yep it was necessary for increasing his post.


Re: What is the best way? - Vanchesta - 23.03.2014

Quote:
Originally Posted by MP2
Посмотреть сообщение
Why don't you explain to him why?


The first method is definately not the way to do things. In fact, you don't really need a variable at all, just do this if you only need it once:

pawn Код:
if(GetPlayerVehicleID(playerid) == Player[playerid][pCar]) SendClientMessage(playerid, "It's your vehicle");
@Vanchesta (OP): Please use [ pawn ] tags, not php.
No mate, I ain't gonna use it once, there will be multiple actions with it, that's why I added "codes to be used with vehicle" as a comment. Well, I noticed that if you use #define anywhere at script then it affects to whole code, so I decided to undefine it when the function ends. So what's better now?

pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(newstate == PLAYER_STATE_DRIVER)
    {
        #define vehicle GetPlayerVehicleID(playerid)
        if(vehicle == Player[playerid][pCar]) SendClientMessage(playerid, "It's your vehicle");
        // codes to be used with vehicle
        #undef vehicle
    }
    return 1;
}
or

pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(newstate == PLAYER_STATE_DRIVER)
    {
        new vehicle GetPlayerVehicleID(playerid)
        if(vehicle == Player[playerid][pCar]) SendClientMessage(playerid, "It's your vehicle");
        // codes to be used with vehicle
    }
    return 1;
}
P.S: Sorry if you don't understand me, I'm not best in speaking english at all.


Re: What is the best way? - Hanuman - 23.03.2014

answer is same, just replace the line new vehicle GetPlayerVehicleID(playerid) with new vehicle = GetPlayerVehicleID(playerid);


Re: What is the best way? - Vanchesta - 23.03.2014

Quote:
Originally Posted by ******
Посмотреть сообщение
Anwarrex: That's not even CLOSE to correct! Don't compare features in PAWN to features in other languages like that - they are different languages for a reason. "#define" is a pure text replacement, go read my pre-processor tutorial for more information.
If #define is for replacement only, so my code looks same as GetPlayerVehicleID(playerid)? Am I right? If so, I'm gonna keep using defines instead of creating a variable.


Re: What is the best way? - Konstantinos - 23.03.2014

If you want to use the vehicleid for more than 2 times, storing the vehicleid in a variable is the best option instead of calling GetPlayerVehicleID over and over again.