31.08.2010, 18:50
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.
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:
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.
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.
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.
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];
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;
}
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 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.