02.06.2014, 09:19
Easy and fast teleport creation
I use an easy and fast way to make teleports. I would like to share this so people can save their time. For this to work, you only need to setup the script once and you can create teleports ingame easily. I'm going to show you how every part of the script works. You need the following includes for this: So, let's start.
First, we need to create a new stock (function) on the main script that you'll run:
pawn Code:
stock TeleportPlayer (playerid, Float:X, Float:Y, Float:Z, Float:A, interior, virtualworld)
{
if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
{
SetVehiclePos(GetPlayerVehicleID(playerid), X, Y, Z + 1.0);
SetVehicleZAngle(GetPlayerVehicleID(playerid), A);
SetVehicleVirtualWorld(GetPlayerVehicleID(playerid), virtualworld);
SetPlayerVirtualWorld(playerid, virtualworld);
LinkVehicleToInterior(GetPlayerVehicleID(playerid), interior);
SetCameraBehindPlayer(playerid);
}
else
{
SetPlayerPos(playerid, X, Y, Z + 1.0);
SetPlayerFacingAngle(playerid, A);
SetPlayerVirtualWorld(playerid, virtualworld);
SetPlayerInterior(playerid, interior);
SetCameraBehindPlayer(playerid);
}
return 1;
}
pawn Code:
if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
pawn Code:
SetVehiclePos(GetPlayerVehicleID(playerid), X, Y, Z + 1.0);
pawn Code:
SetVehicleZAngle(GetPlayerVehicleID(playerid), A);
pawn Code:
SetVehicleVirtualWorld(GetPlayerVehicleID(playerid), virtualworld);
pawn Code:
SetPlayerVirtualWorld(playerid, virtualworld);
pawn Code:
LinkVehicleToInterior(GetPlayerVehicleID(playerid), interior);
pawn Code:
SetCameraBehindPlayer(playerid);
pawn Code:
else
pawn Code:
SetPlayerPos(playerid, X, Y, Z + 1.0);
pawn Code:
SetPlayerFacingAngle(playerid, A);
The remaining three are already explained above.
Create a 'Teleports' folder in your scriptfiles folder before you follow the steps below!
Now, we'll create a command (zcmd) so we can create a teleport in game. The main concept behind the command in game, is that this command will create a .txt file inside scriptfiles/Teleports that will automatically write the command in that file which will use our stock to teleport the player. Let's create the command now:
pawn Code:
CMD:tp (playerid, params[])
{
new mainString[128], userString[128];
new cmd_name[15], Float:X, Float:Y, Float:Z, Float:A, interior, virtualworld;
if (sscanf(params, "s[15]", cmd_name)) return SendClientMessage(playerid, -1, "USAGE: /tp [CMD Name]");
if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
{
GetVehiclePos(GetPlayerVehicleID(playerid), X, Y, Z);
GetVehicleZAngle(GetPlayerVehicleID(playerid), A);
virtualworld = GetPlayerVirtualWorld(playerid);
interior = GetPlayerInterior(playerid);
}
else
{
GetPlayerPos(playerid, X, Y, Z);
GetPlayerFacingAngle(playerid, A);
virtualworld = GetPlayerVirtualWorld(playerid);
interior = GetPlayerInterior(playerid);
}
format(mainString, sizeof(mainString), "CMD:%s (playerid, params[]) return TeleportPlayer(playerid, %f, %f, %f, %f, %d, %d);\r\n", cmd_name, X, Y, Z, A, interior, virtualworld);
new File:tps;
tps = fopen("Teleports/yourTPS.txt", io_append);
fwrite(tps, mainString);
fclose(tps);
format(userString, sizeof(userString), ">> Teleport %s created and saved [%f, %f, %f, %f, %d, %d]", cmd_name, X, Y, Z, A, interior, virtualworld);
SendClientMessage(playerid, -1, userString);
return 1;
}
pawn Code:
new mainString[128], userString[128];
new cmd_name[15], Float:X, Float:Y, Float:Z, Float:A, interior, virtualworld;
pawn Code:
if (sscanf(params, "s[15]", cmd_name)) return SendClientMessage(playerid, -1, "USAGE: /tp [CMD Name]");
pawn Code:
if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
{
GetVehiclePos(GetPlayerVehicleID(playerid), X, Y, Z);
GetVehicleZAngle(GetPlayerVehicleID(playerid), A);
virtualworld = GetPlayerVirtualWorld(playerid);
interior = GetPlayerInterior(playerid);
}
else
{
GetPlayerPos(playerid, X, Y, Z);
GetPlayerFacingAngle(playerid, A);
virtualworld = GetPlayerVirtualWorld(playerid);
interior = GetPlayerInterior(playerid);
}
pawn Code:
format(mainString, sizeof(mainString), "CMD:%s (playerid, params[]) return TeleportPlayer(playerid, %f, %f, %f, %f, %d, %d);\r\n", cmd_name, X, Y, Z, A, interior, virtualworld);
The '\r\n' at the end of the string is for a new line, so that all the strings are not stored in one line.
Format - Click to learn about format function and format specifiers
pawn Code:
new File:tps;
tps = fopen("Teleports/yourTPS.txt", io_append);
fwrite(tps, mainString);
fclose(tps);
We open the file using fopen(path[], mode);
Code:
io_read Reads from the file. io_write Write in the file, or create the file. Erases all contents. io_readwrite Reads the file or creates it io_append Appends (adds) to file, write-only. If the file does not exist, it is created.
As you see, we selected the mode io_append because it is used for adding to the file.
Now we write using fwrite(handle_the_special_file_variable_usedin_fop en, string_you_want_to_write);
We write (or better ''add') the mainString to the yourTPS.txt file. The file handle here is the tps variable which we use in fopen.
We close the file using fclose(handle); You must close the file after writing.
pawn Code:
format(userString, sizeof(userString), ">> Teleport %s created and saved [%f, %f, %f, %f, %d, %d]", cmd_name, X, Y, Z, A, interior, virtualworld);
SendClientMessage(playerid, -1, userString);
After using the /tp command ingame, your notepad file will look something like this:
Just copy these and paste in your script, your teleports are now created in seconds!
Functions used
GetPlayerState
SetPlayerPos
SetPlayerFacingAngle
SetPlayerInterior
SetVehicleZAngle
SetPlayerVirtualWorld
LinkVehicleToInterior
SetCameraBehindPlayer
format
fopen
fwrite
fclose
Thanks for reading.