(help)store floating variable to array
#1

Hello, how to store floating variable to array? I just follow like this thread https://sampforum.blast.hk/showthread.php?tid=243175. here's my code:
Код:
new	bool:dead;

new Float:DistanceCounty_GH;
new Float:DistanceAll_Saints_GH;
new Float:DistanceCrippenMemorial_H;
new Float:DistanceAngelP_MC;
new Float:DistanceSanFierro_MC;
new Float:DistanceElQuebrados_MC;
new Float:DistanceLasVenturas_H;


new Float:HospitalSpawns[][] =
{
		{2031.8365, -1416.9377, 16.9922, 131.0062}, //County General Hospital
		{1182.0847, -1323.5752, 13.5812, 271.3102}, //All Saints General Hospital
		{1245.5205, 335.3977, 19.5547, 336.5431}, //CrippenMemorialHospital
		{-2196.8381, -2305.4912, 30.6250, 319.7987}, //AngelPineMedicalCenter
		{-2653.3613, 627.6060, 14.4531, 179.0622}, //SanFierroMedicalCenter
		{-1514.8903, 2528.7163, 55.7143, 359.3568}, //ElQuebradosMedicalCenter
		{1579.2933, 1769.0499, 10.8203, 90.3324} //LasVenturasHospital
};

public OnPlayerRequestClass(playerid, classid)
{
	dead = false;
	SetPlayerPos(playerid, -160.2186, -324.0767, 3.7590);
	SetPlayerFacingAngle(playerid, 87.7342);
	SetPlayerCameraPos(playerid, -170.899993, -323.940704, 3.929687);
	SetPlayerCameraLookAt(playerid, -170.899993, -323.940704, 3.929687);
	return 1;
}

stock GetSmallestIndex(v[], len = sizeof(v))
{
    new min_index, min_value = -1;
    for(new i = 0; i < len; i ++)
    {
        if(v[i] < min_value || min_value == -1)
        {
            min_index = i;
            min_value = v[i];
        }
    }
    return min_index;
}

