Boolean or Number?

Discussion and feedback on Construct 2

Post » Fri Mar 15, 2013 4:35 pm

Hello constructors,

When you using variables to define the object states, which one you prefer to use, boolean or number? It seems number can do what boolean can do and it's more flexible to extend in the future. For boolean I need to add more variable, for number I can just set A=0, A=1, A=2... its just not that intuitive and needs more comments.
B
14
S
2
G
1
Posts: 94
Reputation: 2,089

Post » Fri Mar 15, 2013 5:19 pm

Boolean is usually easier to read and makes projects easier to manage, I use them if applicable. I also prefer strings over number states: "walking", "running", "crawling" is 100x easier to read than 1,2,3- describing states.
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Fri Mar 15, 2013 7:56 pm

Good suggestion for strings. since I got a lot of boolean variables and stop using some of them after the change, I'm thinking about using string instead.
B
14
S
2
G
1
Posts: 94
Reputation: 2,089

Post » Fri Mar 15, 2013 8:38 pm

i'm happy using numbers for yes/no variables. but in some circumstances boolean is all you. if something is either one thing or another, and it's never going to change than why not use boolean.
B
14
S
3
G
4
Posts: 52
Reputation: 3,271

Post » Sat Mar 16, 2013 11:42 am

Look at it this way; when you try to feed the boolean a number or a string, it throws an error. That's one way to prevent nasty bugs.
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Sat Mar 16, 2013 12:07 pm

@vee41 - does C2 minify/obfuscate strings on export?

"Walking", "Running", "Crawling" may be 100x easier to read, but they're 100x less efficient when doing variable tests (i.e. if state="walking" rather than if state=2)

In most languages, this is exactly why you'd use enums...
B
8
S
2
G
3
Posts: 83
Reputation: 2,668

Post » Sat Mar 16, 2013 12:27 pm

[QUOTE=tanoshimi] @vee41 - does C2 minify/obfuscate strings on export?

"Walking", "Running", "Crawling" may be 100x easier to read, but they're 100x less efficient when doing variable tests (i.e. if state="walking" rather than if state=2)

In most languages, this is exactly why you'd use enums...[/QUOTE]

@tanoshimi
I highly doubt there is anything relevant performance in how you do comparisons. To me it's about usability and managing your code. :) The way Ashley has described C2 performance, it should not really matter how you handle simple comparisons as performance bottlenecks are always somewhere else.vee412013-03-16 12:27:59
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Sat Mar 16, 2013 12:45 pm

Use global and local variables for naming things.

For instance your enemy might have a variable called "state" or such, and then you create some global/local variables called:
idle = 0
run = 1
follow = 2

This way you can just refer to names rather than numbers in equations so that your math is easy to read and adjust.
B
134
S
65
G
16
Posts: 1,765
Reputation: 19,188

Post » Sat Mar 16, 2013 1:08 pm

The performance of comparing strings vs. numbers is irrelevant. Despite that the best solution is probably @alspal's! If you mark the variables const then that guarantees they cannot be changed, and also prevents them appearing in the 'Set variable' dropdowns.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,518


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 11 guests