SA-MP Forums Archive
[Tutorial] [InfiniTuts - Tutorials for beginners] Tips and Tricks regarding 'logical thinking' - Making your own script - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] [InfiniTuts - Tutorials for beginners] Tips and Tricks regarding 'logical thinking' - Making your own script (/showthread.php?tid=338373)



[InfiniTuts - Tutorials for beginners] Tips and Tricks regarding 'logical thinking' - Making your own script - Infinity - 29.04.2012

Infinity's Tutorials
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.
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.

What'll you want to have is something that looks like this:
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.

See what I did there? This way it's much easier to figure out how you'd work it out script-wise. (And no, it doesn't have to be exactly like this. As long as you understand it, and can figure out how to script it it's fine!)


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
With this we can edit the command made in the previous step to the following:
pawn Code:
if(!strcmp(cmdtext, "/tele", true))
{
   SetPlayerPos(playerid,2016.2699, 1017.7790, 996.8750);
   return 1;
}
So, now we have our teleport part, right? Wrong. There is one more thing we should add to this if we want this to work. The insides of buildings are called Interiors. Each interior has a specific dimension or InteriorID it exists in. If you'd teleport yourself to these coordinates but with the wrong interiorID, you'd probably be falling in a grey world.
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


Re: [InfiniTuts - Tutorials for beginners] Tips and Tricks regarding 'logical thinking' - Making your own script - Hiddos - 29.04.2012

I doubt a lot of people will read this, but nice one.

Something I suggest is this:
Code:
1. Write up what the input will be (entering a vehicle, player typing a command, etc.)
2. Write up what the output should be if successful, and write up what the output should be when
   something isn't right.

3. Think about how you need to process the input. For a /ban command it is:
   3.1 Who needs to be banned and how does the script know this?
   3.2 What is the reason for the ban and how will the script know it?

4. Now make real whatever you wrote down/thought about at step 3.
5. Test it
   5.1 Doesn't work? Check each line and try to understand your code, then fix it.



Re: [InfiniTuts - Tutorials for beginners] Tips and Tricks regarding 'logical thinking' - Making your own script - Calgon - 29.04.2012

wtf @ blue


Re: [InfiniTuts - Tutorials for beginners] Tips and Tricks regarding 'logical thinking' - Making your own script - ReneG - 29.04.2012

A lot of this is tl:dr, but very thorough, and organized


Re: [InfiniTuts - Tutorials for beginners] Tips and Tricks regarding 'logical thinking' - Making your own script - 2KY - 29.04.2012

What I suggest is teaching beginners to use more efficient methods than OnPlayerCommandText, such as ZCMD, YCMD, and the YSI Libraries. They're created and released for our use, why not use them?


Re: [InfiniTuts - Tutorials for beginners] Tips and Tricks regarding 'logical thinking' - Making your own script - bashar0151 - 29.04.2012

nice tut


Re: [InfiniTuts - Tutorials for beginners] Tips and Tricks regarding 'logical thinking' - Making your own script - Luis- - 29.04.2012

I'm going to try the "pen and paper" idea, it seems easier.