[Tutorial] How to make a simple position saver
#1

Hello all! So today I was ultra bored and decided I should do something else than just posting answers in the scripting discussion thread so I decided to make a simple tutorial about saving positions with /savepos and then teleport back to the position you saved with /loadpos.
I don't know if a similar tutorial exists so if it does, please post here to tell me.

Okay let's start out then!

Start out by opening a completely new script in pawno by clicking new.
Now I don't know the best way of saving in variables but I decided to include the enum in this tutorial so this is the first thing you need.

pawn Код:
enum posinfo
{
    Float:posx,
    Float:posy,
    Float:posz,
    Float:posangle,
};
new PosInfo[MAX_PLAYERS][posinfo];
Now to explain a bit. This is a way of saving in variables and also a great way of saving if you want to save stats etc. You can store values, strings and all that stuff into variables. As the positions in sa-mp are floats we need to add Float: in front of every variable.
Copy and paste this somewhere above "#if defined FILTERSCRIPT".


The next step is to make sure that the variable isn't saved when a player disconnects. By saying "saved" I mean that when a player disconnects and another player connects he's got the same coords in the pos variables as the player disconnected. We want to make sure that when a player connects the variables are set to 0.

Preventing this is as simple as this:

pawn Код:
public OnPlayerDisconnect(playerid, reason)
{
    PosInfo[playerid][posx] = 0;
    PosInfo[playerid][posy] = 0;
    PosInfo[playerid][posz] = 0;
    PosInfo[playerid][posangle] = 0;
    return 1;
}
It doesn't really matter if you put this under OnPlayerConnect or OnPlayerDisConnect. It has still got the same effect.


New up are the /savepos and /loadpos command.
I assume you've allready learned about command so I will only explain the variables in the commands.

This is the /savepos command.

pawn Код:
if(strcmp(cmdtext, "/savepos", true) ==0)
    {
      if(IsPlayerConnected(playerid))
      {
        new Float:x, Float:y, Float:z, Float:angle;
        GetPlayerPos(playerid,x,y,z);
        GetPlayerFacingAngle(playerid, angle);
        PosInfo[playerid][posx] = x;
        PosInfo[playerid][posy] = y;
        PosInfo[playerid][posz] = z;
        PosInfo[playerid][posangle] = angle;
        SendClientMessage(playerid, 0x33AA33AA, "You have successfully saved your position. To teleport back here, use /loadpos!");
      }
      return 1;
    }

In the command the coordinates are saved into the variables. Not really hard, I'm sure you'll understand it.


This is the /loadpos command.


pawn Код:
if(strcmp(cmdtext, "/loadpos", true) ==0)
    {
      if(IsPlayerConnected(playerid))
      {
        if(PosInfo[playerid][posx] != 0 && PosInfo[playerid][posy] != 0 && PosInfo[playerid][posz] != 0)
        {
           SetPlayerPos(playerid,PosInfo[playerid][posx],PosInfo[playerid][posy],PosInfo[playerid][posz]);
           SetPlayerFacingAngle(playerid, PosInfo[playerid][posangle]);
           SendClientMessage(playerid, 0x33AA33AA, "You have successfully teleported to your saved position!");
        }
        else return SendClientMessage(playerid, 0xe7553dAA, "You need to save a position with /save at first!");
      }
      return 1;
    }
As you can see the variables are checked if they are not 0 after checking if the player is connected. This is to prevent players from doing /loadpos directly when connecting without doing /savepos so that they appear underthe barn in the coordinates 0,0,0. If the variables are 0,0,0, it sends a message to the player.
As you can see the variables are used in the SetPlayerPos command and SetPlayerFacingAngle.




That's pretty much it and I hope you learned something if you are new to scripting. Please post here if you have any problems regarding this tutorial. I will be more than glad to answer.
Reply
#2

good but there is a simplier way:

