Boolean VS Number

Discussion and feedback on Construct 2

Post » Sun Nov 11, 2012 4:26 pm

Is there any performance or memory bonus to using a boolean over a number?

So like if my character is running, I set ISRunning to True or 1?

I only say this because I started off using numbers for my game, and I want to switch but this means I have to go through every instance that I have used a number instead of a boolean (i have many more than just running conditions).

And now I can't figure out how to so whether IsRunning is true for conditions.   "Is Instance Boolean Set" seems to not work
B
32
S
4
Posts: 405
Reputation: 3,219

Post » Sun Nov 11, 2012 5:33 pm

This is called a premature optimisation, it's not worth considering questions like this usually except in exceptional situations.

Therefore you should pick the solution which makes most semantic sense. If `IsRunning` can only be true or false, use a boolean.
Image Image
Scirra Founder
B
125
S
37
G
25
Posts: 3,945
Reputation: 44,922

Post » Sun Nov 11, 2012 6:33 pm

[quote]This is called a premature optimisation ...[/quote] I've suffered with that for years.
B
19
S
5
G
4
Posts: 208
Reputation: 3,960

Post » Sun Nov 11, 2012 6:36 pm

This is a very common type of question, and we've answered it in detail with our blog post Optimisation: don't waste your time.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Sun Nov 11, 2012 7:42 pm

I've read that blog post, but didn't find anything specifically on booleans vs variables. But if you say it doesnt matter, I believe you guys!     I wanted to use a boolean, but whenever I try to check if the condition is true or not (I'm guessing "Is Instance Boolean Set" is asking if it is true) it doesn't work.   I think i'll stick with a 0 or 1, even though I'll be having around 6 or more instance variables per character.   
B
32
S
4
Posts: 405
Reputation: 3,219

Post » Mon Nov 12, 2012 10:36 am

I often use string variables, while less efficient than numbers and booleans, they're much more readable and the difference is really negligible. Variables aren't no bottlenecks and won't be for decades to come, unless you use some weird code such as huge nested loops.
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Mon Nov 12, 2012 12:08 pm

If you have a number of independent states like "running", "jumping", etc..., use a number to represent those states, rather than a boolean IsRunning. It simplifies your event sheet by a few blocks.
B
7
Posts: 38
Reputation: 683

Post » Mon Nov 12, 2012 12:17 pm

OH HELL NO!

It makes troubleshooting a PAIN. Readable code goes much further than "optimized" code.Mipey2012-11-12 12:18:32
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Mon Nov 12, 2012 3:30 pm

I agree with Mipey, often strings are more readable and just as with the OP there is no performance gain to be had by using numbers instead.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Mon Nov 12, 2012 3:42 pm

I agree that boolean would be easier to debug, but for me, I have to use a bunch of state checks. I have one for isattacking that will not only let you know if an attack animation is playing but also the power of the attack to see who's attack will win if two players attacks both collide. (0 is not attacking, 1 or more means is attacking, a higher is attacking will win). I also have some for sidecharge, upcharge, downcharge, isblocking, isrunning, is jumping and a few more to come. This will be replicated for up to four players on screen at once.   But maybe using one string as a player state indicator would be beneficial...    either way thanks so much for all the help everyone!! :)
B
32
S
4
Posts: 405
Reputation: 3,219

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 3 guests