[Tutorial] Easy and fast teleport creation
#1

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;
}
Explanation
pawn Code:
if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
If the player is the driver of the vehicle or if the current state of the player is driver of any vehicle, continue

pawn Code:
SetVehiclePos(GetPlayerVehicleID(playerid), X, Y, Z + 1.0);
Set the the vehicle position to X, Y, Z co-ordinates of the player vehicle ID, which we put in the the stock. We add 1.0 to Z because sometimes the objects won't load quickly, this will make the player to jump and avoid getting stuck into map or floor.

pawn Code:
SetVehicleZAngle(GetPlayerVehicleID(playerid), A);
Set vehicle rotation angle of the player vehicle ID to A co-ordinate.

pawn Code:
SetVehicleVirtualWorld(GetPlayerVehicleID(playerid), virtualworld);
Set vehicle's virtual world to virtualworld (variable which we use in the stock) .

pawn Code:
SetPlayerVirtualWorld(playerid, virtualworld);
Set player virtual world to virtualworld. The player and vehicle's virtual world should be the same. Players will only be able to see vehicles in their own virtual world.

pawn Code:
LinkVehicleToInterior(GetPlayerVehicleID(playerid), interior);
Link the vehicle to the interior, players will only be able to see the vehicles in the same virtual world.

pawn Code:
SetCameraBehindPlayer(playerid);
Set the camera place behind the player, basically restores the camera position.

pawn Code:
else
If the player is not a driver, then..

pawn Code:
SetPlayerPos(playerid, X, Y, Z + 1.0);
Set the player position to X, Y, Z co-ordinates.

pawn Code:
SetPlayerFacingAngle(playerid, A);
Set the player facing angle or rotation to 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;
Here, we're just declaring some new variables we're going to use in getting the positon, interior, virtual world.

pawn Code:
if (sscanf(params, "s[15]", cmd_name)) return SendClientMessage(playerid, -1, "USAGE: /tp [CMD Name]");
If the /tp command doesn't have a CMD name parameter, the player will get message, 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);
    }
This is exactly what we did in our stock, but here, we store the position, rotation in X, Y, Z and A using functions starting with 'Get' (GetPlayerPos, GetVehiclePos etc). The virtual world and interior are stored in variables virtualworld and interior respectively.

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);
Here, we format the mainString so we can include the values of cmd_name, X, Y, Z, A, interior and virtualworld in the mainString. We'll put this string in the .txt file..
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);
This is where we do our main writing, we create a variable, make sure that you put the 'File:' tag before that variable, this is what makes it different from other variables, this will be used in functions associated with files only.
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.
Source: SA-MP wiki, fopen.
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);
Here we format another string (userString) and send it to the player in game as a confirmation.

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.
Reply
#2

Nice and simple
Reply
#3

This seems pretty solid, except:

Quote:
Originally Posted by SyntaxQ
View Post
we need to create a new stock (function) on the main script
There is zero need to use stock in a gamemode; it is a modifier that tells the compiler to hide the function if it's not used. But you are using it, hence stock is redundant. If you do end up forgetting to use the function the compiler will tell you, so you can either use the function or remove it. This way you avoid cluttering your gamemode with stuff you're not even using.

Otherwise good job.
Reply
#4

Pretty neat. A few points -

1) Instead of repeatedly calling GetPlayerVehicleID(playerid) in the TeleportPlayer function, (almost 5 times) why not use a simply variable, would reduce the time. Not a big deal, but still!
2) Also, since your tutorial is 'easy' teleports - there are even better ways of doing this, cancelling the need to even copy paste the commands into your script.

Nice job!
Reply
#5

Thanks!

Quote:
Originally Posted by RajatPawar
View Post
2) Also, since your tutorial is 'easy' teleports - there are even better ways of doing this, cancelling the need to even copy paste the commands into your script.
I tried to make something like this, but it crashes the server if i try to open 'gamemodes/any.pwn' (I think we can only open files inside scriptfiles folder, probably it crashed because there wasn't any folder named filterscripts) So, I tried it other way around. I made a .pwn file and wrote the includes (using fwrite in the script) to that once, and that same process of writing the main CMD string will continue. Using this, the user can straight away compile and put the .amx in the filterscripts folder. But still the user need to put the TeleportPlayer stock into that filterscript. Now, I'm actually trying to find how can I even the cancel the need of copy-pasting the stock. I have a thought in my mind, just trying to see if it'll work.
Reply
#6

nice job
Reply
#7

Nicely done.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)