SA-MP Forums Archive
Nearest hospital spawn - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Nearest hospital spawn (/showthread.php?tid=436751)



Nearest hospital spawn - Hipflop - 12.05.2013

Hi guys,

I have a question about my code. This is supposed to spawn you at the nearest hospital. But when you die, you get the "Out of world bounds" in your screen. Can anybody please say what is wrong?

pawn Код:
new dead[MAX_PLAYERS];
new hospital;

new Float:Hospitals[2][3] = {
{2029.665771, -1418.884765, 16.992187},
{1178.691772, -1324.275268, 14.132597}
};
pawn Код:
stock Float:PlayerDistanceToPoint(playerid, Float:x, Float:y, Float:z)
{
    new Float:coo[3];
    GetPlayerPos(playerid, coo[0], coo[1], coo[2]);
    return floatsqroot(((coo[0] - x) * (coo[0] - x)) + ((coo[1] - y) * (coo[1] - y)) + ((coo[2] - z) * (coo[2] - z)));
}
pawn Код:
stock ClosestHospital(playerid)
{
    new hospitalid, Float:closest = PlayerDistanceToPoint(playerid, Hospitals[0][0], Hospitals[0][1], Hospitals[0][2]);
    for(new i = 1; i < sizeof(Hospitals); i++)
    {
        if(PlayerDistanceToPoint(playerid, Hospitals[i][0], Hospitals[i][1], Hospitals[i][2]) < closest)
        {
            closest = PlayerDistanceToPoint(playerid, Hospitals[i][0], Hospitals[i][1], Hospitals[i][2]);
            hospitalid = i;
        }
    }
    return hospitalid;
}
pawn Код:
public OnPlayerDeath(playerid, killerid, reason)
{
    dead[playerid] = 1;
    hospital = ClosestHospital(playerid);
    return 1;
}
pawn Код:
public OnPlayerSpawn(playerid)
{
        if(dead[playerid] == 1)
    {
        new hid = hospital;
        SetPlayerPos(playerid, Hospitals[hid][0], Hospitals[hid][1], Hospitals[hid][2]);
        dead[playerid] = 0;
    }
}
Thanks in advance!


AW: Nearest hospital spawn - Skimmer - 12.05.2013

pawn Код:
hospital = ClosestHospital(playerid);
Here's your problem you need create this variable for each players.
Create this variable top of your script like this
pawn Код:
new hospital[MAX_PLAYERS];
Then change it to
pawn Код:
hospital[playerid] = ClosestHospital(playerid);
Change OnPlayerSpawn to this.
pawn Код:
public OnPlayerSpawn(playerid)
{
    if(dead[playerid] == 1)
    {
        new hid = hospital[playerid];
        SetPlayerPos(playerid, Hospitals[hid][0], Hospitals[hid][1], Hospitals[hid][2]);
        dead[playerid] = 0;
    }
}



Re: Nearest hospital spawn - Hipflop - 12.05.2013

Thanks for your quick response.

It still gives the same error, "Out of world bounds".

Any other idea's?


AW: Nearest hospital spawn - Skimmer - 12.05.2013

Out of world bounds?

When you get this error? After Spawn or death etc?


Re: Nearest hospital spawn - Hipflop - 12.05.2013

After I spawned


AW: Nearest hospital spawn - Skimmer - 12.05.2013

Do you have AddPlayerClass() function on OnGameModeInit() callback?

Are you sure the positions (x, y, z) of the hospital are right?


Re: Nearest hospital spawn - Hipflop - 12.05.2013

I do not have AddPlayerClass().

And the coordinates for the hospitals are fine. They are the ones in Los Santos.


AW: Nearest hospital spawn - Skimmer - 12.05.2013

Quote:

I do not have AddPlayerClass().

Well, this is your problem.
Add this fast below OnGameModeInit() callback.
pawn Код:
AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
AddPlayerClass


Re: Nearest hospital spawn - Hipflop - 12.05.2013

Why should I have the AddPlayerClass()?
I don't get what that has to do with it.

It's for a roleplay server I'm making at the moment. And everything is fine, register, login etc.
Only the spawn when you die is not working properly.


AW: Nearest hospital spawn - Skimmer - 12.05.2013

You should have at least one AddPlayerClass() function on OnGameModeInit(), because then you'll get this Error.
I know it, because i had it too.

Yes, this is for RolePlays, but you can use SetPlayerPos on OnPlayerSpawn. Do what i say and test it.