You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorials/AdvancedStructures.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ title: "Advanced Structures"
8
8
9
9
This example shows how to find an empty slot in an array using standard coding practices.
10
10
11
-
```c
11
+
```pawn
12
12
new
13
13
gMyArray[10];
14
14
@@ -27,7 +27,7 @@ stock FindEmptySlot()
27
27
28
28
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:
29
29
30
-
```c
30
+
```pawn
31
31
MyFunction()
32
32
{
33
33
new
@@ -59,19 +59,19 @@ Example:
59
59
60
60
Say you have the following array:
61
61
62
-
```c
62
+
```pawn
63
63
3, 1, 64, 2, 4, 786, 2, 9
64
64
```
65
65
66
66
If you wanted to sort the array you would end up with:
67
67
68
-
```c
68
+
```pawn
69
69
1, 2, 2, 3, 4, 9, 64, 786
70
70
```
71
71
72
72
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:
73
73
74
-
```c
74
+
```pawn
75
75
start = 1
76
76
3, 1, 64, 2, 4, 786, 2, 9
77
77
4, 3, 5, 6, 7, -1, 0, 2
@@ -81,7 +81,7 @@ The next index associated with 786 is -1, this is an invalid array index and ind
81
81
82
82
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;
83
83
84
-
```c
84
+
```pawn
85
85
start = 1
86
86
3, 1, 64, 2, 4, 786, 2, 9, 3
87
87
8, 3, 5, 6, 7, -1, 0, 2, 4
@@ -90,7 +90,7 @@ start = 1
90
90
91
91
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:
92
92
93
-
```c
93
+
```pawn
94
94
start = 1
95
95
3, 1, 64, X, 4, 786, 2, 9, 3
96
96
8, 6, 5, 6, 7, -1, 0, 2, 4
@@ -103,7 +103,7 @@ Here the first 2 has been removed and the number which pointed to that number (t
103
103
104
104
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):
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:
115
115
116
-
```c
116
+
```pawn
117
117
2, 3, 3
118
118
1, 2, -1
119
119
-1, 2, 0
120
120
```
121
121
122
122
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:
123
123
124
-
```c
124
+
```pawn
125
125
2, 3, 3
126
126
1, 2, -1
127
127
-1, 0, 2
@@ -131,7 +131,7 @@ Both of those lists start and end on the end two numbers, the back list in the w
131
131
132
132
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:
133
133
134
-
```c
134
+
```pawn
135
135
start = 1
136
136
end = 5
137
137
3, 1, 64, 2, 4, 786, 2, 9, 3
@@ -143,7 +143,7 @@ end = 5
143
143
144
144
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):
Copy file name to clipboardExpand all lines: docs/tutorials/MenuGuide.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,13 +12,13 @@ Menus look very complicated and difficult to script for the most players, althou
12
12
13
13
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:
14
14
15
-
```c
15
+
```pawn
16
16
new Menu:teleportmenu;
17
17
```
18
18
19
19
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`:
@@ -39,7 +39,7 @@ Now a bit of an explanation about the [CreateMenu](../scripting/functions/Create
39
39
40
40
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.
41
41
42
-
```c
42
+
```pawn
43
43
AddMenuItem(teleportmenu, 0, "LS");
44
44
AddMenuItem(teleportmenu, 0, "LS");
45
45
AddMenuItem(teleportmenu, 0, "SF");
@@ -66,15 +66,15 @@ The explanation for [AddMenuItem](../scripting/functions/AddMenuItem):
66
66
67
67
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`:
68
68
69
-
```c
69
+
```pawn
70
70
new Menu:CurrentMenu = GetPlayerMenu(playerid);
71
71
```
72
72
73
73
Now, when somebody selects something on the menu, their menuid will be saved in `CurrentMenu`.
74
74
75
75
Now we have to check that the menu they selected on is the menu we want:
76
76
77
-
```c
77
+
```pawn
78
78
public OnPlayerSelectedMenuRow(playerid, row)
79
79
{
80
80
new Menu:CurrentMenu = GetPlayerMenu(playerid);
@@ -88,7 +88,7 @@ public OnPlayerSelectedMenuRow(playerid, row)
88
88
89
89
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.
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`.
Copy file name to clipboardExpand all lines: docs/tutorials/PickupGuide.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ A short tutorial that describes how to use pickups.
8
8
9
9
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".
10
10
11
-
```c
11
+
```pawn
12
12
new gMyPickup;
13
13
```
14
14
@@ -38,7 +38,7 @@ Pickups are most commonly created when the script starts, in [OnGameModeInit](..
38
38
39
39
So here is the code to create our pickup, and store the ID in 'gMyPickup':
@@ -50,7 +50,7 @@ Some pickup types are designed to work automatically, so there is no need to do
50
50
51
51
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:
52
52
53
-
```c
53
+
```pawn
54
54
public OnPlayerPickUpPickup(playerid, pickupid)
55
55
{
56
56
// Check that the pickup ID of the pickup they picked up is gMyPickup
Copy file name to clipboardExpand all lines: docs/tutorials/colorfix.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ descripion: A basic script to add more player colours.
5
5
6
6
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.
Copy file name to clipboardExpand all lines: docs/tutorials/cooldowns.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ First I'll example the _bad_ way of doing a cooldown by using `SetTimer` to upda
11
11
12
12
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:
@@ -148,7 +148,7 @@ There's a lot less code there, no need for a public function or a timer. If you
148
148
149
149
(I'm using SendFormatMessage in this example)
150
150
151
-
```c
151
+
```pawn
152
152
SendFormatMessage(
153
153
playerid,
154
154
-1,
@@ -163,7 +163,7 @@ That's a very basic example, it would be better to convert that MS value into a
163
163
164
164
Hopefully you can see how powerful this is to get intervals between events, let's look at another example
165
165
166
-
```c
166
+
```pawn
167
167
static Stopwatch[MAX_PLAYERS];
168
168
169
169
StartPlayerRace(playerid)
@@ -190,13 +190,13 @@ In this example, the tick count is saved to the player variable when he starts t
190
190
191
191
Now lets break the code down a bit.
192
192
193
-
```c
193
+
```pawn
194
194
new Stopwatch[MAX_PLAYERS];
195
195
```
196
196
197
197
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)
198
198
199
-
```c
199
+
```pawn
200
200
StartPlayerRace(playerid)
201
201
{
202
202
Stopwatch[playerid] = GetTickCount();
@@ -207,7 +207,7 @@ This is when the player starts the race, the tick count of now is recorded, if t
207
207
208
208
Okay, we now have that player's variable set at 60,000, now he finishes the race 1 minute 40 seconds later:
0 commit comments