Amount of items in an array.
#1

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.
Reply
#2

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
Reply
#3

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.
Reply
#4

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?
Reply
#5

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.
Reply
#6

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); 
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)