SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 30/03/2014, 02:07 AM   #1
rangerxxll
Gangsta
 
rangerxxll's Avatar
 
Join Date: Aug 2011
Posts: 882
Reputation: 59
Post How to properly use arrays?

I haven't used arrays much, but I'm trying to learn how to use them properly. I've tried self-tests but they all lead to failure. How would I load all the coordinates from this array? So I can use "Create3DTextLabel" once, instead of having multiple for the same thing.

Here's what I've tried to do.

pawn Code:
new Float:fishingspots[0][2] =
{
    {-1874.6598,-1670.0956,0.9824}
};

Create3DTextLabel("Fishing area.\n/fish ", 0x008080FF,fishingspots[0][0], fishingspots[0][1], fishingspots[0][2], 20.0, 0, 0);
Which leads me to:
Code:
C:\Users\Luke\Desktop\hungergames.pwn(50) : error 018: initialization data exceeds declared size
C:\Users\Luke\Desktop\hungergames.pwn(1085) : error 032: array index out of bounds (variable "fishingspots")
How would I be able to load all the coordinates from the array into just one create3dtextlabel? Like this:

pawn Code:
Create3DTextLabel("Fishing area.\n/fish ", 0x008080FF,fishingspots[][0], fishingspots[][1], fishingspots[][2], 20.0, 0, 0);
So it creates a pickup at the array's coordinates instead of me having to completely add multiple text labels.
__________________

Server Website: http://www.pgagaming.net/forums/
Server IP:87.98.241.207:7024
rangerxxll is offline   Reply With Quote
Old 30/03/2014, 03:00 AM   #2
EiresJason
Gangsta
 
Join Date: Mar 2013
Location: Dublin.
Posts: 685
Reputation: 76
Default Re: How to properly use arrays?

You can use something like this to create the 3dtextlabels.

pawn Code:
#define MAX_FISHING_AREAS 3
new Float:fishingspots[MAX_FISHING_AREAS][3] =
{
    {-1874.6598,-1670.0956,0.9824}, //Fishing Spot 1
    {2323.2, 23232.4, 0.2323}, //Fishing Spot 2
    {4443.0, 323.0, 25.1} //Fishing Spot 3
};

//creating the TextLabels.
    for(new i=0; i<MAX_FISHING_AREAS; i++) { Create3DTextLabel("Fishing area.\n/fish ", 0x008080FF,fishingspots[i][0], fishingspots[i][1], fishingspots[i][2], 20.0, 0, 0); }
EiresJason is offline   Reply With Quote
Old 30/03/2014, 03:04 AM   #3
rangerxxll
Gangsta
 
rangerxxll's Avatar
 
Join Date: Aug 2011
Posts: 882
Reputation: 59
Default Re: How to properly use arrays?

Interesting.

Code:
C:\Users\Luke\Desktop\hungergames.pwn(50) : warning 213: tag mismatch
C:\Users\Luke\Desktop\hungergames.pwn(50) : warning 213: tag mismatch
C:\Users\Luke\Desktop\hungergames.pwn(50) : warning 213: tag mismatch
C:\Users\Luke\Desktop\hungergames.pwn(50) : error 018: initialization data exceeds declared size
C:\Users\Luke\Desktop\hungergames.pwn(1088) : error 032: array index out of bounds (variable "fishingspots")
Pawn compiler 3.2.3664	 	 	Copyright (c) 1997-2006, ITB CompuPhase


2 Errors.
pawn Code:
#define MAX_FISH_AREAS 2

new fishingspots[MAX_FISH_AREAS][2] =
{
    {-1874.6598,-1670.0956,0.9824},
    {-1885.7527,-1671.0122,1.0201}
};
__________________

Server Website: http://www.pgagaming.net/forums/
Server IP:87.98.241.207:7024
rangerxxll is offline   Reply With Quote
Old 30/03/2014, 03:30 AM   #4
EiresJason
Gangsta
 
Join Date: Mar 2013
Location: Dublin.
Posts: 685
Reputation: 76
Default Re: How to properly use arrays?

You need to set the array to store 3 variables, not 2

pawn Code:
#define MAX_FISH_AREAS 2

new fishingspots[MAX_FISH_AREAS][3] = //changed your [2] to [3]
{
    {-1874.6598,-1670.0956,0.9824},
    {-1885.7527,-1671.0122,1.0201}
};
EiresJason is offline   Reply With Quote
Old 30/03/2014, 03:38 AM   #5
newbie scripter
Gangsta
 
newbie scripter's Avatar
 
Join Date: Apr 2012
Location: Heaven <3
Posts: 798
Reputation: 34
Default Re: How to properly use arrays?

pawn Code:
fishingspots[MAX_FISH_AREAS][2] = // [2] means the number of rows in the array I.E
// {-1874.6598,-1670.0956,0.9824} here -1874 and -1670 will be saved but not 0.9824. So, we make it [3]
__________________
SUGGESTION FOR FUTURE SA-MP
pawn Code:
forward SetWeaponRange(weaponid, maxrange); // if weapon is fist and 100 range, it may give a warn or super punch xD
forward SetWeaponRecoil(weapoinid, maxrecoil);
newbie scripter is offline   Reply With Quote
Old 30/03/2014, 03:38 AM   #6
rangerxxll
Gangsta
 
rangerxxll's Avatar
 
