[Tutorial] Tutorial for script beginners
#1

Hi!
Are you an beginner in PAWN scripting, and do you wanna learn more?
I'll help you for the begin! (At least, I hope that I can help you :"))

STEP 1: Includes
You need "includes" before you can make a usefull gamemode or filterscript. You can do this with '#include'
The most people put this above their script, so I strongly recommend you to do this too
For a good FS/GM (FilterScript/GameMode) you need the include 'a_samp' : here are all the basic functions.
So put this above the script

pawn Код:
#include <a_samp>
Easy!! :O
And there are more includes, the standard includes and the one that people made. You need to put the includes in the 'pawno/includes' map. So if you wanna make a log system, a join log for example, you'll need to use files! These are included in a_samp, but this is just an example! You should add this:

pawn Код:
#include <file>
You can include as much you want, just watch out that some includes don't have the same function names.

STEP 2: Starting FS/GM
Maybe you need to set some string if the game starts. You need to put this in 'public OnFilterScriptInit()' or 'public OnGameModeInit()' If you are making a gamemode you need to put 'main()' to in the script.
But this is not everything! Now it's gonna be a bit harder. You need to set... things in it :'). Use brackets to open and close the function ({ & }). Example's:

pawn Код:
main()
{
     print("STARTED");
}
The function print: It just shows some text in the samp_server.exe client. You can use printf too to format some things (printf("text: %s", string)
You need to do the same with public OnGameModeInit and public OnFilterScriptInit as main():
pawn Код:
public OnGameModeInit()
{
     print("GM STARTED");
}
pawn Код:
public OnFilterScriptInit()
{
     print("FS STARTED");
}
It is easy :') - You can do this the same way to EXIT the FS/GM (OnFilterScriptExit and OnGameModeExit)

STEP 3 - Making variables and defines
You are really making you own FS/GM now! Now it is very handy to make variables and defines.
Variables are just things you can use for usersystems or something, to check if someone is logged.
Make this things on this way: new VAR_NAME;. If you are just using it as an 'TRUE' and 'FALSE' variable you can make a bool: new bool:VAR_NAME = false;. And yeah, you can set the string of it when you are making it: new VAR_NAME = STRING;. This won't work: new VAR_NAME = Just Some Text;. Then you will get an error :S. Then use this: new VAR_NAME = "Just Some Text";. I don't know of it works, I never use it. I am using 'STRINS'. You can make strings for one player, or one vehicle (I meant: one 'effect' per player or vehicle). Just add [MAX_PLAYERS] or [MAX_VEHICLES] behing the 'VAR_NAME'. You can set the max string too, if the string may not be longer then 10 lettre's, use this: new VAR_NAME[10];
But whatever, There are DEFINES too. These are almost the same, but different too .
You can't change this things, but you can remove them (the normal variables can't). You make one with '#define'.
The most people put this under the INCLUDES, Me too, So I recommend you to do it too :'). It is very handy and usefull to make Color Defines for sending messages. The define will look like this: #define DEFINE_NAME INT
after #define and DEFINE_NAME a space. So a color will look like this: #define COLOR_WHITE 0xFFFFFFAA. Very handy . You can put text into defines too: #define OWNER_NAME "Kwarde".
There are more ways to use defines, like 'mini functions' (macro's): #define SendMessage(%0, %1, %2) SendClientMessage(%0, %1, %2);. But enough about this stuff. Let's talk about functions.

STEP 4 - Making Functions
Functions are VERRRRY usefull :'). If you are constantly using one thing it is better to make one function, it cost less diskspace. You can make a function on different ways, the PUBLIC function, the STOCK function and the "" Funtions (-.-).
Let's begin with the public function. This are callbacks. There are standard callbacks, like 'public OnPlayerConnect(playerid): Do something if a player connects'- 'public OnPlayerDisconnect(playerid, reason): Do something if a player disconnect'. You can just put these callbacks in your script (the built in callbacks!). But if you wanna make your own, you need to FORWARD it on this way: forward FUNCTIONNAME();. Ok, forwarded. And now? Simple: Use the public again : public FUNCTIONNAME(){//code}. You can't use the tag 'playerid' now, because it isn't in the callback. Do it on this way:
pawn Код:
forward FUNCTION_NAME(playerid);

public FUNCTION_NAME(playerid)
{
     //Code goes here
}
Easy, isn't it? Remember that you can much more stuff in the callbacks, like vehicleid or your own tag. I'll give an example function, a function that sends a message to ALL the RCON admins, so the non-RCON admins won't see it
In this script, I'll use "SendClientMessage". This is the function to send an client message to a player: SendClientMessage(playerid, color, message[]); - Example: SendClientMessage(playerid, 0xFFFFFFFF, "Hi there!"); : This will send a message "Hi there!" in a white color

pawn Код:
forward SendMessageToRconAdmins(color, message[]); //Yes, a '[]' behind message, because it is a text string!

public SendMessageToRconAdmins(color, message[])
{
     new string[128]; //Make a string, you already know how to do this :P
     format(string, sizeof(string), "[RCON] %s", message); //Format the variable string
     for(new i = 0; i < MAX_PLAYERS; i++) //Loop through all the players (just a '500' loop)
     {
          if(IsPlayerConnected(i)) //If the player is connected, prevent laggs
          {
                if(IsPlayerAdmin(i)) //If Is Player Admin (OMG), just check if the player is an RCON admin
                {
                      SendClientMessage(i, color, string); //SendClientMessage(all_rcon_admins, text_color, var_string);
                }
          }
      }
}
The function will work like this: SendMessageToRconAdmins(COLOR_CODE, MESSAGE);. For example: SendMessageToRconAdmins(0xFFFFFFAA, "Hi RCON admins!"); - All Rcon Admins will now see the message 'Hi RCON admins!' in a white color. But enough about this, now the STOCK and the "" functions.
These are easy XD. You don't need to forward these. Just some examples:
pawn Код:
stock SendMessage(playerid, color, text[])
{
     SendClientMessage(playerid, color, text);
}
OR
pawn Код:
SendMessage(playerid, color, text[])
{
     SendClientMessage(playerid, color, text);
}
Very easy

STEP 5 - Making commands
I think you have seen it: Commands. There are simple commands, like '/commands' or '/help'. These are easy to script. But there are harder commands, like '/pm' (easy for me, for beginners harder :').
You need the CALLBACK OnPlayerCommandText. Use it on this way: public OnPlayerCommandText(playerid, cmdtext[]). Hmm. We didn't make a command yet. Let's do that :P

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
     if(strcmp(cmdtext, "/THE_COMMAND", true) == 0)
     {
          //Here goes the code
     }
     return 1;
}
EASY! But be carefully: this is the way how to script it in a GameMode, you need to do it different in a FilterScript, if you don't all the GameMode commands won't work anymore :P

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
     if(!strcmp(cmdtext, "/THE_COMMAND", true))
     {
          //Here goes the code
     }
     return 0;
}
Well. This ain't hard. If you don't understand it, send me an PM
[EDIT]
I've got a PM from a beginner, he wanted two commands but got a error, so I am gonna explain how to add more commands: Just don't add more 'OnPlayerCommandText' callbacks, because you will get (a) error(s) - Just add more "strcmp's" :') A example:

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/THE_COMMAND", true))
    {
        //Code
    }
    if(!strcmp(cmdtext, "/ANOTHER_COMMAND", true))
    {
        //Code
    }
    return 0;
}


