Booleans
#1

I've searched on ****** for how to use a boolean in PAWN, but never could get the hang of it. If someone could give me an example (I've already got how to create one: "new bool:variableName = true;" How would I, for instance, set that bool to false, or get the current status of the boolean?
Reply
#2

A boolean is actually quite useless in Pawn, because it occupies the same space as a regular integer while only allowing for two values to be set. You set and get its value in just the same way as regular variables.
pawn Код:
new bool:myBool = true;
if(myBool == true) { /* do stuff */ }
Reply
#3

pawn Код:
new bool:Boolean;

    Boolean = true;

    if(Boolean == true)
    {
        print("Hi.");
    }
Reply
#4

pawn Код:
variableName = true/false;

if (variableName) {
    // do something if it's true
}
Booleans are just like other variables, except they only accept the values true and false.
Reply
#5

very simple, just like assigning integers to a variable.

simply:

variableName = false;
or
variableName = true;

if you want to use it inside a if(...), you could use if(variableName) which is the same as if(variableName == true) or if(!variableName) which is the same as if(variableName == false).
Reply
#6

Holy crap, 4 replies instantly. Uhh, is there a speed difference between integers and booleans by any chance? The reason I'm using a boolean is because it's a variable to check if they are logged in or not, and it only has two possible options, so I assumed a boolean would be a proper solution for this.
Reply
#7

Well, no, the only thing the "bool" tag does is tell the compiler to complain (no pun intended) if you give the variable anything but true or false, or if you compare it to anything but true or false.
"true" is essentially 1 and "false" 0.
Reply
#8

To save space you could opt for a packed array;

pawn Код:
new bool:g_isLogged[MAX_PLAYERS char];

g_isLogged{playerid} = true; // Note: Curly brackets!
Reply
#9

Quote:
Originally Posted by Slice
Посмотреть сообщение
Well, no, the only thing the "bool" tag does is tell the compiler to complain (no pun intended) if you give the variable anything but true or false, or if you compare it to anything but true or false.
"true" is essentially 1 and "false" 0.
You punster, you. And alright, that explains it. Thanks!

Quote:
Originally Posted by Vince
Посмотреть сообщение
To save space you could opt for a packed array;

pawn Код:
new bool:g_isLogged[MAX_PLAYERS char];

g_isLogged{playerid} = true; // Note: Curly brackets!
Okay, could I ask you to explain what the "char" tag does after MAX_PLAYERS, and what it does that causes me to have to use curly brackets around the playerid?
Reply
#10

See "Char-arrays" in this topic.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)