[Help]Last Position - Spawn
#1

I was searching alot for this FS but couldn't find it.
I need some script that if you logged off/crashed you will start at the same place you was before the crash/log off
Thank you.
Reply
#2

You will need to learn to use some sort of file reader/writer like djson/dini (or sql somthng like that) then OnPlayerDissconnect save the players x,y,z co-ordinates.

Then OnPlayerSpawn SetPlayerPos to the x,y,z that was saved to a file.

I could show you a method using djson but that would probably confuse u. Your best learning than cop/paste.

I will write a way with djson and edit this post, but still think ur better learning.
Reply
#3

I was thinking about that too.. that OnPlayerDissconect will save the X,Y,Z to a file like position.save and OnPlayerSpawn SetPlayerPos to the x,y,z that was saved on position.save but, I really don't know how to do that :S I am new on Pawn but I know some stuff.. I just need someone show me how to begin this script and I'll learn how to end it.
Reply
#4

This is what i have (not tested but absolutly no reason why it should not work). Obviously put ur own conditions in OnPlayerSpawn like if player is regged ect.

Using djson FOUND HERE.

OnGameModeInit/OnFilterScriptInit
pawn Код:
djson_GameModeInit();
OnGameModeExit/OnFilterScriptExit
pawn Код:
djson_GameModeExit();
Then OnPlayerDisconnect
pawn Код:
public OnPlayerDisconnect(playerid, reason)
{
    djAutocommit(false);//correct
    new Float:x,Float:y,Float:z,fstr[35];
    GetPlayerPos(playerid,x,y,z);//put co-ords in variables x,y,z.
    format(fstr,35,"%.json",playername(playerid));//file string (players name)
    djSetFloat(fstr,"pos/x",x);//save co-ords
    djSetFloat(fstr,"pos/y",y);
    djSetFloat(fstr,"pos/z",z);
    djCommit(fstr);
    djAutocommit(true);//This is the edit make sure this is in your code(here) and not djAutocommit(false)
    return 1;
}
then OnPlayerSpawn
pawn Код:
public OnPlayerSpawn(playerid)
{
    new fstr[35];
    format(fstr,35,"%.json",playername(playerid));
    SetPlayerPos(playerid,djFloat(fstr,"pos/x"),djFloat(fstr,"pos/y"),djFloat(fstr,"pos/z"));
    return 1;
}
Then at bottom a function to return players name
pawn Код:
stock playername(playerid)
{
    new pname[MAX_PLAYER_NAME];
    GetPlayerName(playerid,pname,sizeof(pname));
    return pname;
}
Hope it works. dont see why not if not let me know what happened u may also need to save interior and vw.

Edit: i changed the later djAutocommit() to djAutocommit(true); silly mistake by me. Make sure you change the original code i posted to the edited one above (it is slghtly different).
Reply
#5

wow thank you i'll try it now and edit it if it works/not works I hope it will :P
--------------------------------
Edit:
I try this but no success, no errors or warning but still it doesn't works:
This is the FS:
pawn Код:
#include <a_samp>
#include <djson>
#pragma tabsize 0

#define COLOR_GREEN 0x33AA33AA
#define COLOR_ORANGE 0xFF9900AA
#define COLOR_RED 0xFF3366FF

#define Filterscript
#if defined Filterscript

public OnFilterScriptInit()
{
    djson_GameModeInit();
    return 1;
}

public OnFilterScriptExit()
{
    djson_GameModeExit();
   
    return 1;
}

public OnGameModeInit()
{
  djson_GameModeInit();
    return 1;
}
public OnGameModeExit()
{
 djson_GameModeExit();
 return 1;
 }
 