STEP 6 - Make your script working
This is the easiest thing. Just press F5 (if you are using PAWNO). You need to wait, and then a screen appears. Here are the errors and warnings. If you just see this:
Quote:

Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase

It's okay! Everything is GOOD! If you see warnings it is okay (mostly), but if you see errors, the script won't compile and you need to fix it. I won't help you with this now


This is the end of the tutorial. I hope it was usefull for you, and to the advanced scripters (and maybe beginners): tell me if something is missing!

Kind Regards,
Kwarde
Reply
#2

VERY nice explained

-J
Reply
#3

Good job Dutch man.
Reply
#4

Nice Tutorial!
Reply
#5

Thank You All
And er, sorry about the spelling mistakes xD
Reply
#6

looks nice im not a newbie so i didnt read it all xD
This forum requires that you wait 120 seconds between posts. Please try again in 15 seconds.
freaking boxhead forum!!
Reply
#7

Nice Tutorial, well explained
Reply
#8

Thank You, FireCat too (banned? :O)

This forum requires that you wait 120 seconds between posts. Please try again in 69 seconds.
-.-"
Reply
#9

Quote:
Originally Posted by Kwarde
(At least, I hope that I can help you :"))
Quote:
Originally Posted by Erks56
helped me alot
Yes! :P

Quote:
Originally Posted by Erks56
This Deserves STICKINESS
Quote:
Originally Posted by Erks56
Very nice tutorial
Thank You (A)

Quote:
Originally Posted by Erks56
Thank You!
You're welcome
Reply
#10

[EDIT]
Sorry, there was a internet problem, I got a error, so I thought the message wasn't placed (I refreshed the page and resended the message).
But I was already placed, so it was double post. Sorry
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)