[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
#2

Added a tutorial explaining how to make player respawn on dm zones.
Reply
#3

I mean... at least use char arrays since its all about the bool variables

EDIT: and you didn't even use the bool tag here:
PHP код:
new zone[MAX_PLAYERS]; 
You also messed the tabsize here
PHP код:
      if(zone[playerid] == 1
      { 
      new 
zone random(sizeof(zone)); 
      
SetPlayerPos(playeridzone[zone][0],zone[zone
      [
1],zone[zone][2]); 
      } 
Reply
#4

Quote:
Originally Posted by RogueDrifter
Посмотреть сообщение
I mean... at least use char arrays since its all about the bool variables

EDIT: and you didn't even use the bool tag here:
PHP код:
new zone[MAX_PLAYERS]; 
You also messed the tabsize here
PHP код:
      if(zone[playerid] == 1
      { 
      new 
zone random(sizeof(zone)); 
      
SetPlayerPos(playeridzone[zone][0],zone[zone
      [
1],zone[zone][2]); 
      } 
Oh, thanks didn't noticed
Reply
#5

Good Tutorial
Reply
#6

Quote:
Originally Posted by BulletRaja
Посмотреть сообщение
Good Tutorial
Thanks appreciated.
Reply
#7

Quote:
Originally Posted by wallen
Посмотреть сообщение
Thanks appreciated.
I still think u should fix the indentation here:
PHP код:
      if(zone[playerid] == 1)  
      {  
      new 
zone random(sizeof(zone));  
      
SetPlayerPos(playeridzone[zone][0],zone[zone]  
      [
1],zone[zone][2]);  
      } 
And put a note regarding char arrays
Reply
#8

Quote:
Originally Posted by RogueDrifter
Посмотреть сообщение
I still think u should fix the indentation here:
PHP код:
      if(zone[playerid] == 1)  
      {  
      new 
zone random(sizeof(zone));  
      
SetPlayerPos(playeridzone[zone][0],zone[zone]  
      [
1],zone[zone][2]);  
      } 
And put a note regarding char arrays
Done ! there is a redirect to sa-mp wiki about arrays and it's meaning.

And added few new ways of checking a bool. Which are really usefull for people that uses bool
Reply
#9

You tried to explain everything really well, but some things are very vague or not really correct.

For example

Quote:

Note: you can also define the boolean variable gloabally with the usage of [MAX_PLAYERS].

Making this variable an Array has nothing to do with it being global. It can be local or global and/or a single cell or an array. Global only means that it's declared outside any function regardless of it being an Array or not.

Quote:

we need always define it GLOBALLY, because there will be so much players entering this zone and we need to make sure that the bool variable will be working for everyone.

You should change this to something like "we need to declare it as ARRAY, because every player in the server has their own value for it." or similar.

Of course it also has to be global, but this part is more about it being an Array than a global variable.


Furthermore you could've explained that boolean variables aren't really booleans in PAWN. It's just a tag, they can still have any other value like -5 or 23023. Understanding this is really important in my opinion. Knowing this will let you understand why a char array is much more efficient for boolean arrays.
Reply
#10

Quote:
Originally Posted by NaS
Посмотреть сообщение
You tried to explain everything really well, but some things are very vague or not really correct.

For example



Making this variable an Array has nothing to do with it being global. It can be local or global and/or a single cell or an array. Global only means that it's declared outside any function regardless of it being an Array or not.



You should change this to something like "we need to declare it as ARRAY, because every player in the server has their own value for it." or similar.

Of course it also has to be global, but this part is more about it being an Array than a global variable.


Furthermore you could've explained that boolean variables aren't really booleans in PAWN. It's just a tag, they can still have any other value like -5 or 23023. Understanding this is really important in my opinion. Knowing this will let you understand why a char array is much more efficient for boolean arrays.
Thank you for kind words, fixed and updated thread. Thanks also for explaining, i hope people will understand better reading this kind of tutorial about booleans. i might update it often so.
Reply
#11

nice
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)