PDA

View Full Version : Looping Through multi-dimensional array


Tass007
28/11/2016, 05:24 AM
Hey guys, I'm trying to use an array and go through specific values so that when I call the function the next value in the array gets SetPlayerSkin.

I don't know if that makes any sense. Basically I'm making a script that whenever called it gets the next value in the array and then sets the player's skin to the value. However I'm getting error's and I don't quite understand what I'm meant to do. I've read Vince's tutorial about Looping through arrays.

http://forum.sa-mp.com/showthread.php?t=301933

However whenever he does it he uses an enum and I'm not so when I'm using my array I don't know what value to put in as I keep getting
error 035: argument type mismatch (argument 2)

I hope this is making sense :D Here is my array:

static const FemaleSkins[78][] =
{
{9},{10},{11},{12},{13},{31},{39},{40},{41},{53},{ 54},{55},{56},{63},{64},{65},{69},{75},{76},{77},{ 85},{87},{88},{89},{90},{91},{93},{129},{130},{131 },//30
{138},{129},{140},{141},{145},{148},{150},{151},{1 42},{157},{169},{172},{178},{190},{191},{192},{193 },{194},{195},{196},{197},{198},{199},{201},//54
{205},{207},{214},{215},{216},{218},{219},{224},{2 25},{226},{231},{232},{233},{237},{238},{243},{244 },{245},{246},{251},{256},{257},{263},{298}//78
};

These are the female skin ids.

Then here is where i'm calling it. I've removed everything apart from the loop for you guys just to look at the loop.


public PlusSkin(playerid)
{
......
for(new i; i < sizeof(FemaleSkins[]); i++)
{
SetPlayerSkin(playerid, FemaleSkins[i]);
}
....
}
.....
return 1;
}

So in summary I'm getting the error

error 035: argument type mismatch (argument 2)

On this line

SetPlayerSkin(playerid, FemaleSkins[i]);

As I'm new to arrays and I'm not quite sure how they work without an enum.

Dayrion
28/11/2016, 05:47 AM
Why you make a 2Dimensions array for one value each time ? Remove every {} between each ID and you will get no errors and by the way, it's should work.

Alvitr
28/11/2016, 05:48 AM
umm..

maybe here have reason
http://forum.sa-mp.com/showthread.php?t=318212

static const FemaleSkins[] =
{ //{
9,10,11,12,13,31,39,40,41,53,54,55,56,63,64,65,69, 75,76,77,85,87,88,89,90,91,93,129,130,131,//30
138,129,140,141,145,148,150,151,142,157,169,172,17 8,190,191,192,193,194,195,196,197,198,199,201,//54
205,207,214,215,216,218,219,224,225,226,231,232,23 3,237,238,243,244,245,246,251,256,257,263,298//78
};//}
for(new i; i < sizeof(FemaleSkins); i++)
{
SetPlayerSkin(playerid, FemaleSkins[i]);
}

Tass007
28/11/2016, 06:01 AM
Thanks guys. I didn't know what I was doing :D

Dayrion
28/11/2016, 06:54 AM
2D Array is usefull when you have more than one value for different 'position'.
Example:
static const Float:dmZone4Coord[3][4] = // dmzone desert
{
{277.8264,1956.2932, 17.6406,267.2016}, // Spawn type1
{275.6713, 1956.5133, 17.6406, 266.3391}, //Spawn type2
{278.1663, 1988.6407, 17.6406, 270.3955} //Spawn type3
};

Tass007
28/11/2016, 07:00 AM
Right so because there's X and Y, it is a 2D Array? If it was X, Y and Z it'll be 3D? Or you'd use an enum?

Micko123
28/11/2016, 07:02 AM
Check this out

http://forum.sa-mp.com/showthread.php?t=318212

Tass007
28/11/2016, 07:17 AM
I've already read that. However it's sort of hard to follow as most of the picture's he's referring to aren't there.

Micko123
28/11/2016, 07:24 AM
This??

http://forum.sa-mp.com/showthread.php?t=301933

Tass007
28/11/2016, 07:30 AM
Yeah I've read that as well. I posted that topic in my initial post. :D

Micko123
28/11/2016, 07:31 AM
Lol.. Didn't saw that sorry :D

Tass007
28/11/2016, 07:45 AM
All goods. I've been testing my script and for some reason the loop is going backwards. It starts with Skin 1 because I manually set it. But when I use the callback PlusSkin it sets to skin 298 which is last female skin and if I try to plus anymore the skins don't change.

SickAttack
28/11/2016, 07:51 AM
Most obvious thing in the word.

public PlusSkin(playerid)
{
......
for(new i; i < sizeof(FemaleSkins[]); i++)
{
SetPlayerSkin(playerid, FemaleSkins[i]);
}
....
}
.....
return 1;
}

IS WRONG

In that case, it should be:
public PlusSkin(playerid)
{
......
for(new i; i < sizeof(FemaleSkins[]); i++)
{
SetPlayerSkin(playerid, FemaleSkins[0][i]);
}
....
}
.....
return 1;
}

0 or any number up to 77.

OR

