Array Problem
#1

Hey!

I got a problem with anti-fall on bike problem..

I got all the ID's of bikes in new array

PHP код:
new Bikes[12] = //Bikes ID's
{
    
448461462463468481509510,
    
521522581586
}; 
Then I got

PHP код:
public OnPlayerStateChange(playeridnewstateoldstate)
{
    if(
oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
    {
        if(
GetVehicleModel(pVeh[playerid]) == sizeof(Bikes)) return PutPlayerInVehicle(playeridpVeh[playerid], 0);
return 
1;

But for some reason if I put for example "522" which is NRG model id instead of "sizeof(Bikes)" it works.
Reply
#2

That's not how it works, if you want to check if the array contains the number, you have to run a loop and check for each one like this:

Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
    {
        for(new i; sizeof(Bikes); i++)
        {
            if(GetVehicleModel(pVeh[playerid]) != Bikes[i]) continue;
            return PutPlayerInVehicle(playerid, pVeh[playerid], 0);
        }
    }
    return 1;
}
Reply
#3

Gonna try
Reply
#4

I just edited the code, indentation got fucked up randomly, plus I didn't notice a missing brace (original post)
Reply
#5

pawn Код:
isBike(vehicleModel) {
    for (int i = 0; i < sizeof(Bikes); i++) {
        if (Bikes[i] == vehicleModel) {
            return true;
        }
    }
    return false;
}
Or simply use this function. Implement it to your code:

pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
    {
        if(isBike(GetVehicleModel(pVeh[playerid]))) PutPlayerInVehicle(playerid, pVeh[playerid], 0);
    }
    return 1;
}
This way you can also use it on different occasions.
Reply
#6

Quote:
Originally Posted by Private200
Посмотреть сообщение
pawn Код:
public isBike(int vehicleModel) {
    for (int i = 0; i < sizeof(Bikes); i++) {
        if (Bikes[i] == vehicleModel) {
            return true;
        }
    }
    return false;
}
Or simply use this function. Implement it to your code:

pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
    {
        if(isBike(GetVehicleModel(pVeh[playerid]))) {
            PutPlayerInVehicle(playerid, pVeh[playerid], 0);
        }
    }
    return 1;
}
This way you can also use it on different occasions.
Код:
public isBike(int vehicleModel)
What is that? Just use a plain function... Plus it's pawn, not C++ or any other programming language, you don't need to add the "int". And why using braces for just a "return true" :c ?
Reply
#7

My bad. Been a while since I last coded in Pawn. Edited.
Reply
#8

Still wrong, there is no need for "public"
Reply
#9

Thanks for teaching me senpai.
Reply
#10

Quote:
Originally Posted by Private200
Посмотреть сообщение
Thanks for teaching me senpai.
Test your codes before giving false information, student.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)