Any other methodes to do this?
#1

Hello guys, so I was wondering if there could be something that acronyms the long code lines which often gives errors when compiling

PHP код:
    new VehID GetPlayerVehicleID(playerid);
    if(
newstate == PLAYER_STATE_DRIVER && VehID == VIPVEH1 || VehID == VIPVEH2 || VehID == VIPVEH3 || VehID == VIPVEH4 || VehID == VIPVEH5 || VehID == VIPVEH6 || VehID == VIPVEH7 || VehID == VIPVEH8 || VehID == VIPVEH9 || VehID == VIPVEH10 || VehID == VIPVEH11 || VehID == VIPVEH12 || VehID == VIPVEH13 || VehID == VIPVEH14 || VehID == VIPVEH15 || VehID == VIPVEH16 || VehID == VIPVEH17 || VehID == VIPVEH18 || VehID == VIPVEH19 || VehID == VIPVEH20)){
        if(
VIPInfo[playerid][VIPLevel] > 0){
            
SendClientMessage(playeridYELLOW"[VIP]: Welcome to VIP Vehicle");
            return 
1;
        }
        else{
            
ErrorMessages(playerid1);
            
RemovePlayerFromVehicle(playerid);
        }
    } 
is there anything I can do to sum up that long line of code ?(newstate == Play.....)
Reply
#2

I would say, create an array with the cars and loop trough that array
Reply
#3

PHP код:
if(newstate == PLAYER_STATE_DRIVER && ContainInSeries(GetPlayerVehicleID(playerid),VIPVEH1,VIPVEH2,VIPVEH3,VIPVEH4,VIPVEH5,VIPVEH6,VIPVEH7,VIPVEH8,VIPVEH9,VIPVEH10,VIPVEH11,VIPVEH12,VIPVEH13,VIPVEH14,VIPVEH15,VIPVEH16,VIPVEH17,VIPVEH18,VIPVEH19,VIPVEH20){ 
    if(
VIPInfo[playerid][VIPLevel] > 0){ 
        
SendClientMessage(playeridYELLOW"[VIP]: Welcome to VIP Vehicle"); 
        return 
1
    } else { 
        
ErrorMessages(playerid1); 
        
RemovePlayerFromVehicle(playerid); 
    } 

ContainInSeries here:
https://sampforum.blast.hk/showthread.php?tid=610012
Reply
#4

Really, just use an array instead of creating twenty separate variables.

PHP код:
in_array(needle, const haystack[], size sizeof haystack, &index 0)
{
    for(new 
isizei++)
    {
        if(
haystack[i] == needle)
        {
            
index i;
            return 
true;
        }
    }
        
    return 
false;

Reply
#5

Quote:
Originally Posted by Vince
Посмотреть сообщение
Really, just use an array instead of creating twenty separate variables.

PHP код:
in_array(needle, const haystack[], size sizeof haystack, &index 0)
{
    for(new 
isizei++)
    {
        if(
haystack[i] == needle)
        {
            
index i;
            return 
true;
        }
    }
        
    return 
false;

man have no idea what that means, where could I find some good tuts about the content above?
Reply
#6

PHP код:
// global
new gVipVehicles[20];

// gm init
gVipVehicles[0] = CreateVehicle(...);
gVipVehicles[1] = CreateVehicle(...);
gVipVehicles[2] = CreateVehicle(...);
// ...
gVipVehicles[19] = CreateVehicle(...);

// OnPlayerStateChange or wherever
if(in_array(VehIDgVipVehicles))
{
    
SendClientMessage(playeridYELLOW"[VIP]: Welcome to VIP Vehicle"); 

Reply
#7

Quote:
Originally Posted by Vince
Посмотреть сообщение
PHP код:
// global
new gVipVehicles[20];
// gm init
gVipVehicles[0] = CreateVehicle(...);
gVipVehicles[1] = CreateVehicle(...);
gVipVehicles[2] = CreateVehicle(...);
// ...
gVipVehicles[19] = CreateVehicle(...);
// OnPlayerStateChange or wherever
if(in_array(VehIDgVipVehicles))
{
    
SendClientMessage(playeridYELLOW"[VIP]: Welcome to VIP Vehicle"); 

so it would be something like this?

PHP код:
       new VehID GetPlayerVehicleID(playerid);
    if(
newstate == PLAYER_STATE_DRIVER && (VehIDgVIPVehicles[37])){
         if(
VIPInfo[playerid][VIPLevel] > 0){
            
SendClientMessage(playeridYELLOW"[VIP]: Welcome to VIP Vehicle");
            return 
1;
        }
        else{
            
ErrorMessages(playerid1);
            
RemovePlayerFromVehicle(playerid);
        }
    } 
because it didn't work, the client message didn't showup neither ways
Reply
#8

Quote:
Originally Posted by Eoussama
Посмотреть сообщение
so it would be something like this?

PHP код:
       new VehID GetPlayerVehicleID(playerid);
    if(
newstate == PLAYER_STATE_DRIVER && (VehIDgVIPVehicles[37])){
         if(
VIPInfo[playerid][VIPLevel] > 0){
            
SendClientMessage(playeridYELLOW"[VIP]: Welcome to VIP Vehicle");
            return 
1;
        }
        else{
            
ErrorMessages(playerid1);
            
RemovePlayerFromVehicle(playerid);
        }
    } 
because it didn't work, the client message didn't showup neither ways
No. By entering the literal 37, you specifically specify that the message should only show when being the driver of the 38th vehicle.

PHP код:
IsVIPVehicle(vehicleid) {
    for(new 
isizeof(gVIPVehicles); ji++) {
        if(
vehicleid == gVIPVehicles[i]) {
            return 
true;
        }
    }
    return 
false;

You loop through all the VIP vehicles and check whether the vehicleid returned by GetPlayerVehicleID is equal to any of them. If so: return true, else: return false.

PHP код:
if(newstate == PLAYER_STATE_DRIVER && IsVIPVehicle(VehID)) {
    
// Code for when the player is in a VIP vehicle

The return value of IsVIPVehicle is then used in the if statement. The && requires both newstate == PLAYER_STATE_DRIVER and IsVIPVehicle(VehID) to be true before executing the code in the if's body.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)