Join Date: Aug 2011
Posts: 882
Reputation: 59
Default Re: How to properly use arrays?

I'm confused. In order what does [][] do? And do they both start at 0, therefor I was assuming it was 2, not 3.

^ You stated that [2] defines the rows. Then what does MAX_FISH_AREAS do?
__________________

Server Website: http://www.pgagaming.net/forums/
Server IP:87.98.241.207:7024
rangerxxll is offline   Reply With Quote
Old 30/03/2014, 03:43 AM   #7
RoboN1X
Huge Clucker
 
RoboN1X's Avatar
 
Join Date: Feb 2011
Location: Indonesia
Posts: 453
Reputation: 145
Default Re: How to properly use arrays?

pawn Code:
#define MAX_FISHING_AREA 3  // change this if you are going to have more fishing area
new Float:fishingspots[MAX_FISHING_AREA][3] = // 3 means there are 3 floating points (for the coordinates: x,y,z)
{
    //  x = 0   |   y = 1   |   z = 2
    {-1874.6598, -1670.0956, 0.9824}, // fishing area 1 (0)
    {-1885.7527, -1671.0122, 1.0201},       // fishing area 2 (1)
    {x, y, z}       // fishing area 3 (2), change the x, y, z to your coordinates (floating points), notice there is no comma for last area
};
// loop the array number from 0 to 2 (the number in brackets of each fishing area)
for(new i = 0; i < MAX_FISHING_AREA; i++)
{
    Create3DTextLabel("Fishing area.\n/fish ", 0x008080FF,fishingspots[i][0], fishingspots[i][1], fishingspots[i][2], 20.0, 0, 0);
}
__________________
Need Help? Use Search, WIKI, and FAQ
RoboN1X is offline   Reply With Quote
Old 30/03/2014, 03:44 AM   #8
EiresJason
Gangsta
 
Join Date: Mar 2013
Location: Dublin.
Posts: 685
Reputation: 76
Default Re: How to properly use arrays?

To explain it with fishing areas.

The first [] is the fishing area, in this case you want 2 fishing areas so placing MAX_FISH_AREAS inside the first [] is basically saying that you want two fishing areas.

(MAX_FISH_AREAS is the same as 2, so [MAX_FISH_AREAS] is the same as [2])

Then the second [] would be the fishing area coordinates but because there is 3 coordinates for each fishing area, you need the second [] to have [3] inside it.

The reason the first [] has MAX_FISH_AREAS is because you can easily change it just by changing the value of the #define.

I hope that explains it enough xD
EiresJason is offline   Reply With Quote
Old 30/03/2014, 03:50 AM   #9
rangerxxll
Gangsta
 
rangerxxll's Avatar
 
Join Date: Aug 2011
Posts: 882
Reputation: 59
Default Re: How to properly use arrays?

Quote:
Originally Posted by Robo_N1X View Post
pawn Code:
#define MAX_FISHING_AREA 3  // change this if you are going to have more fishing area
new Float:fishingspots[MAX_FISHING_AREA][3] = // 3 means there are 3 floating points (for the coordinates: x,y,z)
{
    //  x = 0   |   y = 1   |   z = 2
    {-1874.6598, -1670.0956, 0.9824}, // fishing area 1 (0)
    {x, y, z},      // fishing area 2 (1), change the x, y, z to your coordinates (floating points)
    {x, y, z}       // fishing area 3 (2), notice there is no comma for last area
};
// loop the array number from 0 to 2 (the number in brackets of each fishing area)
for(new i = 0; i < MAX_FISHING_AREA; i++)
{
    Create3DTextLabel("Fishing area.\n/fish ", 0x008080FF,fishingspots[i][0], fishingspots[i][1], fishingspots[i][2], 20.0, 0, 0);
}
You said that there's 3 floating points, yet we're declaring 2 in the creation of the label. Couldn't we change [3] to [2] since they start at 0?

pawn Code:
new Float:fishingspots[MAX_FISH_AREAS][2] // starting from 0 <--
__________________

Server Website: http://www.pgagaming.net/forums/
Server IP:87.98.241.207:7024
rangerxxll is offline   Reply With Quote
Old 30/03/2014, 03:52 AM   #10
RoboN1X
Huge Clucker
 
RoboN1X's Avatar
 
Join Date: Feb 2011
Location: Indonesia
Posts: 453
Reputation: 145
Default Re: How to properly use arrays?

Quote:
Originally Posted by rangerxxll View Post
You said that there's 3 floating points, yet we're declaring 2 in the creation of the label. Couldn't we change [3] to [2] since they start at 0?

pawn Code:
new Float:fishingspots[MAX_FISH_AREAS][2] // starting from 0 <--
It's just the size for declaration, size is starting from 1 if you change it to 2, the compiler will complain
To access the index of the variable arrays, the number starts from 0
There is actually 3 items on each index:
0
1
2
You can't use number 3 for the index, except you are increasing the size to 4 then you can use until number 3
__________________
Need Help? Use Search, WIKI, and FAQ
RoboN1X 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
Need help about Arrays basicllsw Scripting Help 5 26/01/2012 11:28 PM
Need help with arrays. CoaPsyFactor Help Archive 2 22/08/2010 03:52 AM
How to do this properly Torran Help Archive 2 12/03/2010 01:58 PM
Can't aim properly nightmare00 Client Support 2 28/03/2009 01:26 AM


All times are GMT. The time now is 11:43 PM.


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