How boolean works

Get help using Construct 2

Post » Fri Sep 18, 2015 5:29 am

i want to know that how actually boolean works
.
suppose i have two sprites Sets (6 Red, 6 Black) in the middle of the layout i.e one over second sprite
and layout have two buttons
Red button and Black button
.
Now i want if i click Red button then black sprites should go out of layout
and if i click black button then black sprite should came in and red's should go out
.
so here how i can use boolean
.
pls suggest
Image
B
67
S
40
G
48
Posts: 680
Reputation: 34,041

Post » Fri Sep 18, 2015 12:53 pm

Boolean is not an action so it can't actually "work", it's a type of variable. There are various types of variables (of data types) where the most common are - let's say - string, integer and boolean. Let me explain you all of them, that should make you understang boolean better.

String is a text type variable so you can store some text f.ex:
- "Elvis is the only one real super hyper mega king of whole music history!" - this is some long text
- "a" - this is a single character, but still a string, still a text
- "1" - this is still a text, you see a digit but within the quotes it is treated as a string/text
- "" - this is an empty string, like a text without a text but still engine treats it like a text if this is a string variable

Integers are simply a digits like 0,1,2,3,4...

Now as you can see strings and integers can have many different values. Boolean is a type of variable which can have only two different values: true or false. It is not a text "true" or "false", true or false it's just a logic assumption for the condition result. You can also think of it like the boolean may have only a value of 1 or 0. Switch is on or off.

Now you probably wondering: "Why do we need a boolean if we can use Integer and just set it to 1 or 0, or even empty or not empty string?". Well that is a good question, and yes you can do it with integers and strings as well, but here are the reasons why you should use boolean if possible:

Reason 1. To save some memory and what goes after increase performance. Depends on the programming languages, booleans may be a really tiny variables, but in other langs they are treated exactly the same like integers but just "wrapped" with the boolean functionality (something like "enum" in SQL).

Reason 2. Safety. If you have a variable which should always be a digit, then you use integer, if a text then string if it's a switch/flag then boolean. If you use the proper variable type then you protect yourself from unwanted code results (calculation etc.).

Reason 3. Because semantic code is the good code. This means that every functionality you build should be built using tools/elements which are dedicated for the particular functionality.

If you are not an experienced develper then I understan those informations might be confusing or even sensless. Well but doing so is simply a good practice so I encourage you to stick to those rules, you will understand all the good things it brings once you'll get more and more andvanced developer. Especially once you start working in a team.

I hope you get the meaning of boolean now, and as you see it doesn't "work", boolean is simply a flag which can hold one o two possible values officially accepted as true or false.
If you write a game where you can turn on or off the lamp in the dark room, then you can use boolean to remember lamp's current state - isTheLampOn (true or false) etc.

It is also worth to mention that if you think of it more closely then boolean is usually the answer for some question (is the lamp on? yes/no). Therefore a good practice is also naming boolean variables starting with "is", so the variable name itself tells you what question it's value answers to... like: isTheLampOn, isOnAir, isShooting, isMoving, isVisible etc.

ALRIGHT! I was just about to write a short explanation and here's the "short" one haha.... but that is beautiful in development that even such a small thing as boolean is worth a discussion cause of its usability! :).
Last edited by BackendFreak on Fri Sep 18, 2015 12:57 pm, edited 1 time in total.
ImageImageImage
B
27
S
16
G
82
Posts: 1,029
Reputation: 45,777

Post » Fri Sep 18, 2015 12:56 pm

Boolean is true and false and they are like integers. For example true represent 1 and false represent 0. Anyway , we use booleans to check states
ImageImageImageImageImage
B
56
S
15
G
5
Posts: 852
Reputation: 11,446

Post » Fri Sep 18, 2015 1:37 pm

boolean logic also allows thing like

x=x+(x<10)

so the (x<10) is either true (1) or false(0) so if x is less than 10 then add 1 to x, if x 10 or above then we add 0 to x ...

so this could be used for a counter up to 10

https://goo.gl/ypSYSo
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Sat Sep 19, 2015 5:00 am

thanks friend
.
its a long explanation
.
i read all the matter
.
Now i understand what is Boolean
.
.
now question is how to use it
So, Does my example belongs to Boolean
.
and what method will be for doing that



BackendFreak wrote:Boolean is not an action so it can't actually "work", it's a type of variable. There are various types of variables (of data types) where the most common are - let's say - string, integer and boolean. Let me explain you all of them, that should make you understang boolean better.

String is a text type variable so you can store some text f.ex:
- "Elvis is the only one real super hyper mega king of whole music history!" - this is some long text
- "a" - this is a single character, but still a string, still a text
- "1" - this is still a text, you see a digit but within the quotes it is treated as a string/text
- "" - this is an empty string, like a text without a text but still engine treats it like a text if this is a string variable

