Variables

Variables

Variables are used for many things in sa-mp, such as a level system or to check if a player has /armed a bomb before /detonating? Anyway, they are pretty simple to understand, so here goes

I am going to use a /arm and /detonate example

To start, open up a new script then add this "new armed[MAX_PLAYERS];" above the main() line in your script (without the quotes).

Mine looks like this

#else

new armed[MAX_PLAYERS];

main()
{
    print("\n----------------------------------");
    print(" Blank Gamemode by your name here");
    print("----------------------------------\n");
}

Next step is to make the commands, so if you read the commands tutorial, either dcmd or strcmp (the normal one), you can make 2 commands quite easily, take a look at my example

if (strcmp("/arm", cmdtext, true, 4) == 0)
    {
        SendClientMessage(playerid,0xAFAFAFAA,"Bomb armed");

        return 1;
    }

Once you have a command that looks similar, then we can add this line "armed[playerid]=1;" This tells the script that the variable is 1 or true meaning, yes the bomb is armed, so execute the code. Now it will look lik this…

if (strcmp("/arm", cmdtext, true, 4) == 0)
    {
        SendClientMessage(playerid,0xAFAFAFAA,"Bomb armed");
        armed[playerid]=1;
        return 1;
    }

Next up is the detonate command, i'll try to explain it in the command itself, so take a look…

if (strcmp("/detonate", cmdtext, true, 9) == 0)
    {
        if(armed[playerid] == 1) // Think of it this way "if armed = true, detonate"
        {
        CreateExplosion(1958.3783, 1343.1572, 15.3746,0,10); // creates an explosion
        SendClientMessage(playerid,0xAFAFAFAA,"Bomb detonated");
        armed[playerid]=0; // "unarm" the bomb
        }
        else // Or "if armed = false, dont detonate"
        {
        SendClientMessage(playerid,0xAFAFAFAA,"/arm the bomb!"); // tells them what do to to get the command to work
        }
        return 1;
    }

So basically you set the variable to 1 meaning true or "yes" in the arm command, then in the detonate command it checks if its 1 or true or "yes", if it is then detonate the bomb, but if its "else", 0, false or "no" then dont.

Once you have dont that you are gonna want to add some lines to set the variable back to 0, so add this "armed[playerid]=0;" (without the quotes) under Onplayerspawn, and just to be safe, under onplayerconnect.

Once you have done all that then you are nearly ready to go, heres what my whole script looks like right now,

#include <a_samp>

// This is a comment
// uncomment the line below if you want to write a filterscript
//#define FILTERSCRIPT

#if defined FILTERSCRIPT

public OnFilterScriptInit()
{
    print("\n--------------------------------------");
    print(" Blank Filterscript by your name here");
    print("--------------------------------------\n");
    return 1;
}

public OnFilterScriptExit()
{
    return 1;
}

#else

new armed[MAX_PLAYERS];

main()
{
    print("\n----------------------------------");
    print(" Bomb");
    print("----------------------------------\n");
}

#endif

public OnGameModeInit()
{
    SetGameModeText("Variable testing");
    AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerRequestClass(playerid, classid)
{
    SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
    return 1;
}

public OnPlayerConnect(playerid)
{
    armed[playerid]=0;
    return 1;
}

public OnPlayerSpawn(playerid)
{
    armed[playerid]=0;
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/arm", cmdtext, true, 4) == 0)
    {
        SendClientMessage(playerid,0xAFAFAFAA,"Bomb armed");
        armed[playerid]=1;
        return 1;
    }

    if (strcmp("/detonate", cmdtext, true, 9) == 0)
    {
        if(armed[playerid] == 1) // Think of it this way "if armed = true, detonate"
        {
        CreateExplosion(1958.3783, 1343.1572, 15.3746,0,10); // creates an explosion
        SendClientMessage(playerid,0xAFAFAFAA,"Bomb detonated");
        armed[playerid]=0;
        }
        else // Or "if armed = false, dont detonate"
        {
        SendClientMessage(playerid,0xAFAFAFAA,"/arm the bomb!"); // tells them what do to to get the command to work
        }
        return 1;
    }
    return 0;
}

If your looks the same as that then test it! Fisrt off test the detonate, if you get the "/arm the bomb" message then it works, now /arm the bomb and if you get the message, /detonate it and see what happens.

Heres what mine ended up like…


Back | Home