public OnPlayerSpawn(playerid)
{
	if(dead==true)
	{
	new float:values[7];
	values[0]=DistanceCounty_GH;
	values[1]=DistanceAll_Saints_GH;
	values[2]=DistanceCrippenMemorial_H;
	values[3]=DistanceAngelP_MC;
	values[4]=DistanceSanFierro_MC;
	values[5]=DistanceElQuebrados_MC;
	values[6]=DistanceLasVenturas_H;

	switch(GetSmallestIndex(values))
	{
    		case 0:
    		{SetPlayerPos(playerid, HospitalSpawns[0][0], HospitalSpawns[0][1], HospitalSpawns[0][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[0][3]);
		    SetCameraBehindPlayer(playerid);}
 		   	case 1:
			{SetPlayerPos(playerid, HospitalSpawns[1][0], HospitalSpawns[1][1], HospitalSpawns[1][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[1][3]);
		    SetCameraBehindPlayer(playerid);
  			}
  			case 2:
			{SetPlayerPos(playerid, HospitalSpawns[2][0], HospitalSpawns[2][1], HospitalSpawns[2][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[2][3]);
  			SetCameraBehindPlayer(playerid);
  			}
  			case 3:
			{SetPlayerPos(playerid, HospitalSpawns[3][0], HospitalSpawns[3][1], HospitalSpawns[3][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[3][3]);
  			SetCameraBehindPlayer(playerid);
  			}
  			case 4:
			{SetPlayerPos(playerid, HospitalSpawns[4][0], HospitalSpawns[4][1], HospitalSpawns[4][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[4][3]);
  			SetCameraBehindPlayer(playerid);
  			}
  			case 5:
			{SetPlayerPos(playerid, HospitalSpawns[5][0], HospitalSpawns[5][1], HospitalSpawns[5][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[5][3]);
  			SetCameraBehindPlayer(playerid);
  			}
  			case 6:
			{SetPlayerPos(playerid, HospitalSpawns[6][0], HospitalSpawns[6][1], HospitalSpawns[6][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[6][3]);
			SetCameraBehindPlayer(playerid);
  			}
		}
	}

	if(dead==false)
	{
	SetPlayerPos(playerid, 1714.9219,-1948.8318,14.1172);
	SetPlayerFacingAngle(playerid, 357.5184);
	SetCameraBehindPlayer(playerid);
	return 1;
	}
	return 0;
}


public OnPlayerDeath(playerid, killerid, reason)
{
    SendDeathMessage(killerid, playerid, reason); // Shows the kill in the killfeed
    SetPlayerScore(killerid,GetPlayerScore(killerid)+1);
    dead = true;
 	DistanceCounty_GH = GetPlayerDistanceFromPoint(playerid, HospitalSpawns[0][0], HospitalSpawns[0][1], HospitalSpawns[0][2]);
	DistanceAll_Saints_GH = GetPlayerDistanceFromPoint(playerid, HospitalSpawns[1][0], HospitalSpawns[1][1], HospitalSpawns[1][2]);
	DistanceCrippenMemorial_H = GetPlayerDistanceFromPoint(playerid, HospitalSpawns[2][0], HospitalSpawns[2][1], HospitalSpawns[2][2]);
	DistanceAngelP_MC = GetPlayerDistanceFromPoint(playerid, HospitalSpawns[3][0], HospitalSpawns[3][1], HospitalSpawns[3][2]);
	DistanceSanFierro_MC = GetPlayerDistanceFromPoint(playerid, HospitalSpawns[4][0], HospitalSpawns[4][1], HospitalSpawns[4][2]);
	DistanceElQuebrados_MC = GetPlayerDistanceFromPoint(playerid, HospitalSpawns[5][0], HospitalSpawns[5][1], HospitalSpawns[5][2]);
	DistanceLasVenturas_H = GetPlayerDistanceFromPoint(playerid, HospitalSpawns[6][0], HospitalSpawns[6][1], HospitalSpawns[6][2]);
	return 1;
}
my code before using array method:
Код:
	{
		if(DistanceCounty_GH < DistanceAll_Saints_GH && DistanceCrippenMemorial_H && DistanceAngelP_MC && DistanceSanFierro_MC && DistanceElQuebrados_MC && DistanceLasVenturas_H)
		{
		    SetPlayerPos(playerid, HospitalSpawns[0][0], HospitalSpawns[0][1], HospitalSpawns[0][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[0][3]);
		    SetCameraBehindPlayer(playerid);
		    return 1;
  		}
		else if(DistanceAll_Saints_GH < DistanceCounty_GH && DistanceCrippenMemorial_H && DistanceAngelP_MC && DistanceSanFierro_MC && DistanceElQuebrados_MC && DistanceLasVenturas_H)
		{
		    SetPlayerPos(playerid, HospitalSpawns[1][0], HospitalSpawns[1][1], HospitalSpawns[1][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[1][3]);
		    SetCameraBehindPlayer(playerid);
  		}
  		else if(DistanceCrippenMemorial_H < DistanceAll_Saints_GH && DistanceCounty_GH && DistanceAngelP_MC && DistanceSanFierro_MC && DistanceElQuebrados_MC && DistanceLasVenturas_H)
  		{
		    SetPlayerPos(playerid, HospitalSpawns[2][0], HospitalSpawns[2][1], HospitalSpawns[2][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[2][3]);
		    SetCameraBehindPlayer(playerid);
  		}
  		else if(DistanceAngelP_MC < DistanceCrippenMemorial_H && DistanceAll_Saints_GH && DistanceCounty_GH && DistanceSanFierro_MC && DistanceElQuebrados_MC && DistanceLasVenturas_H)
  		{
		    SetPlayerPos(playerid, HospitalSpawns[3][0], HospitalSpawns[3][1], HospitalSpawns[3][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[3][3]);
		    SetCameraBehindPlayer(playerid);
  		}
  		else if(DistanceSanFierro_MC < DistanceAngelP_MC && DistanceCrippenMemorial_H && DistanceAll_Saints_GH && DistanceCounty_GH && DistanceElQuebrados_MC && DistanceLasVenturas_H)
  		{
		    SetPlayerPos(playerid, HospitalSpawns[4][0], HospitalSpawns[4][1], HospitalSpawns[4][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[4][3]);
		    SetCameraBehindPlayer(playerid);
  		}
  		else if(DistanceElQuebrados_MC < DistanceSanFierro_MC && DistanceAngelP_MC && DistanceCrippenMemorial_H && DistanceAll_Saints_GH && DistanceCounty_GH && DistanceLasVenturas_H)
  		{
		    SetPlayerPos(playerid, HospitalSpawns[5][0], HospitalSpawns[5][1], HospitalSpawns[5][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[5][3]);
		    SetCameraBehindPlayer(playerid);
		}
  		else if(DistanceLasVenturas_H < DistanceElQuebrados_MC && DistanceSanFierro_MC && DistanceAngelP_MC && DistanceCrippenMemorial_H && DistanceAll_Saints_GH && DistanceCounty_GH)
  		{
		    SetPlayerPos(playerid, HospitalSpawns[6][0], HospitalSpawns[6][1], HospitalSpawns[6][2]);
		    SetPlayerFacingAngle(playerid, HospitalSpawns[6][3]);
		    SetCameraBehindPlayer(playerid);
		}
	}
by using this, player always spawn in county general hospital and all saints general hospital, because it just compare two variable. I guess the way I unstructured.
Reply
#2

Код:
HospitalSpawns[][]
Any number between the first pair of bracket stand for which coordinate set to use.
For example: HospitalSpawns[0][] means: the first line of coordinates.
With this comes a second pair of brackets which stand for the amount of parameters in each line of coordinates.
For example: HospitalSpawns[][0] means: the first parameter in the coordinate line (The X coordinate).

And you can now combine these two.
pawn Код:
HospitalSpawns[0][0] //The first set of coordinates and the X-coordinate.
pawn Код:
HospitalSpawns[0][1] //The first set of coordinates and the Y-coordinate.
pawn Код:
HospitalSpawns[0][2] //The first set of coordinates and the Z-coordinate.
pawn Код:
HospitalSpawns[0][3] //The first set of coordinates and the Angle-coordinate.
You can easily implement this in a function such as SetPlayerPos.
pawn Код:
SetPlayerPos(playerid, HospitalSpawns[0][0], HospitalSpawns[0][1], HospitalSpawns[0][2]); //The first set of coordinates and the X [0], Y [1] and Z-coordinate [2].
pawn Код:
SetPlayerFacingAngle(playerid, HospitalSpawns[0][3]); //The first set of coordinates and the Angle [3] coordinate.
EDIT: you can then use the same working style for the distance floating.
Reply
#3

Why waste space allocating an array? I recommend that you calculate the distance from the hospitals immediately when a player dies, then do whatever you want with that.
Also another issue I found in your code is the "dead" variable, it will only work for one player alone - make it a per-player var.
pawn Код:
#define FLOAT_INFINITY (Float:0x7F800000)
new Float:gClosestHospitalDist[MAX_PLAYERS]; // if for some reason you have to save the distance
new gClosestHospital[MAX_PLAYERS]; // holds the index to the closest hospital
public OnPlayerDeath(playerid, killerid, reason)
{
    // rest of code..
    new Float:dist = FLOAT_INFINITY;
    for (new i = 0; i < sizeof(HospitalSpawns); i++)
    {
        gClosestHospitalDist[playerid] = GetPlayerDistanceFromPoint(playerid, HospitalSpawns[i][0], HospitalSpawns[i][1], HospitalSpawns[i][2]);
        if (gClosestHospitalDist[playerid] < dist)
        {
            dist = gClosestHospitalDist[playerid];
            gClosestHospital[playerid] = i;
        }
    }
    return 1;
}
Now you simply use gClosestHospital[playerid] as the index for the HospitalSpawns array when you are setting the player position OnPlayerSpawn, you don't need multiple lines of the same code, no switch-case etc.
Reply
#4

Quote:
Originally Posted by Bible
Посмотреть сообщение
Код:
HospitalSpawns[][]
Any number between the first pair of bracket stand for which coordinate set to use.
For example: HospitalSpawns[0][] means: the first line of coordinates.
With this comes a second pair of brackets which stand for the amount of parameters in each line of coordinates.
For example: HospitalSpawns[][0] means: the first parameter in the coordinate line (The X coordinate).

And you can now combine these two.
pawn Код:
HospitalSpawns[0][0] //The first set of coordinates and the X-coordinate.
pawn Код:
HospitalSpawns[0][1] //The first set of coordinates and the Y-coordinate.
pawn Код:
HospitalSpawns[0][2] //The first set of coordinates and the Z-coordinate.
pawn Код:
HospitalSpawns[0][3] //The first set of coordinates and the Angle-coordinate.
You can easily implement this in a function such as SetPlayerPos.
pawn Код:
SetPlayerPos(playerid, HospitalSpawns[0][0], HospitalSpawns[0][1], HospitalSpawns[0][2]); //The first set of coordinates and the X [0], Y [1] and Z-coordinate [2].
pawn Код:
SetPlayerFacingAngle(playerid, HospitalSpawns[0][3]); //The first set of coordinates and the Angle [3] coordinate.
EDIT: you can then use the same working style for the distance floating.
Yes, I am write like that, and I create distance variable for store distance. But, I don't know how to find smallest variable. thank you btw

Quote:
Originally Posted by Virtual1ty
Посмотреть сообщение
Why waste space allocating an array? I recommend that you calculate the distance from the hospitals immediately when a player dies, then do whatever you want with that.
Also another issue I found in your code is the "dead" variable, it will only work for one player alone - make it a per-player var.
pawn Код:
#define FLOAT_INFINITY (Float:0x7F800000)
new Float:gClosestHospitalDist[MAX_PLAYERS]; // if for some reason you have to save the distance
new gClosestHospital[MAX_PLAYERS]; // holds the index to the closest hospital
public OnPlayerDeath(playerid, killerid, reason)
{
    // rest of code..
    new Float:dist = FLOAT_INFINITY;
    for (new i = 0; i < sizeof(HospitalSpawns); i++)
    {
        gClosestHospitalDist[playerid] = GetPlayerDistanceFromPoint(playerid, HospitalSpawns[i][0], HospitalSpawns[i][1], HospitalSpawns[i][2]);
        if (gClosestHospitalDist[playerid] < dist)
        {
            dist = gClosestHospitalDist[playerid];
            gClosestHospital[playerid] = i;
        }
    }
    return 1;
}
Now you simply use gClosestHospital[playerid] as the index for the HospitalSpawns array when you are setting the player position OnPlayerSpawn, you don't need multiple lines of the same code, no switch-case etc.
I don't know how to write a looping. thank you for teach me
I use boolean for checking if player spawn after killed or spawn after joined.
It's hard to understanding looping.
EDIT: how to call gClosestHospital as array? I am use this but didn't work:
pawn Код:
public OnPlayerSpawn(playerid)
{
    if(dead==true)
    {
    SetPlayerPos(playerid, HospitalSpawns[gClosestHospital][0], HospitalSpawns[gClosestHospital][1], HospitalSpawns[gClosestHospital][2]);
    SetPlayerFacingAngle(playerid, HospitalSpawns[gClosestHospital][3]);
    setCaneraBehindPlayer(playerid);
    return 1;
    }
    if(dead==false)
    {
    SetPlayerPos(playerid, 1714.9219,-1948.8318,14.1172);
    SetPlayerFacingAngle(playerid, 357.5184);
    SetCameraBehindPlayer(playerid);
    return 1;
    }
    return 0;
}
EDIT: now is work, I create new variable for gClosestHospital
pawn Код:
public OnPlayerSpawn(playerid)
{
    if(dead==true)
    {
    new i = gClosestHospital[playerid];
    SetPlayerPos(playerid, HospitalSpawns[i][0], HospitalSpawns[i][1], HospitalSpawns[i][2]);
    SetPlayerFacingAngle(playerid, HospitalSpawns[i][3]);
    SetCameraBehindPlayer(playerid);
    }

    if(dead==false)
    {
    SetPlayerPos(playerid, 1714.9219,-1948.8318,14.1172);
    SetPlayerFacingAngle(playerid, 357.5184);
    SetCameraBehindPlayer(playerid);
    return 1;
    }
    return 0;
}
Still no idea to make per player boolean. because I don't like use integer just for true false.
Reply
#5

Just make a simple variable(called an array, actually) that holds MAX_PLAYERS items. You can still keep the "bool:" tag if you wish.
Also, be sure to declare it outside any function or callback (global variable).
Example:
pawn Код:
new bool:dead[MAX_PLAYERS];
// And you use it like e.g. dead[playerid] under OnPlayerDeath
// The way you have it now will only ever work for one player alone
Reply
#6

Quote:
Originally Posted by Virtual1ty
Посмотреть сообщение
Just make a simple variable(called an array, actually) that holds MAX_PLAYERS items. You can still keep the "bool:" tag if you wish.
Also, be sure to declare it outside any function or callback (global variable).
Example:
pawn Код:
new bool:dead[MAX_PLAYERS];
// And you use it like e.g. dead[playerid] under OnPlayerDeath
// The way you have it now will only ever work for one player alone
Okay, got it. Thank you.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)