Check to see if something is in an array
#1

I am creating a vehicle thing. I need to check to see if the players vehicle that they are in is on an array. please help me

this is what i have so far.

array:
Code:
new RandomRegularVehicle[][3] =
{
    // Positions, (model, low price, high price)
    {400, 20000, 21000},
    {401, 18000, 19000},
    {404, 18000, 19000},
    {405, 19000, 20000},
    {410, 16000, 17000},
    {412, 21000, 22000},
    {418, 14000, 15000},
    {419, 15000, 16000},
    {421, 17000, 18000},
    {422, 22000, 23000},
    {426, 20000, 21000},
    {436, 16000, 17000},
    {439, 21000, 22000},
    {445, 19000, 20000},
    {458, 21000, 22000},
    {466, 23000, 24000},
    {467, 23000, 24000},
    {474, 25000, 26000},
    {475, 21000, 22000},
    {479, 19000, 20000},
    {489, 23000, 24000},
    {491, 17000, 18000},
    {492, 18000, 19000},
    {500, 24000, 26000},
    {505, 22000, 24000},
    {507, 18000, 21000},
    {516, 19000, 22000},
    {517, 16000, 20000},
    {518, 17000, 21000},
    {526, 20000, 24000},
    {527, 20000, 24000},
    {529, 18000, 22000},
    {533, 24000, 27000},
    {534, 19000, 23000},
    {535, 25000, 32000},
    {536, 24000, 31000},
    {540, 19000, 22000},
    {542, 16000, 21000},
    {543, 20000, 24000},
    {545, 24000, 30000},
    {546, 19000, 22000},
    {547, 19000, 22000},
    {549, 17000, 22000},
    {550, 23000, 26000},
    {551, 21000, 26000},
    {554, 25000, 34000},
    {558, 24000, 26000},
    {560, 28000, 37000},
    {561, 23000, 35000},
    {562, 27000, 35000},
    {565, 32000, 37000},
    {566, 19000, 24000},
    {567, 24000, 32000},
    {575, 22000, 26000},
    {576, 21000, 24000},
    {579, 26000, 33000},
    {580, 21000, 24000},
    {585, 21000, 26000},
    {587, 32000, 43000},
    {589, 27000, 35000},
    {600, 23000, 31000},
    {602, 32000, 42000}
};
Check to see if its on there:
Code:
if(CarInfo[idx][cModel] != RandomRegularVehicle[][0]) return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");
please help me
Reply
#2

Quote:
Originally Posted by jeffery30162
View Post
Code:
if(CarInfo[idx][cModel] != RandomRegularVehicle[][0]) return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");
Do that in a for loop for the max vehicle count.
Reply
#3

Im confused, could you show me what your talking about?
Reply
#4

pawn Code:
for (new i = 0; i < sizeof(RandomRegularVehicle); i++)
        if(CarInfo[idx][cModel] != RandomRegularVehicle[i][0]) return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");
Reply
#5

That is incorrect. If the first vehicle doesn't match, it will return the error without even checking the rest of the array. Never return inside a loop unless you explicitly want to break it.
Reply
#6

Quote:
Originally Posted by Vince
View Post
That is incorrect. If the first vehicle doesn't match, it will return the error without even checking the rest of the array. Never return inside a loop unless you explicitly want to break it.
Sorry, you are correct of course. You'd have to break the loop when there is a match. Then after the loop you'd have to check if the index is the size of the array. If not, there was a match so you can sell it, if it is the maximum, you can't.
pawn Code:
new i;
for (i = 0; i < sizeof(RandomRegularVehicle); i++)
    if(CarInfo[idx][cModel] == RandomRegularVehicle[i][0]) break;
if (i == sizeof(RandomRegularVehicle))
    return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");
Reply
#7

That didn't work either.
Reply
#8

pawn Code:
bool:IncorrectModel(modelid)
{

    for(new i=0; i != sizeof(RandomRegularVehicle); i++)
        if(RandomRegularVehicle[i][0] == modelid)
            return true;

    return false;
}

if(!IncorrectModel(CarInfo[idx][cModel])) return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");
Reply
#9

That didn't work either
Reply
#10

pawn Code:
new bool:modelfound = false;
for(new i = 0; i < sizeof(RandomRegularVehicle); i++)
{
    if(CarInfo[idx][cModel] != RandomRegularVehicle[i][0]) continue;
    modelfound = true;
    break;
}
if(!modelfound) return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)