Integers are simply a digits like 0,1,2,3,4...

Now as you can see strings and integers can have many different values. Boolean is a type of variable which can have only two different values: true or false. It is not a text "true" or "false", true or false it's just a logic assumption for the condition result. You can also think of it like the boolean may have only a value of 1 or 0. Switch is on or off.

Now you probably wondering: "Why do we need a boolean if we can use Integer and just set it to 1 or 0, or even empty or not empty string?". Well that is a good question, and yes you can do it with integers and strings as well, but here are the reasons why you should use boolean if possible:

Reason 1. To save some memory and what goes after increase performance. Depends on the programming languages, booleans may be a really tiny variables, but in other langs they are treated exactly the same like integers but just "wrapped" with the boolean functionality (something like "enum" in SQL).

Reason 2. Safety. If you have a variable which should always be a digit, then you use integer, if a text then string if it's a switch/flag then boolean. If you use the proper variable type then you protect yourself from unwanted code results (calculation etc.).

Reason 3. Because semantic code is the good code. This means that every functionality you build should be built using tools/elements which are dedicated for the particular functionality.

If you are not an experienced develper then I understan those informations might be confusing or even sensless. Well but doing so is simply a good practice so I encourage you to stick to those rules, you will understand all the good things it brings once you'll get more and more andvanced developer. Especially once you start working in a team.

I hope you get the meaning of boolean now, and as you see it doesn't "work", boolean is simply a flag which can hold one o two possible values officially accepted as true or false.
If you write a game where you can turn on or off the lamp in the dark room, then you can use boolean to remember lamp's current state - isTheLampOn (true or false) etc.

It is also worth to mention that if you think of it more closely then boolean is usually the answer for some question (is the lamp on? yes/no). Therefore a good practice is also naming boolean variables starting with "is", so the variable name itself tells you what question it's value answers to... like: isTheLampOn, isOnAir, isShooting, isMoving, isVisible etc.

ALRIGHT! I was just about to write a short explanation and here's the "short" one haha.... but that is beautiful in development that even such a small thing as boolean is worth a discussion cause of its usability! :).
Image
B
67
S
40
G
48
Posts: 680
Reputation: 34,041

Post » Sat Sep 19, 2015 6:19 am

I'm programmer, I used construct for quick prototyping and decided to continue to work with c2 because the performance is not as bad I think. I have no idea about your structure but assuming if there are 4 types of object, redButton, RedSprite and blackButton and blackSprite then do something like

event (on object clicked blackButton)
for each object red
set position to somewhere outside layout

//and do the same for red one
event (on object clicked redButton)
for each object black
set position to somewhere outside layout

you might want to use boolean here (but I prefer number) if you only have one kind of object let's say a ColoredSprite and coloured button, they have both red and black animation, add an object isRed which is a boolean

event (on object clicked coloredButton)
for each coloredSprite
if (coloreButton.isRed != coloredSprite.isRed)
set position to somewhere outside layout
B
10
S
3
Posts: 12
Reputation: 676

Post » Sat Sep 19, 2015 8:18 am

m not getting point correctly
.
i have done according to you
it going outside screen
but how to get them back .
.
.
ex:
click Red button > black sprite go out and red sprite came in
click Black button ? red sprite go out and black sprite came in
.
how could i get this result

dheart88 wrote:I'm programmer, I used construct for quick prototyping and decided to continue to work with c2 because the performance is not as bad I think. I have no idea about your structure but assuming if there are 4 types of object, redButton, RedSprite and blackButton and blackSprite then do something like

event (on object clicked blackButton)
for each object red
set position to somewhere outside layout

//and do the same for red one
event (on object clicked redButton)
for each object black
set position to somewhere outside layout

you might want to use boolean here (but I prefer number) if you only have one kind of object let's say a ColoredSprite and coloured button, they have both red and black animation, add an object isRed which is a boolean

event (on object clicked coloredButton)
for each coloredSprite
if (coloreButton.isRed != coloredSprite.isRed)
set position to somewhere outside layout
Image
B
67
S
40
G
48
Posts: 680
Reputation: 34,041

Post » Sat Sep 19, 2015 8:50 am

here is my OTT boolean example (I will not be entering it into any good programming techniques competitions)

https://goo.gl/OzoG3U
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Sat Sep 19, 2015 8:59 am

Message: luckyrawatlucky can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
this file not opening
saying : it is in 213 or newer version i have 212.2


RamPackWobble wrote:here is my OTT boolean example (I will not be entering it into any good programming techniques competitions)

https://goo.gl/OzoG3U
Image
B
67
S
40
G
48
Posts: 680
Reputation: 34,041

Post » Sat Sep 19, 2015 9:13 am

August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 2 guests