SA-MP Forums Archive
Amount of items in an array. - 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: Amount of items in an array. (/showthread.php?tid=578288)



Amount of items in an array. - Jimmy0wns - 18.06.2015

So, this is something I've been stuck with for quite some time.

Lets say I have 5 items of vehicle information in my array. For example:
PHP код:
{4513.04.06.0220.01000040000}, 
{
5913.04.06.0220.04000010000}, 
{
5123.04.06.0220.03000060000}, 
{
4853.04.06.0220.06000030000}, 
{
4123.04.06.0220.09000090000
How would one get the amount of items in that array? I tried using sizeof(thisarray[]); but without success, it gave me a random number.


Re: Amount of items in an array. - Darrenr - 18.06.2015

Could this be done with foreach somehow? I've not used foreach much but im trying to figure it out using the standalone include - But i don't know how to use it that well


Re: Amount of items in an array. - Jimmy0wns - 18.06.2015

Quote:
Originally Posted by Darrenr
Посмотреть сообщение
Could this be done with foreach somehow? I've not used foreach much but im trying to figure it out using the standalone include - But i don't know how to use it that well
I've tried that, but it'll just result in a number which has been set as the max amount.

e.g: MAX_PLAYER_VEHICLES is set to 2000 and if you make a loop through that and save the result, it'll say 2000.


Re: Amount of items in an array. - RajatPawar - 18.06.2015

pawn Код:
new arr[][] =
    {
        {1, 2, 3, 4},
        {3, 4, 6, 6}
    };
   
    printf("%d %d", sizeof(arr), sizeof(arr[]));
Gave me results as 2 & 4.
Why didn't it work with yours?


Re: Amount of items in an array. - Jimmy0wns - 18.06.2015

Quote:
Originally Posted by RajatPawar
Посмотреть сообщение
pawn Код:
new arr[][] =
    {
        {1, 2, 3, 4},
        {3, 4, 6, 6}
    };
   
    printf("%d %d", sizeof(arr), sizeof(arr[]));
Gave me results as 2 & 4.
Why didn't it work with yours?
Probably because I do this all in a very different way, I use enums as well but I just couldn't think of the right terms when posting this topic.

I usually make an enum like this:
PHP код:
enum _VehicleData
{
    
vID,
    
vOwner[128],
    
vLocation[128],
    
    
vModel,
    
Float:vPosX,
    
Float:vPosY,
    
Float:vPosZ,
    
Float:vPosAngle,
    
vColor1,
    
vColor2,
    
    
vPrice,
    
vDefaultPrice,
    
vForSale,
    
    
Text3D:vText,
    
curVehID,
}; 
And then use this:
PHP код:
new VehicleData[MAX_VEHICLES][_VehicleData]; 
Whereas the items for this array get loaded with a MySQL query when the server starts.


Re: Amount of items in an array. - Jimmy0wns - 18.06.2015

Solved it by using this.
PHP код:
new total;
    
for(new 
ii<sizeof(VehicleData); i++)
{
    if(
VehicleData[i][vModel] == 0)
    {
        
total i;
        break;
    }
}
printf("%d"total);