How to get a random value from a multidimensional array? -
Magic_Time - 10.10.2014
Hi, I have this:
PHP код:
new Float:TeamSpawns[][4][4] = {
{
{1090.1176,1915.2543,10.8203,359.0264},
{1093.6993,1905.3907,16.4873,2.1364},
{1116.3743,1891.2225,10.8203,167.8679},
{1092.0170,1892.6232,10.8203,182.2814}
},
{
{-799.3118,1570.1510,27.0807,87.2953},
{-819.4633,1499.2266,19.7184,179.7295},
{-767.4883,1520.2850,26.7911,270.9103},
{-721.7050,1538.5356,40.4757,93.5855}
}
};
I have this function:
PHP код:
stock SetPlayerRandTeamPos(playerid, team)
{
new rand = random(sizeof(TeamSpawns[team]));
SetPlayerPos(playerid, TeamSpawns[team][rand][0], TeamSpawns[team][rand][1], TeamSpawns[team][rand][2]);
SetPlayerFacingAngle(playerid, TeamSpawns[team][rand][3]);
return 1;
}
I get an error here:
PHP код:
new rand = random(sizeof(TeamSpawns[team]));
Respuesta: How to get a random value from a multidimensional array? -
SickAttack - 10.10.2014
pawn Код:
new Float:TeamSpawns[][] =
{
{1090.1176,1915.2543,10.8203,359.0264},
{1093.6993,1905.3907,16.4873,2.1364},
{1116.3743,1891.2225,10.8203,167.8679},
{1092.0170,1892.6232,10.8203,182.2814}
{-799.3118,1570.1510,27.0807,87.2953},
{-819.4633,1499.2266,19.7184,179.7295},
{-767.4883,1520.2850,26.7911,270.9103},
{-721.7050,1538.5356,40.4757,93.5855}
};
stock SetPlayerRandomPos(playerid)
{
new rand = random(sizeof(TeamSpawns));
SetPlayerPos(playerid, TeamSpawns[rand][0], TeamSpawns[rand][1], TeamSpawns[rand][2]);
SetPlayerFacingAngle(playerid, TeamSpawns[rand][3]);
return 1;
}
Re: Respuesta: How to get a random value from a multidimensional array? -
Magic_Time - 10.10.2014
Quote:
Originally Posted by SickAttack
pawn Код:
new Float:TeamSpawns[][] = { {1090.1176,1915.2543,10.8203,359.0264}, {1093.6993,1905.3907,16.4873,2.1364}, {1116.3743,1891.2225,10.8203,167.8679}, {1092.0170,1892.6232,10.8203,182.2814} {-799.3118,1570.1510,27.0807,87.2953}, {-819.4633,1499.2266,19.7184,179.7295}, {-767.4883,1520.2850,26.7911,270.9103}, {-721.7050,1538.5356,40.4757,93.5855} };
stock SetPlayerRandomPos(playerid) { new rand = random(sizeof(TeamSpawns)); SetPlayerPos(playerid, TeamSpawns[rand][0], TeamSpawns[rand][1], TeamSpawns[rand][2]); SetPlayerFacingAngle(playerid, TeamSpawns[rand][3]); return 1; }
|
That's not right, I have a three dimentional array.
Respuesta: Re: Respuesta: How to get a random value from a multidimensional array? -
SickAttack - 10.10.2014
Quote:
Originally Posted by Magic_Time
That's not right, I have a three dimentional array.
|
Yeah, but I made a two dimensional array.
I recommend that you create an array for each team, it will require less checking and looping through the information than everything in a single array.
Re: How to get a random value from a multidimensional array? -
paulommu - 10.10.2014
I think you can't use
sizeof on a multidimensional array.
You should do something like this:
pawn Код:
new Float: TeamASpawns[][4] = {
{1090.1176,1915.2543,10.8203,359.0264},
{1093.6993,1905.3907,16.4873,2.1364},
{1116.3743,1891.2225,10.8203,167.8679},
{1092.0170,1892.6232,10.8203,182.2814}
};
new Float: TeamBSpawns[][4] = {
{-799.3118,1570.1510,27.0807,87.2953},
{-819.4633,1499.2266,19.7184,179.7295},
{-767.4883,1520.2850,26.7911,270.9103},
{-721.7050,1538.5356,40.4757,93.5855}
};
Then use @SickAttack's example code.
Re: How to get a random value from a multidimensional array? -
Magic_Time - 11.10.2014
Quote:
Originally Posted by paulommu
I think you can't use sizeof on a multidimensional array.
You should do something like this:
pawn Код:
new Float: TeamASpawns[][4] = { {1090.1176,1915.2543,10.8203,359.0264}, {1093.6993,1905.3907,16.4873,2.1364}, {1116.3743,1891.2225,10.8203,167.8679}, {1092.0170,1892.6232,10.8203,182.2814} };
new Float: TeamBSpawns[][4] = { {-799.3118,1570.1510,27.0807,87.2953}, {-819.4633,1499.2266,19.7184,179.7295}, {-767.4883,1520.2850,26.7911,270.9103}, {-721.7050,1538.5356,40.4757,93.5855} };
Then use @SickAttack's example code.
|
Yeah, I discovered how.
PHP код:
new rand = random(sizeof(TeamSpawns[]));
Re: How to get a random value from a multidimensional array? -
SickAttack - 11.10.2014
Quote:
Originally Posted by Magic_Time
Yeah, I discovered how.
PHP код:
new rand = random(sizeof(TeamSpawns[]));
|
You can, but you don't need the brackets "[]".
Re: How to get a random value from a multidimensional array? -
Magic_Time - 11.10.2014
Quote:
Originally Posted by SickAttack
You can, but you don't need the brackets "[]".
|
You don't understand what I want, you would if you'd look carefully at my array.
Here's something for you to understand.
PHP код:
sizeof(array) // First dimension
sizeof(array[]) // second dimension THIS IS MY CASE
sizeof(array[][]) // third dimension
I discovered how to fix my problem. Thanks for trying to help.
Re: How to get a random value from a multidimensional array? -
SickAttack - 11.10.2014
Quote:
Originally Posted by Magic_Time
You don't understand what I want, you would if you'd look carefully at my array.
Here's something for you to understand.
PHP код:
sizeof(array) // First dimension
sizeof(array[]) // second dimension THIS IS MY CASE
sizeof(array[][]) // third dimension
I discovered how to fix my problem. Thanks for trying to help.
|
I already know but how do you expect me to know what you want if you don't even explain anything?
Quote:
Here's my code. Problem -> this.
|
Be more specific next time.