public OnPlayerDisconnect(playerid, reason)
{
    djAutocommit(false);
    new Float:x,Float:y,Float:z,fstr[35];
    GetPlayerPos(playerid,x,y,z);//put co-ords in variables x,y,z.
    format(fstr,35,"%.json",playername(playerid));//file string (players name)
    djSetFloat(fstr,"pos/x",x);//save co-ords
    djSetFloat(fstr,"pos/y",y);
    djSetFloat(fstr,"pos/z",z);
    djCommit(fstr);
    djAutocommit(false);
    return 1;
}
public OnPlayerSpawn(playerid)
{
    new fstr[35];
    format(fstr,35,"%.json",playername(playerid));
    SetPlayerPos(playerid,djFloat(fstr,"pos/x"),djFloat(fstr,"pos/y"),djFloat(fstr,"pos/z"));
    return 1;
}
stock playername(playerid)
{
    new pname[MAX_PLAYER_NAME];
    GetPlayerName(playerid,pname,sizeof(pname));
    return pname;
}
probebly I did something wrong, but where :S

---
edit again xD
let me try your edit
Reply
#6

Delete this,
pawn Код:
public OnGameModeInit()
{
  djson_GameModeInit();
    return 1;
}
public OnGameModeExit()
{
 djson_GameModeExit();
 return 1;
 }
Also your gamemode might be spawning the player you will have to check.

Edit: Is there a file in your scriptfiles folder called "something.json"?
Reply
#7

If it does, what need I do? I mean, I don't want that my gamemode will spawn the player, only if he is dead.
I don't know if you've played in Valhalla RP, there only if you dead it spawning you where it have to. If you just disconnect you will start at the same point where you have been before.
BTW, it doesn't work probebly cuz my GM spawns me automatically, What can I do now T-T
Reply
#8

Initiate a 500ms timer in OnPlayerSpawn inside the timer function put,
pawn Код:
SetPlayerPos(playerid,djFloat(fstr,"pos/x"),djFloat(fstr,"pos/y"),djFloat(fstr,"pos/z"));
I use this system for some of my script and it should work im not to sure about other gamemodes sorry. Timer should work.
Heres a tutorial on timers TIMER TUT

eg,
pawn Код:
public OnPlayerSpawn(playerid)
{
    SetTimerEx("dspawn", 500, false, "i",playerid);
    return 1;
}
pawn Код:
forward dspawn(playerid);
public dspawn(playerid)
{
    new fstr[35];
    format(fstr,35,"%.json",playername(playerid));
    SetPlayerPos(playerid,djFloat(fstr,"pos/x"),djFloat(fstr,"pos/y"),djFloat(fstr,"pos/z"));
    return 1;
}

EDIT: i found the problem!!!! lol im very sorry change all format lines to
pawn Код:
format(fstr,35,"%s.json",playername(playerid));//i missed the 's' in %s
I need to read my code more before i post sorry.
Reply
#9

lol its not working either. I think thats cuz my gamemode spawns me automatically I don't know how other servers fix it.. :S
================
edit:
Its finaly working.
Thank you for everything! there is a little problem with the interior, but I can handle it ;]

---
lol another problem (it will never end :P)
For example:
I am in the PD faction, when I doing /kill I need to start at the PD - But I am starting at the same place where I spawned when I logged in...
and 1 more thing, how can I auto delete the old djson-db-cache file?
Reply
#10

A simple variable would probably help you,
pawn Код:
public OnPlayerDisconnect(playerid, reason)
{
    djAutocommit(false);
    new Float:x,Float:y,Float:z,fstr[35];
    GetPlayerPos(playerid,x,y,z);
    format(fstr,35,"%s.json",playername(playerid));
    djSetFloat(fstr,"pos/x",x);
    djSetFloat(fstr,"pos/y",y);
    djSetFloat(fstr,"pos/z",z);
    djCommit(fstr);
    djAutocommit(true);
    return 1;
}

public OnPlayerSpawn(playerid)
{
    if(GetPVarInt(playerid,"firstspawn",0)//if first spawn is 0.
    {
        new fstr[35];
        format(fstr,35,"%s.json",playername(playerid));
        SetPlayerPos(playerid,djFloat(fstr,"pos/x"),djFloat(fstr,"pos/y"),djFloat(fstr,"pos/z"));
        SetPVarInt(playerid,"firstspawn",1);//set to one to make the above 'if' statment false.
    }
    return 1;
}
Hope this works im not sure about auto deleting the cache files do it by hand once a week or something (this puts some people off djson) it is faster than dini though.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)