How to properly use arrays?
#1

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

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); }
Reply
#3

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 © 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}
};
Reply
#4

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}
};
Reply
#5

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

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

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);
}
Reply
#8

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

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

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
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)