Help with Saving/Loading Player Positions. EASY
#9

I made a couple of functions for you. I've tested a little bit and it works fine.

IMPORTANT: You need to have a folder in your scriptfiles directory called "lastpos". If you don't your server will crash.

The following code has a couple of test commands if you remove them you can remove the zcmd include. Other than that it requires sscanf. Could have done it without but this was faster.

pawn Код:
#include <a_samp>
#include <sscanf2>
#include <zcmd>

enum E_POSITION_DATA
{
    Float:e_fLastX,
    Float:e_fLastY,
    Float:e_fLastZ,
    Float:e_fLastAngle,
    e_iLastWorld,
    e_iLastInt,
};
new PositionData[MAX_PLAYERS][E_POSITION_DATA];

stock SaveLastPos(playerid)
{
    new
        File:PosFile,
        fOutput[64],
        FileDest[MAX_PLAYER_NAME + 64];
    GetPlayerPos(playerid, PositionData[playerid][e_fLastX], PositionData[playerid][e_fLastY], PositionData[playerid][e_fLastZ]);
    GetPlayerFacingAngle(playerid, PositionData[playerid][e_fLastAngle]);
    PositionData[playerid][e_iLastWorld] = GetPlayerVirtualWorld(playerid);
    PositionData[playerid][e_iLastWorld] = GetPlayerInterior(playerid);
    format(fOutput, sizeof(fOutput), "%f|%f|%f|%f|%d|%d",
        PositionData[playerid][e_fLastX],
        PositionData[playerid][e_fLastY],
        PositionData[playerid][e_fLastZ],
        PositionData[playerid][e_fLastAngle],
        PositionData[playerid][e_iLastWorld],
        PositionData[playerid][e_iLastInt]
    );
    GetPlayerName(playerid, FileDest, MAX_PLAYER_NAME);
    format(FileDest, sizeof(FileDest), "/lastpos/%s.txt", FileDest);
    PosFile = fopen(FileDest, io_write);
    fwrite(PosFile, fOutput);
    fclose(PosFile);
}

stock LoadLastPos(playerid)
{
    new
        File:PosFile,
        PosData[MAX_PLAYER_NAME + 64];
    GetPlayerName(playerid, PosData, MAX_PLAYER_NAME);
    format(PosData, sizeof(PosData), "/lastpos/%s.txt", PosData);
    if(fexist(PosData))
    {
        PosFile = fopen(PosData, io_read);
        fread(PosFile, PosData);
        fclose(PosFile);
        sscanf(PosData, "p<|>ffffdd",
            PositionData[playerid][e_fLastX],
            PositionData[playerid][e_fLastY],
            PositionData[playerid][e_fLastZ],
            PositionData[playerid][e_fLastAngle],
            PositionData[playerid][e_iLastWorld],
            PositionData[playerid][e_iLastInt]
        );
        SetPlayerPos(playerid, PositionData[playerid][e_fLastX], PositionData[playerid][e_fLastY], PositionData[playerid][e_fLastZ]);
        SetPlayerFacingAngle(playerid, PositionData[playerid][e_fLastAngle]);
        SetPlayerVirtualWorld(playerid, PositionData[playerid][e_iLastWorld]);
        SetPlayerInterior(playerid, PositionData[playerid][e_iLastInt]);
        SetCameraBehindPlayer(playerid);
    }
    else
    {
        //position not saved process as normal.
    }
}

COMMAND:savepos(playerid, params[])
{
    SaveLastPos(playerid);
    return 1;
}

COMMAND:loadpos(playerid, params[])
{
    LoadLastPos(playerid);
    return 1;
}
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 3 Guest(s)