Author Topic: [TUTORIAL] Understanding the basics of PAWN scripting  (Read 666 times)

greentarch

  • Global Moderator
  • Newbie
  • ********
  • Posts: 16
  • Reputation: 0
    • View Profile
[TUTORIAL] Understanding the basics of PAWN scripting
« on: April 21, 2013, 09:45:32 pm »
This is actually easier than hard maths to Zero :x

If you are new to scripting, then this tutorial is good for you.
SA-MP uses PAWN programming language.

In scripting, usually we do logic and maths.

Take a look at this code below :
Code: [Select]
new
    var;

var = 5;
Which is really understandable.
Create a variable called "var" and set it to '5'.

Now, let's add something to it
Code: [Select]
new
    var;

var = 5;

if (var == 5)
{
    var = 7;
}
Again, it's very understandable.
If var is 5, then set it to 7.
Easy, isn't it?

Now :
Code: [Select]
if (var != 5 )
{
    var = 5;
}
'!' means NO or "== 0"
So it means if var IS NOT 5, it set's var to 5.

Code: [Select]
if (var < 5 ) var = 5; // Var is less than 5
if (var > 5 ) var = 5; // Var is greater than 5

'>' and '<' operator also works here.

Now, let's do basic maths
Code: [Select]
new var = 0;

var = (var + 5);
// Add var by 5

var += 5;
// Add var by 5, which is the same thing as above.

var *= 5;
// Multiply var by 5;

new i = (var * 5);
// Make a variable called 'i' and set's the value as "var * 5".

Now for strings :
Code: [Select]
new
    hi[6] = "Hello";
If you ask, why 6? There's only 5 characters!
Code: [Select]
'H' 'e' 'l' 'l' 'o' '\0'

'H' = 0
'e' = 1
'l' = 2
'l' = 3
'o' = 4
'\0' = 5 (null)

Which is 6 characters, not 5 (as we count from 0 in PAWN)

And
Code: [Select]
new
    hi[1] = "Hello";
Will give an error, because Hello should have more array size, not 1.

Now, how do you compare strings at PAWN?
You don't use :
Code: [Select]
if (string1 == string2)
To compare strings.

Instead, we use strcmp to compare strings at PAWN :
Code: [Select]
if (strcmp(string1, string2, true) == 0)
strcmp returns '0' if the string matches.
"true" means it's not case sensitive.
About case-sensitive I mean :
Code: [Select]
new str1[] = "HELLO";
new str2[] = "Hello";

if (strcmp(string1, string2, true) == 0)
// String matches, because it's not case sensitive.

// But
if (strcmp(string1, string2, false) == 0)
// String will not match, because it's case sensitive.

To be continued
« Last Edit: April 21, 2013, 10:10:07 pm by greentarch »

Share on Bluesky Share on Facebook


KiNG3

  • Global Moderator
  • Newbie
  • ********
  • Posts: 10
  • Reputation: 0
    • View Profile
I like how you explained the bits of the tutorial, nice job!