[Tutorial] How to code a boolean variable
#1

Hello, in this tutorial you will also see there the usage of Floats(RandomSpawns), SetPlayerPos, SetPlayerInterior , SetPlayerVirtualWorld which will not be explained. This is only explaining how the bool variable works and checks made.

This tutorial is using arrays and operators check this out

https://sampwiki.blast.hk/wiki/Scripting_Basics#Arrays
https://sampwiki.blast.hk/wiki/Control_Structures#Operators



(the basics) What is a bool?
  • A bool is a variable that can be true (1) or false (0), regarding on how you want these booleans to be like.
  • It's also a variable, we can make any variables we wan't, we name it, and we tell what it need to do.
  • Also important part: these variables aren't really meant to be booleans when we talk about SA-MP PAWN. Their usage and meaning are similar to the boolean variable on C++.
Today i will start off with teaching you how to properly code this kind of variable.
We will start with something easy, we will try to send a message that will be shown for one time when a player spawns and so basically OnPlayerSpawn callback will be called and a simple DM respawn.

Different ways of checking a specific bool *4/03/2018

PHP код:
if(CheckFirstTimeMessagel == true
This kind of check can be also written to:
PHP код:
if(CheckFirstTimeMessagel
And if "CheckFirstTimeMessagel" is false can be used in:
PHP код:
if(!CheckFirstTimeMessagel
Starting with coding it

So at the first, we obviously start defining the variable name and it's functions.

PHP код:
new bool:CheckFirstTimeMessagel
Once we finished defining our boolean variable we start now working under OnPlayerConnect callback, this is a very important thin line if we want that our boolean will work without problems, we need to make the boolean variable true to all the new incoming players on our server in order to show our stuff we want.

PHP код:
public OnPlayerConnect(playerid)
{
  
CheckFirstTimeMessagel[playerid] = true

So let's now analyze our code on this specific callback to see how and what is used, so as i stated above with the tutorial booleans can be true or false regarding on what we want, we decided that we want to show for every new player that will spawn on the server a specific message that will not be shown for a second time.
  • We made the boolean true on the connection of the player. "= true".
  • We specified the player ID that is connecting to our server "playerid".
So, after we made the boolean true on the player connection, we must start now working under OnPlayerSpawn callback, because, it's there where we want to show our masterpiece.

PHP код:
public OnPlayerSpawn(playerid)
{
    if(
CheckFirstTimeMessagel[playerid] == true// checking if the boolean is true
    
{
        
CheckFirstTimeMessagel[playerid] = false//checking false to make it get called once
        
SendClientMessage(playerid, -1"Welcome to our server, read /rules and /cmds");
    }

So, what we did on that callback.
  • Checking if the boolean is true.
  • Making the boolean false so it gets called once.
  • And sending our holy message.
So after checking false to our boolean, it won't send it back again when a player dies and suddenly will respawn and so when the same callback will get called our boolean won't be shown with it's message because it's now simply returning false.
Now we need to work on OnPlayerDisconnect callback, otherwise it won't be fun having the message not shown for other players with same ID.
We simply do the same exactly thing we did with the OnPlayerConnect callback, but instead returning it to true, we must return it to false.

PHP код:
public OnPlayerDisconnect(playerid)
{
  
CheckFirstTimeMessagel[playerid] = false

Respawn on DM locations (with random spawn) *4/03/2018

Another example of this following tutorial, how to make a player respawn when entering a certain DM zone.
This is our dm location we called it /zone.

PHP код:
new Float:zone[][] =
{
  {-
1469.6196,1617.4175,1052.5313},
  {-
1416.2900,1616.4597,1052.5313},
  {-
1373.7206,1588.0524,1052.5313},
  {-
1370.3744,1561.4594,1052.5313},
  {-
1357.4647,1618.7813,1052.5313}
}; 
PHP код:
CMD:zone(playerid)
{
      
SetPlayerVirtualWorld(playerid3);
      
SetPlayerInterior(playerid14);
      new 
zones random(sizeof(zone));
      
SetPlayerPos(playeridzone[zones][0],zone[zones ]
      [
1],zone[zones ][2],zone[zones ][3],zone[zones ][4]);
      return 
1;

As you see there are no booleans defined inside this command and so we can't make a player respawn, so let's go ahead and make our new bool variable, and don't forget that for these kind of stuffs we need to declare it as ARRAY, because every player in the server has their own value for it.
PHP код:
new bool:IsInZone[MAX_PLAYERS]; 
So once we finished with that, we add the check inside our command, so it will be like that:
PHP код:
IsInZone[playerid] = true
Now we need to work on our callback and it will be OnPlayerSpawn, which will be called when or not the player must be spawned on DM, and based on our checks he must respawn on DM zone he entered before with /zone command.

PHP код:
if(IsInZone[playerid] == 1)
{
      new 
zones random(sizeof(zone));
      
SetPlayerPos(playeridzone[zones][0],zone[zones ]
      [
1],zone[zones ][2],zone[zones ][3],zone[zones ][4]);

So as we did we placed the right check on our interested callback, and the player will now get respawned on the interested area, but it's not finished. Because we didn't worked on our 2 last callback and they are OnPlayerConnect and OnPlayerDisconnect, it's to simply avoid that a player with same ID (as told before) gets respawned there for no reasons, so let's work on it by simply adding false on both callbacks.

PHP код:
IsInZone[playerid] = false
Thanks for reading and following my first tutorial, we now ended this simple tutorial about these kind of variables. If you got anything to tell, notice me by commenting this thread. Don't send any PM's. !!
Reply


Messages In This Thread
Usage of boolean variable (e.g. respawn on DM locations) - by wallen - 03.03.2018, 17:00
Re: How to code a boolean variable - by wallen - 04.03.2018, 14:12
Re: How to code a boolean variable - by RogueDrifter - 04.03.2018, 14:13
Re: How to code a boolean variable - by wallen - 04.03.2018, 14:27
Re: How to code a boolean variable - by BulletRaja - 04.03.2018, 14:30
Re: How to code a boolean variable - by wallen - 04.03.2018, 14:44
Re: How to code a boolean variable - by RogueDrifter - 04.03.2018, 14:54
Re: How to code a boolean variable - by wallen - 04.03.2018, 15:12
Re: How to code a boolean variable - by NaS - 04.03.2018, 15:39
Re: How to code a boolean variable - by wallen - 04.03.2018, 15:51
Re: How to code a boolean variable - by ivndosos - 04.03.2018, 16:05

Forum Jump:


Users browsing this thread: 1 Guest(s)