Код:
if(strcmp(cmdtext, "/savepos", true) ==0)    
{      
if(IsPlayerConnected(playerid))      
{             
     new Float:x,Float:y,Float:z;
    GetPlayerPos(playerid,x,y,z);
    SetPVarFloat(playerid,"xpos",x); // save X POS
    SetPVarFloat(playerid,"ypos",y); // save Y POS
    SetPVarFloat(playerid,"zpos",z); // save Z POS
    SetPVarInt(playerid,"int",GetPlayerInterior(playerid));//get interior
    SendClientMessage(playerid,0x33AA33AA,"position succefully saved! use /loadpos to get to it,"
}
      return 1;    
}

	if (strcmp("/loadpos", cmdtext, true, 10) == 0)
	{
	SetPlayerPos(playerid, GetPVarFloat(playerid,"xpos"), GetPVarFloat(playerid,"ypos"), GetPVarFloat(playerid,"zpos"));
	SetPlayerInterior(playerid, GetPVarInt(playerid,"int"));
 	SendClientMessage(playerid, 0x33AA33AA, "Saved Position Loaded.");
              return 1;
	}
Reply
#3

dont works...i have four errors!

error 017: undefined symbol "PosInfo"
warning 215: expression has no effect
error 001: expected token: ";", but found "]"
error 029: invalid expression, assumed zero
fatal error 107: too many error messages on one line
Reply
#4

pmk1!! your works! thx!!
Reply
#5

Very nice tutorls,
simplier way:

pawn Код:
new Float: X_P[MAX_PLAYERS], Float:Y_P[MAX_PLAYERS], Float: Z_P[MAX_PLAYERS];

//  In commandtext
if(!strcmp(cmdtext,"/SavePOS", true))
{
    GetPlayerPos(playerid, X_P[playerid],Y_P[playerid],Z_P[playerid]);
    return true;
}

if(!strcmp(cmdtext,"/LoadPOS", true))
{
    SetPlayerPos(playerid, X_P[playerid],Y_P[playerid],Z_P[playerid]);
    return true;
}
xDDDD
Reply
#6

can you make a auto saving version pls?
Reply
#7

what about making it without commands? i know ur answer but give an ex: if u got register system so it load auto when u enter
Reply
#8

Nice script I live servers with position loaders. This is a nice tutorial and I am defintly going to use this!
Reply
#9

Edit: Ops, deleted Misunderstood

Edit2: Ahhhhhh, nice script! I HATE it when there is a nice hotspot and i can't save the place >: (

-J
Reply
#10

Okay this is all nice and beautiful, but what if I want all players position to be saved automatically , and loaded automatically each time they log in , disconnect?
Reply
#11

Nice TUT
Reply
#12

Hey i did every thing you said but when i compile i get warning 225: unreachable code for this

public OnPlayerDisconnect(playerid, reason)
{
{
PosInfo[playerid][posx] = 0;
PosInfo[playerid][posy] = 0;
PosInfo[playerid][posz] = 0;
PosInfo[playerid][posangle] = 0;
return 1;
}
return 1;
}

Help me please!!!

I am a noob at scripting but i am learning
Reply
#13

Quote:
Originally Posted by Las Venturas CNR
View Post
Make it:

pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    PosInfo[playerid][posx] = 0;
    PosInfo[playerid][posy] = 0;
        PosInfo[playerid][posz] = 0;
        PosInfo[playerid][posangle] = 0;

return 1;
}
Forum messed up my indentation...
plus, if you look closely you will see

pawn Code:
return 1;
}
return 1;
}
at the end of the code, the first return will automatically return to the beginning of the callback in return it will not call the second return. Another reason could be, you left out portions of the code lol.
Reply
#14

Quote:
Originally Posted by pmk1
Посмотреть сообщение
good but there is a simplier way:

Код:
if(strcmp(cmdtext, "/savepos", true) ==0)    
{      
if(IsPlayerConnected(playerid))      
{             
     new Float:x,Float:y,Float:z;
    GetPlayerPos(playerid,x,y,z);
    SetPVarFloat(playerid,"xpos",x); // save X POS
    SetPVarFloat(playerid,"ypos",y); // save Y POS
    SetPVarFloat(playerid,"zpos",z); // save Z POS
    SetPVarInt(playerid,"int",GetPlayerInterior(playerid));//get interior
    SendClientMessage(playerid,0x33AA33AA,"position succefully saved! use /loadpos to get to it,"
}
      return 1;    
}

	if (strcmp("/loadpos", cmdtext, true, 10) == 0)
	{
	SetPlayerPos(playerid, GetPVarFloat(playerid,"xpos"), GetPVarFloat(playerid,"ypos"), GetPVarFloat(playerid,"zpos"));
	SetPlayerInterior(playerid, GetPVarInt(playerid,"int"));
 	SendClientMessage(playerid, 0x33AA33AA, "Saved Position Loaded.");
              return 1;
	}
PVars are slower than normal variables. Also you could save it to file, not to variable, it'd be better .
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)