Array Problem - 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: Array Problem (
/showthread.php?tid=664517)
Array Problem -
NoteND - 02.03.2019
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
{
448, 461, 462, 463, 468, 481, 509, 510,
521, 522, 581, 586
};
Then I got
PHP код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
if(oldstate == PLAYER_STATE_DRIVER && newstate == PLAYER_STATE_ONFOOT)
{
if(GetVehicleModel(pVeh[playerid]) == sizeof(Bikes)) return PutPlayerInVehicle(playerid, pVeh[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.
Re: Array Problem -
TheToretto - 02.03.2019
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;
}
Re: Array Problem -
NoteND - 02.03.2019
Gonna try
Re: Array Problem -
TheToretto - 02.03.2019
I just edited the code, indentation got fucked up randomly, plus I didn't notice a missing brace (original post)
Re: Array Problem -
Private200 - 02.03.2019
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.
Re: Array Problem -
TheToretto - 02.03.2019
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 ?
Re: Array Problem -
Private200 - 02.03.2019
My bad. Been a while since I last coded in Pawn. Edited.
Re: Array Problem -
TheToretto - 02.03.2019
Still wrong, there is no need for "public"
Re: Array Problem -
Private200 - 03.03.2019
Thanks for teaching me senpai.
Re: Array Problem -
TheToretto - 03.03.2019
Quote:
Originally Posted by Private200
Thanks for teaching me senpai.
|
Test your codes before giving false information, student.