Basic SA-MP & Pawn Guide - Printable Version + open.mp forum (https://forum.open.mp) -- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3) --- Forum: Tutorials (https://forum.open.mp/forumdisplay.php?fid=37) --- Thread: Basic SA-MP & Pawn Guide (/showthread.php?tid=738) |
Basic SA-MP & Pawn Guide - Ygzeb - 2019-08-02 BASIC SA-MP? & PAWN GUIDE
INTRO: This guide is based on other tutorials and on my own experience. It is created specially for new scripters and people who want to learn SA-MP Pawn scripting, it explains just basic scripting and how to start. * Note: this guide is based on the one I made for?SA-MP forum?in the first months of 2015; if you read this later, some elements may have changed or updated. Based on: Nicholas tutorial Kwarde tutorial Wiki SA-MP Own knowledge. INDEX: STARTING ? ? Explaining each file (basic ones) scriptfiles plugins pawno npcmodes include gamemodes filterscripts announce.exe samp-npc.exe samp-server.exe server.cfg CONFIGURATION PAWN SCRIPTING ? ? Publics public OnGameModeInit() public OnGameModeExit() public OnPlayerRequestClass(playerid, classid) public OnPlayerConnect(playerid) public OnPlayerSpawn(playerid) public OnPlayerDeath(playerid) public OnPlayerDisconnect(playerid, reason) public OnPlayerText(playerid) public OnPlayerUpdate(playerid) Functions SetPlayerPos(playerid, X, Y, Z); TextDrawCreate(X, Y, text[]) Dialog Function (menus & others) ShowPlayerDialog(playerid, dialogid, style, caption[], info[], button1[], button2[]); Message box style (sends a message to the player) Input style (allows players to input text into the dialog) List style (menu, show the player a list of options) Password style (allows players to input text into the dialog without revealing it) CREATING AND REMOVING OBJECTS Map editor (1 & 2) Start using it Creating objects Removing objects MTA map editor Start using it Delux GTA Map Converter v2 Creating Objects Streamer installation START PRACTICING STARTING: First we must download the SA-MP Windows Server from SA-MP.com After doing this we will open the files contained in the download and after decompressing it, we will see our server files & folders... Basic files & folders: scriptfiles, plugins, pawno, npcmodes, include, gamemodes, filterscripts, announce.exe, samp-npc.exe, samp-server.exe & server.cfg Other files: Text files that include terms & a configuration guide (samp-license.txt & server-readme.txt respectively). Explaining each file (basic ones): scriptfiles: this folder contains information called inside your script or logs created by your script, usually used as a database. plugins: this folder contains additional codes that give more options in SA-MP programming, usually programmed in other languages. This guide will not give detailed information about plugins as they are more advanced. pawno: contains the basic program to start scripting in SA-MP; also has the "include" folder that contains basic includes to start our script (SA-MP basic functions). We can also add our own includes and add them to our script. npcmodes: this contains information about NPCs (script & rec); this guide will not give detailed information about NPCs as they are more advanced. include: this contains codes that can be included inside your script. gamemodes: contains basic gamemodes and we must place our gamemode if we create one. filterscripts: contains additional codes apart from your gamemode, not included in it and can be called by using rcon commands. announce.exe: this will allow us to show our server on Masterlist. samp-npc.exe: this will allow us to use NPCs. samp-server.exe: we start our server with this. server.cfg: our server configuration. CONFIGURATION: First we must open the file "server.cfg" and edit our server configuration. After opening it, you will find: Code: echo Executing Server Config... You can edit your configuration based on this: Code: echo Executing Server Config... You can also read more information about server.cfg?configuration. Configuration example (this guide is based on this configuration): Code: echo Executing Server Config... PAWN SCRIPTING: After changing our configuration we will start scripting our gamemode. In this case we will use a basic mode you can download here. Put it inside the "gamemodes" folder. After doing this open the pawno folder inside your server and open the file "pawno.exe". Press "File/Open" (or just press CTRL O) and open our basic mode, placed inside the gamemodes folder. Inside we have this: Code: #include <a_samp> This is the basic SA-MP include, it will be included in every script you create in SA-MP. This message will be shown in console when we open our server with samp-server.exe: Code: main() The blue text is the message that will be shown. You can edit the text that you want to be shown in console when your gamemode starts, just editing the blue text; for example: Code: main() * Note: Every code must have opening and closing braces to start and end the function (all codes). Example: Opening braces = Red Closing braces = Green main() { print("My first mode!"); } * Note: It is very important to press F5 after editing or creating a script! This will update the AMX file and all changes will be saved ("if there is not mistake", but in special cases may happen programming mistakes that will not figure in the compilation and will affect your server). If everything is okay you will see something like: If not there is a mistake and you need to fix it. Publics: Publics are basic functions called while your server is running. SA-MP counts with basic publics called in most gamemodes. Most basic public names describe when the public is executed. Some basic publics are called in special moments, for example while spawning; you can add a function to that moment. Example; if we want to set players health to 50 when spawning it would be something like: Code: public OnPlayerSpawn(playerid) * Note: we can not use identical publics in the same script; for example two OnPlayerSpawn publics; if we want to add an extra function we must add it inside the same public or inside a filterscript. Base on the last example, we will add a message while spawning: Code: public OnPlayerSpawn(playerid) Here are some basic publics that are mostly used by scripters: public OnGameModeInit() - Called when your mode starts. Additional information: https://sampwiki.blast.hk/wiki/OnGameModeInit Basic structure: Code: public OnGameModeInit() Example code: Code: public OnGameModeInit() AddPlayerClass is a function that adds skin options to class selection; for example if we want to add Sweet's Skin we should add this to our code: Code: AddPlayerClass(270, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0); The number "270" is the skin ID of Sweet. public OnGameModeExit() - Called when your mode ends; it is not necessary in all scripts (optional in most cases). Additional information: https://sampwiki.blast.hk/wiki/OnGameModeExit Basic structure: Code: public OnGameModeExit() Example code: Code: public OnGameModeExit() public OnPlayerRequestClass(playerid, classid) - Called when you choose your skin. Additional information: https://sampwiki.blast.hk/wiki/OnPlayerRequestClass Basic structure: Code: public OnPlayerRequestClass(playerid, classid) Example code: Code: public OnPlayerRequestClass(playerid, classid) SetPlayerFacingAngle sets the angle where the player is looking; it's structure is: Code: playerid = ID of the player More information about SetPlayerFacingAngle. public OnPlayerConnect(playerid) - Called when a player connects. Additional information: https://sampwiki.blast.hk/wiki/OnPlayerConnect Basic structure: Code: public OnPlayerConnect(playerid) Example code: Code: public OnPlayerConnect(playerid) PlayAudioStreamForPlayer is a function that plays an audio stream for a player. It's basic structure is: Code: playerid = ID of the player * Note: audio links must be in a specific format to be played; for example thislink. You can find music in many websites. For example in this website. public OnPlayerSpawn(playerid) - Used after class selection, while spawning. This are the functions that will affect you when spawning. Additional information: https://sampwiki.blast.hk/wiki/OnPlayerSpawn Basic structure: Code: public OnPlayerSpawn(playerid) Example code: Code: public OnPlayerSpawn(playerid) SendClientMessage is a function that sends a message to the player; it's structure is: Code: playerid = ID of the player More information about SendClientMessage. public OnPlayerDeath(playerid) - Called when you die. This function will affect you when you die. Additional information: https://sampwiki.blast.hk/wiki/OnPlayerDeath Basic structure: Code: public OnPlayerDeath(playerid) Example code: Code: public OnPlayerDeath(playerid) GameTextForPlayer is a function that sends a screen message to the player; it's structure is: Code: playerid = ID of the player More information about GameTextForPlayer. public OnPlayerDisconnect(playerid, reason) - Called when a player disconnects. Additional information: https://sampwiki.blast.hk/wiki/OnPlayerDisconnect Basic structure: Code: public OnPlayerDisconnect(playerid, reason) Example code: Code: public OnPlayerDisconnect(playerid, reason) public OnPlayerText(playerid) - Called when a player sends a message (chat). Additional information: https://sampwiki.blast.hk/wiki/OnPlayerText Basic structure: Code: public OnPlayerText(playerid, text[]) Example code: Code: public OnPlayerText(playerid, text[]) This function is the format assigned to SendClientMessageToAll. When it is send, it will follow that format: Code: format(String, sizeof(String), "%s [%d]: {FFFFFF}%s", Name, playerid, text);? Code: %s and %d are values assigned depending on the player. Code: format(String, sizeof(String), "%s [%d]: {FFFFFF}%s", Name, playerid, text);? Name, playerid & text are the values assigned to %s, %d and %s. public OnPlayerUpdate(playerid) - Called everytime a client/player updates the server with their status. Additional information: https://sampwiki.blast.hk/wiki/OnPlayerUpdate Basic structure: Code: public OnPlayerUpdate(playerid) Example code: Code: public OnPlayerUpdate(playerid) "if" is used to determine that if something is happening, the server will execute that function. It would mean something like: If he is using minigun, kick him! * Note: using "if" in OnPlayerUpdate may cause lag, this is just an example. Using return means that the function ends there, with "Kick(playerid);". If you have other code down that function, it will not be executed. For example: Code: public OnPlayerUpdate(playerid) Functions: Basically, most functions names (native functions) describe what the function does; you can find many of them here. Some basic functions that might be used in a basic gamemode: SetPlayerPos(playerid, X, Y, Z); - Sets a player position (X, Y & Z are the place where the player will be moved). Additional information: https://sampwiki.blast.hk/wiki/SetPlayerPos Example code: Code: SetPlayerPos(playerid, 0.0, 0.0, 3.0); // Changes player's position to a farm. You can save your current position by using the command /Save inside the game (default command by SA-MP). After using this you must go to "\GTA San Andreas User Files\SAMP" and then open the file "savedpositions.txt". There you will find something like: Code: AddPlayerClass(270, 700, 700, 5, 0, 0, 0, 0, 0, 0, 0); Then just copy the red values: Code: AddPlayerClass(270, 700, 700, 5, 0, 0, 0, 0, 0, 0, 0); * Note: obviously our values are not red, it is just for the example. Then paste those values inside your SetPlayerPos function; like this: Code: SetPlayerPos(playerid, 700, 700, 5); You can also use this system that will do it for you. TextDrawCreate(X, Y, text[]) - Creates a textdraw (X & Y are the coords in the screen and "text" is the text that will be shown). Additional information: https://sampwiki.blast.hk/wiki/TextDrawCreate Example of textdraw: Code: new Text:Textdraw0; An easy way to use this function is using a Textdraw Creator. To show a textdraw to a player you should use the function TextDrawShowForPlayer. Example of use (based on the last example): Code: public OnPlayerConnect(playerid) To hide a textdraw to a player you must use the function TextDrawHideForPlayer. Example: Code: public OnPlayerDisconnect(playerid, reason) And to destroy a textdraw you use the function TextDrawDestroy. Example: Code: public OnGameModeInit() Dialog Function (menus & others): ShowPlayerDialog(playerid, dialogid, style, caption[], info[], button1[], button2[]); - Shows the player a dialog box. Additional information: https://sampwiki.blast.hk/wiki/ShowPlayerDialog * Note: Every dialog needs a unique "dialogid"; unless you do not use that ID inside the public OnDialogResponse (filterscripts and include dialogs must also use a unique dialogid). There are 4 types of dialog: Message box style (sends a message to the player): Example code (with cancel button): Code: if(!strcmp(cmdtext, "/Help", true)) Every lines has characters; the first line has 77 characters, so we put 77 at the end. In the second line we have to count the characters in that line plus the characters in all the lines before that line. In this case is 153; so we put 153 at the end. Example code (without cancel button): Code: if(!strcmp(cmdtext, "/Help", true)) The function "strcat"? concatenates (joins together) our Menu string and our text. * Note: You can use special websites to count characters. When counting characters you just count the characters inside the ""; for example: Code: if(!strcmp(cmdtext, "/Help", true)) The \n character also counts. We can also add a function when we press "Accept" or "Cancel" inside the public OnDialogResponse. For example: Code: if(!strcmp(cmdtext, "/Help", true)) We add a message function inside the public OnDialogResponse: Code: public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) * Note: When using OnDialogResponse every dialog needs a number. In this case we used number 1: Code: ShowPlayerDialog(playerid, 1, DIALOG_STYLE_MSGBOX, "Dialog Help", Menu, "Accept", ""); Input style (allows players to input text into the dialog): Example code: Code: ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Text", "Write a text for all players!", "Accept", "Cancel"); After creating our code we must create a function to that code; so we will use the public OnDialogResponse: Example code: Code: public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) List style (menu, show the player a list of options): Example code (creating a weapon menu): Code: if(!strcmp(cmdtext, "/Weapon", true)) Code: "Weapons" = Title of the list. The \n symbol is used to separate the elements of the list. To give the weapon we use the public OnDialogResponse: Code: public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) Structure of the function GivePlayerWeapon: Code: GivePlayerWeapon(playerid, weaponid, ammo); Additional information about this function: https://sampwiki.blast.hk/wiki/GivePlayerWeapon Password style (allows players to input text into the dialog without revealing it): It has the same structure of "Input style"; mostly used in account systems (it is a bit advanced for this tutorial). If you want to create an account system you can read this tutorials: Kush account system guide (Y_INI). Lorenc_ account system guide (SQLite using rBits). CREATING AND REMOVING OBJECTS: There are many programs to create maps for your server; the most known are: Map editor 1 Map editor 2 MTA map editor Game Object ID List: https://sampwiki.blast.hk/wiki/Model_ID Map editor (1 & 2): Both have the same structure; so this is an explanation for both. Start using it: - After dowloading it, you must open the file "editor.exe" and press the button "LOAD". - It will load the game map (GTA-SA); you can move your camera by moving your mouse while clicking the right mouse button. You can move around the map with the mouse scroll or with the keys "W, A, S & D" while pressing the right mouse button. Creating objects: - Move to the place where you want to add your mapping, then press the button "Objects" inside the menu (the menu on the right of the window). - Press "Add" and search the object you want to add (ID or name); then press double click on it and press the button "Add". - After doing it press on the object shown on the right of the window and after doing it press "insert". - You can move the object by using the "Movement" panel. - After mapping you can get the code and add it to your GM by pressing "Show Code". Copy the code and paste it inside the public OnGameModeInit. Example code: Code: public OnGameModeInit() Additional information: https://sampwiki.blast.hk/wiki/CreateObject Removing objects: - Move around the map and search the object you want to delete. - Click on it and press "Supr". - After deleting the object press "Show Code". Paste the code inside the public OnPlayerConnect. Example code: Code: public OnPlayerConnect(playerid) Additional information: https://sampwiki.blast.hk/wiki/RemoveBuildingForPlayer MTA map editor: This program is easier than the other editor. You can download it here. * Note: It is able to create objects but not to delete them from the original map. Start using it: - After downloading it, open the file MTA.exe - Click on the map editor. - To move around the map use the camera and the keys "W, A, S & D". - To create objects press "F" and click on the cube object (in the bottom): - Search or select your object and place it on the map. * Note: You can move it down and up by clicking on the object and using the keys "Page Down (pg dn)" and "Page Up (pg up)". To rotate it use "Shift Scroll". To change it's angle use "CTRL Page Down/Page Up" or "CTRL Arrow keys". - To save your map press the save button: - To get the object code you can use a MTA converter: Delux GTA Map Converter v2 (2015) Delux GTA Map Converter v2: - Instead of IPL file format choose PAWN Code for SA-MP. Search your map inside your MTA folder; it is usually inside \mods\deathmatch\resources. - Copy your code. Creating Objects: After having the code of the object(s), you must add it inside the public OnGameModeInit or OnFilterScriptInit. Example code: Code: public OnGameModeInit() But SA-MP has an object limit of 1000 objects. If you want to optimize your script or you want to add more objects you should use the plugin Streamer. Streamer installation: - Dowload it from the streamer plugin thread. - After downloading it, paste the file "streamer.so" or "streamer.dll" inside the plugins folder (or just paste the plugins folder, the download one, inside your server folder). - Copy the include "streamer.inc" inside your pawno includes folder. - Edit the file "server.cfg" inside your server folder and add this line if you are using Windows: Code: plugins streamer.dll If you are using Linux add: Code: plugins streamer.so - Copy this code down all the includes (include example: #include <a_samp>), inside every script that uses streamer: Code: #include <streamer> * Note: This code is based on the Streamer plugin update from 2015 (v2.7.4). - After doing this edit the object code you have. Instead of using "CreateObject" use "CreateDynamicObject". Example: Instead of using a normal code... Code: public OnGameModeInit() Use this: Code: public OnGameModeInit() * Note: You can edit your "CreateObject" code faster by using a ".txt" file. Paste the code inside of it; press "CTRL R", search "CreateObject" and replace it with "CreateDynamicObject" (use this if your map is very big). - After doing this open the PWN file where you want to add the objects (usually inside the gamemode) and paste your code. Example code: Code: public OnGameModeInit() Additional information about Streamer plugin. START PRACTICING: Now you know this, you can practice by editing other scripts or you can try creating your own script. A good way of starting is editing other gamemodes! If there is any mistake in this guide or something should be added, just post it. * Note: this is an small guide compared to all the basic information of SA-MP that a new scripter should know; this guide will be updated according to post suggestions on this thread. Guide credits: Ygzeb (David Talledo) Special thanks to: Y_Less for a correction in some information presented in this guide and Kwarde that helped to update some links that were broken (2021) :) Addon (2021) .- Some links or images may just not work now or in the future, they may be fixed or not. However, I hope this guide will still help people. RE: Basic SA-MP & Pawn Guide - Markski - 2019-08-13 This is a great starting guide. I'm glad you didn't try to push vscode or other IDE down a begginer's throat like many others do for some reason. RE: Basic SA-MP & Pawn Guide - Tyson - 2019-08-17 Good topic, i like it! Thank you very much RE: Basic SA-MP & Pawn Guide - Chakib Chemso - 2021-05-07 (2019-08-13, 06:51 PM)Markski Wrote: This is a great starting guide. I'm glad you didn't try to push vscode or other IDE down a begginer's throat like many others do for some reason. Actually ides like vscode or even a complete vs installation makes your whole workflow easirr and fancier consider vscode combined with sampctl for example RE: Basic SA-MP & Pawn Guide - Y_Less - 2021-05-09 Very nice, but with one common misconception: () - these are brackets {} - these are braces RE: Basic SA-MP & Pawn Guide - ImOver - 2021-05-12 (2021-05-09, 04:11 PM)Y_Less Wrote: Very nice, but with one common misconception: Aren't () called parenthesis? RE: Basic SA-MP & Pawn Guide - Y_Less - 2021-05-15 No. Parentheses are anything that enclose additional text. For example: Quote:James, who just woke up, went to the shop. "who just woke up" is additional information, and is enclosed by ","s. Thus they are parentheses. Brackets, dashes, semi-colons, and probably more, can all be used in this way; to parenthesise information. I actually only learnt this the other day as well. RE: Basic SA-MP & Pawn Guide - Kwarde - 2021-05-21 (2021-05-15, 10:03 AM)Y_Less Wrote: "who just woke up" is additional information, and is enclosed by ","s.? Thus they are parentheses.? Brackets, dashes, semi-colons, and probably more, can all be used in this way; to parenthesise information.? I actually only learnt this the other day as well. This is the most useful thing I ever learned from you And to go a bit more on topic: Quote:Based on: Do you mean some kind of tutorial (or tutorials) I once wrote? I read it back some time ago and it wasn't that good at all (in fact, reading things I wrote as a kid is close to shamefull). Thanks though :-P Also you might want to consider changing the URLs in this post, replacing "http://wiki.sa-mp.com/wiki/" with "https://sampwiki.blast.hk/wiki/". That would make it even more usefull ;-) Reply - Basic SA-MP & Pawn Guide - Ygzeb - 2021-11-05 (2019-08-13, 06:51 PM)Markski Wrote: This is a great starting guide. I'm glad you didn't try to push vscode or other IDE down a begginer's throat like many others do for some reason. Thanks friend! (2019-08-17, 09:27 AM)Tyson Wrote: Good topic, i like it! Thank you very much Thanks to you, I'm happy you found it helpful ^^ (2021-05-07, 05:37 AM)Chakib Chemso Wrote:(2019-08-13, 06:51 PM)Markski Wrote: This is a great starting guide. I'm glad you didn't try to push vscode or other IDE down a begginer's throat like many others do for some reason. You are right friend; tho we didn't have them back (at least most of us)?in 2015, but yeah, if you have some experience with Pawn, VS sampctl is way better in my opinion. For starting and practicing I would still recommend using the classic. (2021-05-09, 04:11 PM)Y_Less Wrote: Very nice, but with one common misconception: Hey friend, thanks for appreciating this thread & for the additional information; I wasn't aware of that small mistake since 2015 till just now haha. We always learn something new when it's about programming. "Brackets" changed to "Braces", thanks for the input! :) (2021-05-21, 08:32 AM)Kwarde Wrote:Quote:Based on:Do you mean some kind of tutorial (or tutorials) I once wrote? I read it back some time ago and it wasn't that good at all (in fact, reading things I wrote as a kid is close to shamefull). Thanks though :-P Yeah, probably one tutorial of yours; despite the mistakes you say you made, I probably found some useful information in them so... They weren't that bad, and credit where credit's due ^^ PD: Just updated the guide with your new link (https://sampwiki.blast.hk/wiki/), thanks! |