SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 19/03/2016, 05:43 PM   #1
CalvinC
High-roller
 
CalvinC's Avatar
 
Join Date: Nov 2014
Location: Denmark
Posts: 1,852
Reputation: 114
Default Sizeof 2D array

I'm trying to loop through vehicles of the players job and unlocking them, so they're only locked for people without that job, right now i'm doing this:
Code:
// Garbage Truck Driver

JobVehicle[1][0] = AddStaticVehicleEx(408, 2182.3635, -1992.1925, 14.1022, 359.7592, 1, 1, 180000); // Trashmaster
JobVehicle[1][1] = AddStaticVehicleEx(408, 2187.0657, -1993.5066, 14.0923, 359.9882, 1, 1, 180000); // Trashmaster
JobVehicle[1][2] = AddStaticVehicleEx(408, 2192.1223, -1994.7883, 14.0956, 0.0472, 1, 1, 180000); // Trashmaster
JobVehicle[1][3] = AddStaticVehicleEx(408, 2165.1445, -1972.4277, 14.1021, 179.3826, 1, 1, 180000); // Trashmaster

// Delivery Driver

JobVehicle[2][0] = AddStaticVehicleEx(499, 2486.5935, -2115.5938, 13.5393, 359.8707, 1, 1, 180000); // Benson
JobVehicle[2][1] = AddStaticVehicleEx(499, 2481.7615, -2115.6641, 13.5385, 356.5260, 1, 1, 180000); // Benson
JobVehicle[2][2] = AddStaticVehicleEx(456, 2476.6794, -2114.4895, 13.7211, 1.6723, 1, 1, 180000); // Yankee
Code:
for(new v = 0; v < sizeof(JobVehicle[PlayerInfo[playerid][Job]]); v ++)
{
    if(IsValidVehicle(v)) SetVehicleParamsForPlayer(v, playerid, 0, 0);
}
But the loop gives errors:
Quote:
(2731) : error 001: expected token: "]", but found "-integer value-"
(2731) : error 029: invalid expression, assumed zero
(2731) : error 029: invalid expression, assumed zero
(2731) : fatal error 107: too many error messages on one line
So what is the proper way of doing this?
__________________
[Tutorial] Custom Playerdamage system
Desert Eagle's dealing too much damage, you want one-hit sniper kills, or damage based on body parts? Check this out.
CalvinC is offline   Reply With Quote
Old 19/03/2016, 05:52 PM   #2
YouServ
Banned
 
Join Date: Oct 2015
Posts: 141
Reputation: 17
Default Re: Sizeof 2D array

for(new v = 0; v < sizeof(JobVehicle[PlayerInfo[playerid][Job]]))
YouServ is offline   Reply With Quote
Old 19/03/2016, 05:55 PM   #3
introzen
Gangsta
 
introzen's Avatar
 
Join Date: Aug 2008
Posts: 774
Reputation: 30
Default Re: Sizeof 2D array

Try this:

pawn Code:
for(new v=0; v <= sizeof(JobVehicle[PlayerInfo[playerid][pJob]]) v++) {
    if(IsValidVehicle(v)) SetVehicleParamsForPlayer(v, playerid, 0 0);
}

You forgot to to create the actual loop by adding "v++" to it. I also added "<=" as the first vehicle in the array else wouldn't be affected by the loop (if i'm not mistaken. Was a long time ago I did programming).
introzen is offline   Reply With Quote
Old 19/03/2016, 06:03 PM   #4
CalvinC
High-roller
 
CalvinC's Avatar
 
Join Date: Nov 2014
Location: Denmark
Posts: 1,852
Reputation: 114
Default Re: Sizeof 2D array

Quote:
Originally Posted by introzen View Post
Try this:

pawn Code:
for(new v=0; v <= sizeof(JobVehicle[PlayerInfo[playerid][pJob]]) v++) {
    if(IsValidVehicle(v)) SetVehicleParamsForPlayer(v, playerid, 0 0);
}

You forgot to to create the actual loop by adding "v++" to it. I also added "<=" as the first vehicle in the array else wouldn't be affected by the loop (if i'm not mistaken. Was a long time ago I did programming).
Sorry i forgot to include that in the post, edited.
Still the same errors.
__________________
[Tutorial] Custom Playerdamage system
Desert Eagle's dealing too much damage, you want one-hit sniper kills, or damage based on body parts? Check this out.
CalvinC is offline   Reply With Quote
Old 19/03/2016, 06:05 PM   #5
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,989
Reputation: 2649
Default Re: Sizeof 2D array

An array only has one size which is determined at compile time. Therefore you can't use an index. Indices that aren't explicitly assigned a value are implicitly initialized to 0. You can use:
PHP Code:
sizeof(JobVehicle
PHP Code:
sizeof(JobVehicle[]) 
To get the size of the first and second dimension, respectively.
__________________
Vince is offline   Reply With Quote
Old 19/03/2016, 06:25 PM   #6
introzen
Gangsta
 
introzen's Avatar
 
Join Date: Aug 2008
Posts: 774
Reputation: 30
Default Re: Sizeof 2D array

Quote:
Originally Posted by Vince View Post
An array only has one size which is determined at compile time. Therefore you can't use an index. Indices that aren't explicitly assigned a value are implicitly initialized to 0. You can use:
PHP Code:
sizeof(JobVehicle
PHP Code:
sizeof(JobVehicle[]) 
To get the size of the first and second dimension, respectively.
According to this information (which I didn't know appearently, thank you btw) would this work perhaps?

pawn Code:
for(new v=0; v <= sizeof(JobVehicle[]) v++) {
    if(IsValidVehicle(v) && JobVehicle[] == PlayerInfo[playerid][pJob]) SetVehicleParamsForPlayer(v, playerid, 0 0);
}
introzen is offline   Reply With Quote
Old 19/03/2016, 07:47 PM   #7
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,989
Reputation: 2649
Default Re: Sizeof 2D array

You can use indices inside the loop, just not in combination with the sizeof operator itself. Which means what is likely desired is:
PHP Code:
for(new 0sizeof(JobVehicle[]); v++) {
    
SetVehicleParamsForPlayer(JobVehicle[PlayerInfo[playerid][pJob]][v], playerid00);

Assuming that 0 <= PlayerInfo[playerid][pJob] < sizeof JobVehicle.
__________________
Vince 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
sizeof through an array 2KY Scripting Help 4 21/12/2013 09:35 PM
Sizeof 2D Array CoaPsyFactor Scripting Help 16 22/03/2013 10:58 PM
sizeof char-array MP2 Scripting Help 9 17/03/2013 07:39 PM
sizeof array RedFusion Scripting Help 2 07/12/2012 03:21 PM
sizeof 2D array zgintasz Scripting Help 8 05/07/2012 09:35 AM


All times are GMT. The time now is 01:19 PM.


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