29.04.2012, 17:49
(
Last edited by Infinity; 05/05/2012 at 08:52 PM.
)
Infinity's Tutorials
Tips and Tricks regarding 'logical thinking'
Tips and Tricks regarding 'logical thinking'
Who am I?/What is my goal?
(Skip this part to get to the actual tutorial)
Hello. I am Infinity. I've been playing SA-MP for quite some time now, having owned/created a few servers in the process. (Namely my co-ownership at Son of Mini Missions)
Outside of SA-MP I like to do stuff with computers. I program small applications for school, but I am in no way an 'expert' or 'pro'. What I am good at is getting myself out of a mess. Like when errors start popping up, or when I have to try something new for one of my school projects and even when I'm just scripting something for SA-MP.
In this tutorial I'm going to try and explain that scripting isn't as hard and terrifying as most people here think. Even when people know the basics of scripting in PAWN, they still take every opportunity to come to this forum asking others to make something for their script. While this is fine in theory, people here can't make everything for you exactly how you want it. And even though some do an amazing job, I still prefer it when people make those features themselves, as it fits in better with the rest of the code and is easier to understand for the user. (Not to mention the hassle you'll have with editing it later on when somebody else made it.)
So, everybody get your learning goggles, 'cause here we go!
What you'll need?
In this tutorial I'm going to (try and) teach you how to make stuff yourself, making you less dependent of people here on the forums.
- Basic understanding of how to script in PAWN
- Pen and paper
- Pawno (with the SA-MP libraries)
- The SA-MP Wiki (In this tutorial I'll be using the Functions and Callbacks pages)
Step 1: What are you going to make?
The first step to make any feature is (obviously) to come up with the feature. For this tutorial, we are going to make a teleport command that teleports you to the Four Dragons Casino in Las Venturas.
Step 2: Splitting it all up
This may not seem like much, but some features could turn out to be quite long and intimidating when you'd write them down. Splitting up each individual part in said feature will make it so much easier to script. So, get your paper ready to split and try to split up the example feature.
Quote:
Example Feature: A teleport command that teleports you to the Four Dragons Casino in Las Venturas. |
Quote:
Example Feature - split up: It's a command. It teleports the player that uses it. The location where the player is teleported to is the Four Dragons Casino in Las Venturas. |
Step 3.1: Transforming it into a script - The command
Now you have all these individual parts, making a script should seem more like something logical, and less like magic.
The way I'd do it is to take every part and just try to find out how i could do this. (This is where the Wiki comes in handy)
The first part tells us it's a command. In other words, the player has to do something, and the server reacts. This is what Callbacks are all about. They are called/executed when a player does something.
An example: OnPlayerDeath is executed when a player dies, OnPlayerConnect is executed when a player connects to the server.
Now we're looking for a Callbacks that gets executed when a player enters a command. When searching the Callbacks page for everything related to commands, you'll find 2 callbacks: OnPlayerCommandText and OnRconCommand. Since this has nothing to do with a Rcon command, we'll try OnPlayerCommandText. When you click this link, you'll see a brief description of the callback and an example. (The latter is also found in new.pawn)
By looking at the page, you'll (hopefully) figure out that you can make the teleport command by placing the following code in the OnPlayerCommandText callback.
pawn Code:
if(!strcmp(cmdtext, "/tele", true))
{
//Add the rest of the parts here
return 1;
}
Step 3.2: Transforming it into a script - Teleporting the player
So, we have a command. But it doesn't do anything yet... Let's do something about that!
What we wanted is that the player gets teleported to the Four Dragon's Casino when he enters the command. But how do we do that, teleporting? To begin with, what is teleporting? Teleporting is setting the position of a player to a specific set of coordinates.
Unlike with callbacks, it's the server who does something and the player 'reacts'. This means we'll have to look at Functions. FYI, the functions list is huge. Looking at it manually would take a long time. The best way to do this is by using CTRL+F and entering some keywords for the needed function. For example, we don't know what they've called the function for teleporting, but what I know is that most if not all functions that do something to something (changing a players health/armor/money for example) start with 'Set'. That seems like a good place to start searching. If you pay attention, you'll see that the functions list is sorted by alphabet, so this makes looking for something somewhat easier.
So, once you've looked through all of the SetPlayer functions, you've likely stumbled on SetPlayerPos. Just like with the callbacks, the wiki page for SetPlayerPos has a brief explanation of the function's parameters and an example so you'll know how to use it. So, now we're going to enter it in the command, right? But, ehhh... what will we set the position to? We need coordinates!
We know the teleport command should lead to the Four Dragons Casino in Las Venturas. To get the coordinates, you can do two things:
You can use the SA-MP Debug client (You can find it in the folder you've installed SA-MP in), go to the casino and use the '/save' command to save the coordinates. This way you can get the specific coordinates of the place you want the player to teleport to.
Or, you can be lazy like me and get the general coordinates from the Wiki. This should get you the following coordinates:
Quote:
Originally Posted by Coordinates of the Four Dragons Casino
2016.2699, 1017.7790, 996.8750
|
pawn Code:
if(!strcmp(cmdtext, "/tele", true))
{
SetPlayerPos(playerid,2016.2699, 1017.7790, 996.8750);
return 1;
}
Using the same search 'techniques' used to find SetPlayerPos, you can find the needed function to change a players' interior.
(Summary: It changes something, so it'll most likely start with 'Set'. It affects the player, so it'll most likely be something along the lines of 'SetPlayer'. It changes the interior(ID), so the name of the function will most likely have something related to interior(ID) in the name.)
Eventually you'll find SetPlayerInterior. Pretty straight-forward, isn't it?
The InteriorID can be found on the same wiki page as the coordinates. In this example it's 10. So, let's add this to the command:
pawn Code:
if(!strcmp(cmdtext, "/tele", true))
{
SetPlayerInterior(playerid, 10);
SetPlayerPos(playerid,2016.2699, 1017.7790, 996.8750);
return 1;
}
Step 4: Testing
So, that's the command. Now you can teleport a player to the casino to do some necessary gambling. Now you'll just have to compile the script and hope/test everything works the way you want it. And if it doesn't, it's OK to ask! People here may not always want to help you making the features, but they're almost jumping to help people fix bugs.

(Detailed tutorial about testing and debugging coming soon!)
But making a teleport command wasn't the goal of the tutorial. It was to teach you to do these things on your own, relying less on other people and more on your own abilities. And I really hoped that I've helped you who've mde it this far, even if it's just a little.

Thanks for reading, and good luck to all of you out there!
~Infinity