SA-MP Forums Archive
[TUTORIAL] Simple Arrays - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: [TUTORIAL] Simple Arrays (/showthread.php?tid=86080)



[TUTORIAL] Simple Arrays - lavamike - 11.07.2009

Simple Arrays
By Lavamike

Difficulty: Easy [Minor Scripting Knowledge Required]


Introduction
Hello! My name is Mike. Today I will be showing you about how you can use some simple arrays to help you in your script. So let's get started

Main
First, what is an array?

Definition: An array is a single programming variable with multiple "compartments". Each compartment can hold a value.

This means we have one variable that stores many things inside it.

Here is an example of an array:
pawn Код:
new Array[10];
Okay so let's say we have a folder in windows on our desktop called, "Array". Inside that we have 10 folders numbered 1-10. Inside each of those 10 folders is a numeric value. When you first create the array, that numeric value is by default 0 until you set it.

So let's try a more practical example. This would do the same thing as IsPlayerConnected.
pawn Код:
#include <a_samp>
new Connected[MAX_PLAYERS];

OnPlayerConnect(playerid)
{
  Connected[playerid] = 1;
}

OnPlayerDisconnect(playerid, reason)
{
  Connected[playerid] = 0;
}
Okay so let's talk about this code. First we #include <a_samp> Always include this at the very top of your script!! This includes all of SA:MP's functions so you can use them in your code. Then we create a new array. new Connected[MAX_PLAYERS];

So now we have a folder on our desktop called, "Connected". Inside that folder there are...wait, what? What is MAX_PLAYERS??!!
When you include SA:MP's functions, there is something in it called a #define. A #define lets you define something you will commonly use. When you compile your script any define's you placed are replaced with the actual value.

So if you have something like this:
pawn Код:
#define Test 500 // Define Test to the value of 500.

new Array[Test]; // Create a new array.
In the example above, new Array[Test]; is the same as new Array[500]; It's just an easier way to reference things.

MAX_PLAYERS is defined with the value of 200. As that is the max amount of players that can ever be in your server at one time.

So we are basically doing: new Connected[200];

So now we have 200 folders inside our main Connected folder. Each of the values inside the folder are set to 0 because we haven't set them to anything yet. When a player connects it sets that their ID is connected by changing the 0 to 1.

So say a player connects with the playerid of 7. We are setting the value inside folder #7 to 1. And when they disconnect, we are setting the value back to 0.

That's just an example. Array's can be used for many things. Array's can get very advanced too.




I hope this may have helped some people, please PM Me suggestions for tutorials. If there is something in this little tutorial that you would like me to clear up, please post here.


-Mike


Re: [TUTORIAL] Simple Arrays - Zeromanster - 05.08.2009

Great job Mike, it realy helped


Re: [TUTORIAL] Simple Arrays - agusfn20 - 05.08.2009

Really good


Re: [TUTORIAL] Simple Arrays - yezizhu - 05.08.2009

Nice for beginner~


Re: [TUTORIAL] Simple Arrays - ruarai - 05.08.2009

[me=яυαяαι]fails as he was wanting to make a tutorial about this [/me]