[Tutorial] Creating a Proper Teleport Command.
#1

Introduction
In this tutorial, I'll be teaching you how to make a proper teleporting command. Many people don't think about a lot of key factors of teleportation. I'll be going over all of those in this tutorial. This tutorial is really good for beginners.

Few things before we start
The key to every good script is not to repeat yourself. If you feel that you're using a piece of code again and again, That code should be simplified. This is the case with teleportation. Therefore, I'll be using a function to simply the Teleportation process.

So, Let's begin.

Creating the Teleport Function
PHP код:
Teleport(playeridFloat:xFloat:yFloat:zFloat:angleinteriorvirtualworldbool:ignoreVehicle) {
// Declaring the function and defining its parameters. 
As you can see from the above code, This function has 8 parameters. It might seem a lot, But each of those parameter play a vital role in the teleportation process.
  • playerid - The id of the player you wish to teleport.
  • Float:x - The 'X' coordinate of the place you wish to teleport to.
  • Float:y - The 'Y' coordinate of the place you wish to teleport to.
  • Float:Z - The 'Z' coordinate of the place you wish to teleport to.
  • Float:angle - The angle which the player should face when he teleports.
  • interior - The interior id of the place you wish to teleport to.
  • virtualworld - The virtual world of the place you wish to teleport to.
  • bool:ignoreVehicle - Tells the script if it should teleport the player alone or teleport the vehicle as well.
We use 'Float' for decimals and 'bool' for true or false

Next up, We have to check if the player is in a vehicle & the player is the driver of the vehicle.

PHP код:
Teleport(playeridFloat:xFloat:yFloat:zFloat:angleinteriorvirtualworldbool:ignoreVehicle) {
    if(!
ignoreVehicle && IsPlayerInAnyVehicle(playerid) && GetPlayerState(playerid) == PLAYER_STATE_DRIVER) {
    } else {
    }

We created a basic if else statement. And we check if the player is in a vehicle, If he is a driver and If the teleport allows vehicle transportation.

IsPlayerInAnyVehicle() - Returns 1 or 0 if the player is in a Vehicle or not
GetPlayerState() - Gets the player's current state as an integer. We compare that with the 'PLAYER_STATE_DRIVER' integer. If they are the same, The statement holds true.

ignoreVehicle - We define this when we create the teleport command. If we set it as 'true', then the function doesn't check for vehicle transportation. If it is set to 'false', then the function checks for vehicle transportation.

We use '&' to check if all the three are true. If it is true, Then the if statement runs. Or else, The 'else' statement runs.

Now we do this:

PHP код:
Teleport(playeridFloat:xFloat:yFloat:zFloat:angleinteriorvirtualworldbool:ignoreVehicle) {
    if(!
ignoreVehicle && IsPlayerInAnyVehicle(playerid) && GetPlayerState(playerid) == PLAYER_STATE_DRIVER) {
        new 
vehicleid GetPlayerVehicleID(playerid); // Gets the vehicleid of the vehicle in which the player is.
        
SetVehiclePos(vehicleidxyz); //Sets the vehicle's position.
        
SetVehicleZAngle(vehicleidangle); //Sets the vehicle's Facing Angle.
        
LinkVehicleToInterior(vehicleidinterior); // Links the vehicle to the interior so that the vehicle is visible in that interior.
        
SetVehicleVirtualWorld(vehicleidvirtualworld); // Sets the vehicle's Virtual World.
    
} else {
    }

Many people set the interior and virtual world while teleporting. This will cause problems if the player is trying to teleport to place with a different interior id.

Now if the 'if' statement fails, i.e, If the player is not in a vehicle, If the player is not a driver, and if ignoreVehicle is set to true, The else statement will run.

PHP код:
Teleport(playeridFloat:xFloat:yFloat:zFloat:angleinteriorvirtualworldbool:ignoreVehicle) {
    if(!
ignoreVehicle && IsPlayerInAnyVehicle(playerid) && GetPlayerState(playerid) == PLAYER_STATE_DRIVER) {
        new 
vehicleid GetPlayerVehicleID(playerid); // Gets the vehicleid of the vehicle in which the player is.
        
SetVehiclePos(vehicleidxyz); //Sets the vehicle's position.
        
SetVehicleZAngle(vehicleidangle); //Sets the vehicle's Facing Angle.
        
LinkVehicleToInterior(vehicleidinterior); // Links the vehicle to the interior so that the vehicle is visible in that interior.
        
SetVehicleVirtualWorld(vehicleidvirtualworld); // Sets the vehicle's Virtual World.
    
} else {
        
SetPlayerPos(playeridxyz); // Sets the player's position.
        
SetPlayerFacingAngle(playeridangle); // Sets the facing angle of the player
    
}

Now, whatever we put outside the 'If' & 'else' statement, It doesn't take into consideration if the player is in a vehicle or not. There we have to set the player's interior id and virtual world.

PHP код:
Teleport(playeridFloat:xFloat:yFloat:zFloat:angleinteriorvirtualworldbool:ignoreVehicle) {
    if(!
ignoreVehicle && IsPlayerInAnyVehicle(playerid) && GetPlayerState(playerid) == PLAYER_STATE_DRIVER) {
        new 
vehicleid GetPlayerVehicleID(playerid); // Gets the vehicleid of the vehicle in which the player is.
        
SetVehiclePos(vehicleidxyz); //Sets the vehicle's position.
        
SetVehicleZAngle(vehicleidangle); //Sets the vehicle's Facing Angle.
        
LinkVehicleToInterior(vehicleidinterior); // Links the vehicle to the interior so that the vehicle is visible in that interior.
        
SetVehicleVirtualWorld(vehicleidvirtualworld); // Sets the vehicle's Virtual World.
    
} else {
         
SetPlayerPos(playeridxyz); // Sets the player's position.
           
SetPlayerFacingAngle(playeridangle); // Sets the facing angle of the player
    
}
    
SetPlayerInterior(playeridinterior); // Sets the player's Interior.
    
SetPlayerVirtualWorld(playeridvirtualworld); // Sets the player's virtual world

And that's the whole function. It might look like a lot, but it is really useful.

Implementing the function in to the script
Now it's time to use the function we just created. For this example, I'll teleport the player to San Fierro Airport when she types "/sfa".

So, Under onPlayerCommandText, We add this.

PHP код:
public OnPlayerCommandText(playeridcmdtext[])
{
    if (
strcmp("/sfa"cmdtexttrue10) == 0)
    {
        
Teleport(playerid, -1546.8569,-54.5519,14.1440,317.285900false);
        return 
1;
    }
    return 
0;

And That's it. It is that simple to teleport a player. It sets all the interior id and virtual world for you. And since we set the ignoreVehicle to false in this case, It will teleport the vehicle as well if the player is in one.

Remember The syntax for the function is:

PHP код:
Teleport(playerid,YZAngleInteriorVirtualworldIgnoreVehicle
Now go ahead and add more teleportations.

PHP код:
public OnPlayerCommandText(playeridcmdtext[])
{
    
    if (
strcmp("/sfa"cmdtexttrue10) == 0)
    {
        
Teleport(playerid, -1546.8569,-54.5519,14.1440,317.285900false); // San Fierro Airport
        
return 1;
    }
    if (
strcmp("/lsa"cmdtexttrue10) == 0)
    {
        
Teleport(playerid1781.0963,-2605.5422,13.5469,56.926900false); // Los Santos International 
        
return 1;
    }
    if (
strcmp("/lva"cmdtexttrue10) == 0)
    {
        
Teleport(playerid1389.1277,1494.7058,10.8203,353.993100false); // Las Venturas Airport
        
return 1;
    }
    if (
strcmp("/tune"cmdtexttrue10) == 0)
    {
        
Teleport(playerid2644.976, -2030.90313.554000false); // Tune Garage
        
return 1;
    }
    if (
strcmp("/mansion"cmdtexttrue10) == 0)
    {
        
Teleport(playerid1299.14,  -794.77,  1084.00050true); // Teleports to Madd Dogg's Mansion. It's interior id is 5, and we are disabling vehicle transportation. 
        
return 1;
    }
    return 
0;

A list of all Interior ID's can be found here.

Have fun teleporting.

If there's any error in the code, Leave a comment and I'll fix it. I have tested this code and it works fine.

Credits

SA:MP Wiki
Reply
#2

Nice tut +REPD
Reply
#3

why it should be stock?
Reply
#4

Quote:
Originally Posted by TaiRinsuru
Посмотреть сообщение
Nice tut +REPD
Thanks.

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
why it should be stock?
I actaully was thinking about that. Whether I should use stock or not. But I ended up using stock anyway. Does it matter what I use?
Reply
#5

Quote:
Originally Posted by DarkSkull
Посмотреть сообщение
Thanks.



I actaully was thinking about that. Whether I should use stock or not. But I ended up using stock anyway. Does it matter what I use?
Do you know what it means? And if not, why are you using something without knowing what it means?
Reply
#6

Quote:
Originally Posted by Infinity
Посмотреть сообщение
Do you know what it means? And if not, why are you using something without knowing what it means?
are you doing every kind of things like that? mistake is mistake i think he understood it by now...
Reply
#7

Quote:
Originally Posted by GhostHacker
Посмотреть сообщение
are you doing every kind of things like that? mistake is mistake i think he understood it by now...
It's not a mistake, he's just asking why he chose to use a stock apposed to just the raw function.
Reply
#8

Quote:
Originally Posted by PawnHunter
Посмотреть сообщение
Aye nice tutorial for new scripters, about stock, it's a keyword that tells compiler to ignore 'function is not used' warnings.
Quote:
Originally Posted by Infinity
Посмотреть сообщение
Do you know what it means? And if not, why are you using something without knowing what it means?
Quote:
Originally Posted by GhostHacker
Посмотреть сообщение
are you doing every kind of things like that? mistake is mistake i think he understood it by now...
I get what a stock it now. I thought it's used to declare a function. I learned PHP and we use function to declare a function over there. Sorry guys. I'll update the thread right now.
Reply
#9

Quote:
Originally Posted by DarkSkull
Посмотреть сообщение
I actaully was thinking about that. Whether I should use stock or not. But I ended up using stock anyway. Does it matter what I use?
not just it ignores the warning it also ignore the function out of compiling source and thats why its called STOCK
Reply
#10

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
not just it ignores the warning it also ignore the function out of compiling source and thats why its called STOCK
Oh thanks. Didn't know that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)