Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pawn lang #1078

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions docs/tutorials/AdvancedStructures.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ title: "Advanced Structures"

This example shows how to find an empty slot in an array using standard coding practices.

```c
```pawn
new
gMyArray[10];

Expand All @@ -27,7 +27,7 @@ stock FindEmptySlot()

This basic example assumes an array slot is empty if its value is 0. The loop loops through all values in the array (could also be done with a constant) as long as the values are not 0. When it reaches one which is 0 the while condition will fail and the loop ends without using a break as is common practice but discouraged in situations like this. This function also returns -1 if a free slot is not found, which would need to be checked at the other end. More commonly you would use the found id straight away:

```c
```pawn
MyFunction()
{
new
Expand Down Expand Up @@ -59,19 +59,19 @@ Example:

Say you have the following array:

```c
```pawn
3, 1, 64, 2, 4, 786, 2, 9
```

If you wanted to sort the array you would end up with:

```c
```pawn
1, 2, 2, 3, 4, 9, 64, 786
```

If however you wanted to leave the data in the original order but still know the numbers in order for some reason (it's just an example), you have a problem, how are you meant to have numbers in two orders at once? This would be a good use of lists. To construct a list from this data you would need to make the array into a 2d array, where the second dimension was 2 cells big, the first dimension containing the original number, the other containing the index of the next largest number. You would also need a separate variable to hold the index of the lowest number, so your new array would look like:

```c
```pawn
start = 1
3, 1, 64, 2, 4, 786, 2, 9
4, 3, 5, 6, 7, -1, 0, 2
Expand All @@ -81,7 +81,7 @@ The next index associated with 786 is -1, this is an invalid array index and ind

The other advantage of this method of sorting the numbers is adding more numbers is a lot faster. If you wanted to add another number 3 to the sorted array you would need to first shift at least 4 numbers one slot to the right to make space, not terrible here but very slow in larger arrays. With the list version you could just append the 3 to the end of the array and modify a single value in the list;

```c
```pawn
start = 1
3, 1, 64, 2, 4, 786, 2, 9, 3
8, 3, 5, 6, 7, -1, 0, 2, 4
Expand All @@ -90,7 +90,7 @@ start = 1

None of the other numbers have moved so none of the other indexes need updating, just make the next lowest number point to the new number and make the new number point the number the next lowest used to be pointing to. Removing a value is even easier:

```c
```pawn
start = 1
3, 1, 64, X, 4, 786, 2, 9, 3
8, 6, 5, 6, 7, -1, 0, 2, 4
Expand All @@ -103,7 +103,7 @@ Here the first 2 has been removed and the number which pointed to that number (t

The lists in the examples above were just basic single lists, you can also have double lists where every value points to the next value and the last value, these tend to have a pointer to the end of the list too to go backwards (e.g. to get the numbers in descending order):

```c
```pawn
start = 1
end = 5
value: 3, 1, 64, 2, 4, 786, 2, 9, 3
Expand All @@ -113,15 +113,15 @@ last: 6, -1, 7, 1, 8, 2, 3, 4, 0

You have to be careful with these, especially when you have more than one of any value, that the last pointer points to the number who's next pointer goes straight back again, e.g this is wrong:

```c
```pawn
2, 3, 3
1, 2, -1
-1, 2, 0
```

The 2's next pointer points to the 3 in slot one, but that 3's last pointer doesn't go back to the two, both lists are in order on their own (as the two threes can be either way round) but together they are wrong, the correct version would be:

```c
```pawn
2, 3, 3
1, 2, -1
-1, 0, 2
Expand All @@ -131,7 +131,7 @@ Both of those lists start and end on the end two numbers, the back list in the w

The other type of list is the looping one where the last value points back to the first. The obvious advantage to this is that you can get to any value from any other value without knowing in advance whether the target is before or after the start point, you just need to be careful not to get into an infinite loop as there's no explicit -1 end point. These lists do still have start points. You can also do double looping lists where you have a next and last list, both of which loop round:

```c
```pawn
start = 1
end = 5
3, 1, 64, 2, 4, 786, 2, 9, 3
Expand All @@ -143,7 +143,7 @@ end = 5

Mixed lists are arrays containing multiple lists at once. An example could be an array of values, sorted by a list, with another list linking all unused slots so you know where you can add a new value. Example (X means unused (free) slot):

```c
```pawn
sortedStart = 3
unusedStart = 1
value: 34, X, X, 6, 34, 46, X, 54, 23, 25, X, 75, X, 45
Expand All @@ -153,7 +153,7 @@ free: 2, 6, 10, 12, -1

Obviously the two lists never interact so both can use the same slot for their next value:

```c
```pawn
sortedStart = 3
unusedStart = 1
value: 34, X, X, 6, 34, 46, X, 54, 23, 25, X, 75, X, 45
Expand All @@ -166,7 +166,7 @@ Before you start the code you need to decide what sort of list is best suited fo

This example shows how to write code for a list sorted numerically ascending.

```c
```pawn
#define NUMBER_OF_VALUES (10)

enum E_DATA_LIST
Expand Down Expand Up @@ -279,7 +279,7 @@ Binary trees are a very fast method of searching for data in an array by using a

**Example**

```c
```pawn
1 2 5 6 7 9 12 14 17 19 23 25 28 33 38
```

Expand Down
14 changes: 7 additions & 7 deletions docs/tutorials/MenuGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Menus look very complicated and difficult to script for the most players, althou

First we have to create a menu. The prefix before is `Menu:` this makes the variable the correct [tag](../scripting/language/Tags). There are different types for different uses such as `Float:` `bool:` `Text3D:` etc. Write the following code near the top of your script:

```c
```pawn
new Menu:teleportmenu;
```

Okay, we created the variable to store the menu. Now we have to create the menu and assign the variable we created to the menu. Type this into `OnGameModeInit`:

```c
```pawn
teleportmenu = CreateMenu("Teleportmenu", 2, 200.0, 100.0, 150.0, 150.0);
```

Expand All @@ -39,7 +39,7 @@ Now a bit of an explanation about the [CreateMenu](../scripting/functions/Create

Ok, now we've got the Menu, but we need some items, under which you can choose in the menu. You add them underneath the `CreateMenu` that we made earlier.

```c
```pawn
AddMenuItem(teleportmenu, 0, "LS");
AddMenuItem(teleportmenu, 0, "LS");
AddMenuItem(teleportmenu, 0, "SF");
Expand All @@ -66,15 +66,15 @@ The explanation for [AddMenuItem](../scripting/functions/AddMenuItem):

Okay, now that we have created a full menu with items what should happen when you choose an item? In our example we want to make a teleportmenu, so we should get teleported to the position we choose. When a player selects an item on a menu the script calls the callback [OnPlayerSelectedMenuRow](../scripting/callbacks/OnPlayerSelectedMenuRow). The best way to do it is to do it with a switch, this is like several if statements to check if a variable is worth certain values. But first we only want these effects for the menu we want so we need to create a variable that holds what menu the player is looking at, this is done with `GetPlayerMenu`:

```c
```pawn
new Menu:CurrentMenu = GetPlayerMenu(playerid);
```

Now, when somebody selects something on the menu, their menuid will be saved in `CurrentMenu`.

Now we have to check that the menu they selected on is the menu we want:

```c
```pawn
public OnPlayerSelectedMenuRow(playerid, row)
{
new Menu:CurrentMenu = GetPlayerMenu(playerid);
Expand All @@ -88,7 +88,7 @@ public OnPlayerSelectedMenuRow(playerid, row)

Now in between these brackets is where the `switch` is, this checks what item the player selected or `row` this can be done with `if` statements checking what `row` it is, but the `switch` is a much simpler way of writing it.

```c
```pawn
if(CurrentMenu == teleportmenu)
{
switch(row)
Expand Down Expand Up @@ -137,7 +137,7 @@ if(CurrentMenu == teleportmenu)

Now we need a command to show the menu. This is the easiest step. Just a comparison with `strcmp` and a `ShowMenuForPlayer`. This is done in `OnPlayerCommandText`. Or, if you have a command processor already, use that instead to call `ShowMenuForPlayer`.

```c
```pawn
if(strcmp(cmdtext, "/teleport", true) == 0)
{
ShowMenuForPlayer(teleportmenu,playerid);
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/PickupGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A short tutorial that describes how to use pickups.

The first thing to be done when creating pickups is creating a place to store their ID. This will be done in a global variable so it can be set when you create the pickup and read when you pick up a pickup, calling a callback with the ID of the pickup you picked up. For this example we will use the name "gMyPickup".

```c
```pawn
new gMyPickup;
```

Expand Down Expand Up @@ -38,7 +38,7 @@ Pickups are most commonly created when the script starts, in [OnGameModeInit](..

So here is the code to create our pickup, and store the ID in 'gMyPickup':

```c
```pawn
gMyPickup = CreatePickup(1274, 2, 2491.7900, -1668.1653, 13.3438, -1);
```

Expand All @@ -50,7 +50,7 @@ Some pickup types are designed to work automatically, so there is no need to do

When a player picks up our new pickup, we want to give them $100, to do this first we need to check that they have picked up our dollar pickup and not a different one. When we've done that, we can give them the $100:

```c
```pawn
public OnPlayerPickUpPickup(playerid, pickupid)
{
// Check that the pickup ID of the pickup they picked up is gMyPickup
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/colorfix.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ descripion: A basic script to add more player colours.

This tutorial is to be used with [GetPlayerColor](../scripting/functions/GetPlayerColor), if you do not use [SetPlayerColor](../scripting/functions/SetPlayerColor) in your script when players connect.

```c
```pawn
new PlayerColors[] = {
0xFF8C13FF,0xC715FFFF,0x20B2AAFF,0xDC143CFF,0x6495EDFF,0xf0e68cFF,0x778899FF,0xFF1493FF,0xF4A460FF,0xEE82EEFF,
0xFFD720FF,0x8b4513FF,0x4949A0FF,0x148b8bFF,0x14ff7fFF,0x556b2fFF,0x0FD9FAFF,0x10DC29FF,0x534081FF,0x0495CDFF,
Expand All @@ -24,15 +24,15 @@ First place that at the top of your script.

Next place this under the OnPlayerConnect callback:

```c
```pawn
SetPlayerColor(playerid, PlayerColors[playerid % sizeof PlayerColors]);
```

Now [GetPlayerColor](../scripting/functions/GetPlayerColor) will work!

For new versions of SA-MP you can add this array:

```c
```pawn
new PlayerRainbowColors[511] = {
/*OKStyle*/ 0x000022FF, 0x000044FF, 0x000066FF, 0x000088FF, 0x0000AAFF, 0x0000CCFF, 0x0000EEFF,
0x002200FF, 0x002222FF, 0x002244FF, 0x002266FF, 0x002288FF, 0x0022AAFF, 0x0022CCFF, 0x0022EEFF,
Expand Down
18 changes: 9 additions & 9 deletions docs/tutorials/cooldowns.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ First I'll example the _bad_ way of doing a cooldown by using `SetTimer` to upda

Say for example you have a specific action that can only be performed once every so many seconds, I see a lot of people (including Southclaws, many years ago) doing something like this:

```c
```pawn
static bool:IsPlayerAllowedToDoThing[MAX_PLAYERS];

OnPlayerInteractWithServer(playerid)
Expand Down Expand Up @@ -58,7 +58,7 @@ Now this is all well and good, it works, the player won't be able to do that thi

Take another example here, this is a stopwatch that measures how long it takes for a player to do a simple point to point race:

```c
```pawn
static
StopWatchTimerID[MAX_PLAYERS],
StopWatchTotalTime[MAX_PLAYERS];
Expand Down Expand Up @@ -96,7 +96,7 @@ If you call either of these functions at two different times, and subtract the f

### A Cooldown

```c
```pawn
static PlayerAllowedTick[MAX_PLAYERS];

OnPlayerInteractWithServer(playerid)
Expand All @@ -121,7 +121,7 @@ OnPlayerInteractWithServer(playerid)

Or, alternatively the `gettime()` version:

```c
```pawn
static PlayerAllowedSeconds[MAX_PLAYERS];

OnPlayerInteractWithServer(playerid)
Expand All @@ -148,7 +148,7 @@ There's a lot less code there, no need for a public function or a timer. If you

(I'm using SendFormatMessage in this example)

```c
```pawn
SendFormatMessage(
playerid,
-1,
Expand All @@ -163,7 +163,7 @@ That's a very basic example, it would be better to convert that MS value into a

Hopefully you can see how powerful this is to get intervals between events, let's look at another example

```c
```pawn
static Stopwatch[MAX_PLAYERS];

StartPlayerRace(playerid)
Expand All @@ -190,13 +190,13 @@ In this example, the tick count is saved to the player variable when he starts t

Now lets break the code down a bit.

```c
```pawn
new Stopwatch[MAX_PLAYERS];
```

This is a global variable, we need to use this so we can save the tick count and retrieve the value at another point in time (in other words, use it in another function, later on)

```c
```pawn
StartPlayerRace(playerid)
{
Stopwatch[playerid] = GetTickCount();
Expand All @@ -207,7 +207,7 @@ This is when the player starts the race, the tick count of now is recorded, if t

Okay, we now have that player's variable set at 60,000, now he finishes the race 1 minute 40 seconds later:

```c
```pawn
OnPlayerFinishRace(playerid)
{
new
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/perplayervariablesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The functions for setting and retrieving the player variables are:
- [GetPVarFloat](../scripting/functions/GetPVarFloat) Get the previously set float from a player variable.
- [DeletePVar](../scripting/functions/DeletePVar) Delete a player variable.

```c
```pawn
#define PLAYER_VARTYPE_NONE (0)
#define PLAYER_VARTYPE_INT (1)
#define PLAYER_VARTYPE_STRING (2)
Expand Down
Loading