static const FemaleSkins[] =
{
{9},{10},{11},{12},{13},{31},{39},{40},{41},{53},{ 54},{55},{56},{63},{64},{65},{69},{75},{76},{77},{ 85},{87},{88},{89},{90},{91},{93},{129},{130},{131 },//30
{138},{129},{140},{141},{145},{148},{150},{151},{1 42},{157},{169},{172},{178},{190},{191},{192},{193 },{194},{195},{196},{197},{198},{199},{201},//54
{205},{207},{214},{215},{216},{218},{219},{224},{2 25},{226},{231},{232},{233},{237},{238},{243},{244 },{245},{246},{251},{256},{257},{263},{298}//78
};

public PlusSkin(playerid)
{
......
for(new i; i < sizeof(FemaleSkins); i++)
{
SetPlayerSkin(playerid, FemaleSkins[i]);
}
....
}
.....
return 1;
}

Tass007
28/11/2016, 08:01 AM
Thanks SickAttack but I've already had help solving that issue.

Dayrion
28/11/2016, 08:04 AM
I'll try to explain as best as I can
new Dim[3] =
{
5, // 1 value in one dimension
6,
7
};
or
new Dim[3] =
{
{5}, // 1 value in one dimension
{6},
{7}
};


new Dim[3][2] =
{
{1, 2}, // 2 values so it's creating a dimension. What I mean? The first dimension is {1,2}, {2,3}, {4,5}. The second is a specific value in the first dimension. Example Dim[0][0] = The value 0 in the first dimension : {1,2} and the first value which is 1. It will return 1.
{2, 3},
{4, 5}
};

new Dim[3][3][9] =
{
{1, 2, "Someone0"}, // It's the same thing but we need an another dimension for the string. Why? We have to choose letter. Example I want the '0' of the first string in the first dimension I need to do that : Dim[0][2][8]
// Dim[0] : First dimension which is {1, 2, "Someone0"} - Dim[0][2] : we choose the string which is "Someone0" Dim[0][2][8] the last letter of the string '0'.
{2, 3, "Someone1"},
{4, 5, "Someone2"}
};

My english suck as you can see, apologize. ><

Tass007
28/11/2016, 08:15 AM
I think I understand the difference between 2D, 3D and normal arrays thanks for that clarification.

SickAttack
28/11/2016, 09:04 AM
I'll try to explain as best as I can
new Dim[3] =
{
5, // 1 value in one dimension
6,
7
};
or
new Dim[3] =
{
{5}, // 1 value in one dimension
{6},
{7}
};


new Dim[3][2] =
{
{1, 2}, // 2 values so it's creating a dimension. What I mean? The first dimension is {1,2}, {2,3}, {4,5}. The second is a specific value in the first dimension. Example Dim[0][0] = The value 0 in the first dimension : {1,2} and the first value which is 1. It will return 1.
{2, 3},
{4, 5}
};

new Dim[3][3][9] =
{
{1, 2, "Someone0"}, // It's the same thing but we need an another dimension for the string. Why? We have to choose letter. Example I want the '0' of the first string in the first dimension I need to do that : Dim[0][2][8]
// Dim[0] : First dimension which is {1, 2, "Someone0"} - Dim[0][2] : we choose the string which is "Someone0" Dim[0][2][8] the last letter of the string '0'.
{2, 3, "Someone1"},
{4, 5, "Someone2"}
};

My english suck as you can see, apologize. ><

And what does that have to do with anything? It's not even relevant.

Tass007
29/11/2016, 02:09 AM
And what does that have to do with anything? It's not even relevant.

Actually they were helping me understand the difference between 2D, and 3D arrays. Therefore relevance.

SickAttack
29/11/2016, 02:23 AM
Actually they were helping me understand the difference between 2D, and 3D arrays. Therefore relevance.

It's not relevant because you weren't doing it wrong in the first place.

Threshold
29/11/2016, 03:18 AM
Yes he was.
static const FemaleSkins[78][] =
{
{9},{10},{11},{12},{13},{31},{39},{40},{41},{53},{ 54},{55},{56},{63},{64},{65},{69},{75},{76},{77},{ 85},{87},{88},{89},{90},{91},{93},{129},{130},{131 },//30
{138},{129},{140},{141},{145},{148},{150},{151},{1 42},{157},{169},{172},{178},{190},{191},{192},{193 },{194},{195},{196},{197},{198},{199},{201},//54
{205},{207},{214},{215},{216},{218},{219},{224},{2 25},{226},{231},{232},{233},{237},{238},{243},{244 },{245},{246},{251},{256},{257},{263},{298}//78
};

That is a 2 dimensional array.. you only need a single dimensional array.

SickAttack
29/11/2016, 05:42 AM
Yes he was.
static const FemaleSkins[78][] =
{
{9},{10},{11},{12},{13},{31},{39},{40},{41},{53},{ 54},{55},{56},{63},{64},{65},{69},{75},{76},{77},{ 85},{87},{88},{89},{90},{91},{93},{129},{130},{131 },//30
{138},{129},{140},{141},{145},{148},{150},{151},{1 42},{157},{169},{172},{178},{190},{191},{192},{193 },{194},{195},{196},{197},{198},{199},{201},//54
{205},{207},{214},{215},{216},{218},{219},{224},{2 25},{226},{231},{232},{233},{237},{238},{243},{244 },{245},{246},{251},{256},{257},{263},{298}//78
};

That is a 2 dimensional array.. you only need a single dimensional array.

I was talking about the curly braces. That was already pointed out nonetheless.