SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 14/05/2014, 09:57 PM   #1
jeffery30162
Huge Clucker
 
Join Date: Jun 2012
Posts: 312
Reputation: 10
Default Check to see if something is in an array

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
jeffery30162 is offline   Reply With Quote
Old 14/05/2014, 10:32 PM   #2
Madd92
Big Clucker
 
Join Date: Apr 2013
Location: Germany
Posts: 61
Reputation: 1
Default Re: Check to see if something is in an array

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.
Madd92 is offline   Reply With Quote
Old 14/05/2014, 10:46 PM   #3
jeffery30162
Huge Clucker
 
Join Date: Jun 2012
Posts: 312
Reputation: 10
Default Re: Check to see if something is in an array

Im confused, could you show me what your talking about?
jeffery30162 is offline   Reply With Quote
Old 14/05/2014, 11:09 PM   #4
Madd92
Big Clucker
 
Join Date: Apr 2013
Location: Germany
Posts: 61
Reputation: 1
Default Re: Check to see if something is in an array

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");
Madd92 is offline   Reply With Quote
Old 15/05/2014, 08:32 AM   #5
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,067
Reputation: 2660
Default Re: Check to see if something is in an array

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.
__________________
Vince is offline   Reply With Quote
Old 15/05/2014, 08:31 PM   #6
Madd92
Big Clucker
 
Join Date: Apr 2013
Location: Germany
Posts: 61
Reputation: 1
Default Re: Check to see if something is in an array

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");
Madd92 is offline   Reply With Quote
Old 15/05/2014, 10:55 PM   #7
jeffery30162
Huge Clucker
 
Join Date: Jun 2012
Posts: 312
Reputation: 10
Default Re: Check to see if something is in an array

That didn't work either.
jeffery30162 is offline   Reply With Quote
Old 16/05/2014, 12:45 AM   #8
Jefff
Banned
 
Join Date: Dec 2007
Posts: 2,593
Reputation: 326
Default Re: Check to see if something is in an array

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");
Jefff is offline   Reply With Quote
Old 17/05/2014, 06:05 PM   #9
jeffery30162
Huge Clucker
 
Join Date: Jun 2012
Posts: 312
Reputation: 10
Default Re: Check to see if something is in an array

That didn't work either
jeffery30162 is offline   Reply With Quote
Old 17/05/2014, 11:37 PM   #10
Threshold
High-roller
 
Threshold's Avatar
 
Join Date: Nov 2012
Location: Australia
Posts: 3,122
Reputation: 529
Default Re: Check to see if something is in an array

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");
__________________


Kar's Cops and Robbers 2020
Threshold is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check, if anything is in array? Micius Scripting Help 1 12/07/2013 09:47 AM
(dFile) error 047: array sizes do not match, or destination array is too small Jack_Leslie Scripting Help 0 06/09/2011 01:20 AM
(dFile) error 047: array sizes do not match, or destination array is too small Jack_Leslie Scripting Help 8 01/09/2011 05:32 AM
[SOLVED]error 047: array sizes do not match, or destination array is too small help! Help Archive 7 18/06/2011 08:09 PM
Array Check Nonameman Help Archive 3 06/08/2010 12:16 AM


All times are GMT. The time now is 11:40 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.