[Tutorial] [WIP] 'Arrays / Enums / For loops' for newbies!
#1

**** WORK IN PROGRESS, will be done shortly, hit submit instead of preview.

*Before we get started let me tell you that this tutorial is aimed at new scripters, so of course if you already know what you're doing this probably wont help you at all*
Part 1: Includes
Since this is aimed at newbies, we wont be using any includes that are not included when you download the sa-mp server.

pawn Код:
#include <a_samp> // This include is pretty much needed for everything, it includes sa-mp callbacks etc
Part 2: Arrays and Enums
For this tutorial we are going to be going over a few different things, so we are going to need a few different variables.
pawn Код:
new Locker[];
^ Here we created a variable thats an array called Locker, and the [] (Square brackets) are for the array, Currently I have put no value inside, so it wouldn't work as is, but next we are going to put something inside and I will explain how that works.
pawn Код:
new Locker[MAX_PLAYERS];
^ See now for the array size we've added MAX_PLAYERS meaning 500, so basically is says "new Locker[500];"
Anyways the best way I can explain this is by using "slots" thats why I called the variable Locker, so think of each player as a slot in a locker, our locker has 500 slots and each slot can hold a unique value, so I could do
pawn Код:
Locker[0] = 5;
Locker[1] = 10;
Locker[2] = 12;
^ So above shows that Locker[0] "0" being slot 0 contains "5", so that lockers slot is equal to 5, and on the next line we make slot 1 equal 10, so every slot can hold a unique value.
pawn Код:
//Variables
//new Locker[MAX_PLAYERS]; // We comment out this line for now
enum LockerA
{
    Apples,
    Oranges,
}
new Locker[MAX_PLAYERS][LockerA];
^Now above is a enumeration, enum for short... an enum is a list basically, a list of numbers.. Apples being 1, Oranges being 2, similar to defining something, I'll show an example below.. but before that, the array we made
new Locker[MAX_PLAYERS][LockerA]; is a 2D array (2 dimensional) . How it works is pretty much the same way as our other array worked, we have MAX_PLAYERS representing slots 0-500, then for the next square brackets box, we have the enum name "LockerA" meaning it'll use the values inside. It will be explained a little further down, but first the example of how the enum works
pawn Код:
#define Apples 0
#define Oranges 1

enum LockerA
{
    Apples, //0
    Oranges, //1
}
^ See how I defined Apples being "0" and in the enum Apples is also representing "0", enums are just the better way to go, or everything would be highly confusing.
so heres an example using our enum with the 2D array
pawn Код:
new Locker[0][Apples] = 5;
new Locker[0][Oranges] = 10;

new Locker[1][Apples] = 25;
^ So how the above works is we have the Locker then we have the slot being 0, and in the next brackets we have the value, and thats Apples, or you could put "0" and it would be the same thing, but there is no need for that as it would get confusing, so basically Locker slot 0 then were using the value "Apples" from the enum that its using, and setting it to 5, and same with oranges but setting that to 10. And we can do this for any slot, and set unique values for each item (Apples, Oranges).

Part 3: PlayerInfo example
I'm going to show a little example about how you'd make a player info enum/array. IE for admin level, age, etc.
pawn Код:
#include <a_samp>
#include <zcmd>

enum PlayerInfo
{
    AdminLevel,
    Age,
    Sex,
        Name[MAX_PLAYER_NAME], //This is a string
}
new p_data[MAX_PLAYERS][PlayerInfo];

public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/getmoney", true))
    {
        if(p_data[playerid][AdminLevel] == 0) return SendClientMessage(playerid, -1, "You're not admin!");
        GivePlayerMoney(playerid, 5000);
        return 1;
    }
    return 0
}
^ See I made the enum and called it PlayerInfo and inside I added AdminLevel, Age, Sex and Name, also we made Name a string, and for it to be a string you need to add [] with a size, I used MAX_PLAYER_NAME which is 24.
anyways so I made a command called getmoney, and if the players admin level is 0 it wont allow him to use the command, as you can see I'm using the array I made "p_data" and using the slot that is equal to what ever my playerid is, so for example if I was player number 28 it'd be "p_data[28][AdminLevel]" and its checking if that is equal to 0, which it is becuase I never changed it from the initial value of 0.

Part 4: Looping
TODO

Part 5: Booleans 'Bools'
Booleans (bools) are basically true or false or 1 or 0.
pawn Код:
new bool:IsAdmin[MAX_PLAYERS];
^ Above we created a bool called "IsAdmin" its also an array, so it can be set unique for each player, aslong as we put playerid inside the brackets. so for an example I'm going to set IsAdmin to true OnPlayerSpawn if they are an RCON admin, if not its going to remain false.
pawn Код:
public OnPlayerSpawn(playerid)
{
    if(IsPlayerAdmin(playerid) == true)
    {
        IsAdmin[playerid] = true;
    }
    return 1;
}
^ So above we made it so that if the player is an RCON admin when spawned, the IsAdmin variable will equal to true, or 1.

Part 6: Tips (Read this!)
1: When using an array with MAX_PLAYERS, since you're going to be using it for the playerid you're going to want to "reset" its value either OnPlayerDisconnect or OnPlayerConnect
pawn Код:
public OnPlayerDisconnect(playerid)
{
    Locker[playerid] = 0;
    Locker[playerid][Apples] = 0;
}
2: Just because I called the array Locker, doesn't mean it must be locker.. if you looked at a RP script for example you'll see enums called example PlayerInfo, which would have a enum with like AdminLevel, level, age etc.

3: There is already a tutorial in the wiki about arrays, but I believe mine goes a bit more newb friendly.
Reply
#2

Explain booleans, and some other things, it can help

Nice tutorial, great organizing and presentation

( ^ Mat preston :S lolwtf )
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)