Flags

Blitz3D Forums/Blitz3D Beginners Area/Flags

Ace Killjoy(Posted 2008) [#1]
This may sound like a torrent of noobishness but I have to ask:
What is a flag?
I don't know exactly what it is or how to use one.


Gabriel(Posted 2008) [#2]
It's generally used to denote a variable which either holds 0 (False) or 1 (True). In other languages this might be called a boolean, or a bool, but Blitz doesn't have that datatype, so it ends up being an integer.

If you want something more specific, it would help if you linked to wherever you're getting it from.


Charrua(Posted 2008) [#3]
I agree

you can define a variable as integer and assign it a True or False and then use that variable as the condition in an If .. then .. else, while, or any situation in that a boolean condition should be tested.

for example:

quit = false ;if you don't declare that variable, is assumed to be False = 0

while not quit

if keyhit(1) then quit = true ;flags that you wish to end the while
;your code....

wend

Juan


Ross C(Posted 2008) [#4]
I just view it as an aid to code readablity, because as Gab says, there are no boolean datatypes. So, instead of writing 1 and 0, True and False make more sense.

A handy piece of code to switch a flag on and off, just in one line, regardless of the flags value:

flag = 1 - flag


Every time you use that piece of code, the flag will toggle between 1 and 0 (True and False)


Ace Killjoy(Posted 2008) [#5]
Ok.
Thanks.


Stevie G(Posted 2008) [#6]
Sure a flag can hold a single true or false but to me, a flag is something which can hold many elements, for example :

const WIND = 1
const RAIN = 2
const SNOW = 4
const THUNDER = 8
const LIGHTNING = 16

global THE_WEATHER = WIND + RAIN + LIGHTNING


From this single variable "THE_WEATHER" you can therefore extract each element as so ....

if THE_WEATHER and WIND
   ;there is wind
endif

if THE_WEATHER and LIGHTNING
   ;there is lighning
endif

etc.... 




Charrua(Posted 2008) [#7]
it's ok
I always refuse the fact that a boolean variable were stored with 32 bits (or any number more than one).

In this way we use any single bit as a boolean variable it self.
(As many commands in blitz or in other programming languages do)


To set, reset, toggle individual bits I use:
(Using your example)

To Reset a bit:

THE_WEATHER and not(LIGHTING)

turn off that bit (if it was already off, remains in this value)

To Set a bit:

THE_WEATHER or LIGTHING

turn on that bit (if it was already on, remains in this value)

To toggle a bit:

THE_WEATHER xor LIGTHING

toggles that bit

With this manipulations THE_WEATHER should change dynamically (turning on and off some attributes) as I think you do.


To test a bit simply And

It's possibly to test more than one bit of the flag simultaneously:

if THE_WEATHER and (LIGTHING + RAIN)
....
end if

That condition result True if both LIGTHING And RAIN are on
(I use "+" instead "or" because make more sense)


Juan


boomboom(Posted 2008) [#8]
Ross, I do my switches this way, which I think is even nicer:

Flag = Not Flag


That will flip a 1 and a 0 (True and False) value over.


Ross C(Posted 2008) [#9]
Ah, cool. Looks cleaner.


Moraldi(Posted 2008) [#10]
In my opinion flags should be TRUE or FALSE. The can hold values only 1 or 0 because a statement like this:
myflag = myflag - 1

remains TRUE if myflag can be equal to 2.
Basically there are two types of flags.
Those they must be toggled when the program checks for their state and those they can be toggled under certain conditions.


IcyDefiance(Posted 2008) [#11]
See, I always thought a flag was a piece of cloth, designed and colored to represent a certain country or other group or organization. It is usually mounted on a pole, and used to show loyalty to whatever group or organization it represents.

:P :D