Boolean VS Number

  • 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

  • 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.

  • his is called a premature optimisation ...I've suffered with that for years.

  • 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.

  • 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.

  • 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.

  • 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.

  • OH HELL NO!

    It makes troubleshooting a PAIN. Readable code goes much further than "optimized" code.

  • 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.

  • 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!! :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Well, I use strings for finite state machine, booleans for dual states (on/off and toggles) and numbers for mathy stuff as well as enumeration.

    I would be ecstatic with an array as a variable (for example ["PowerAttack",2,true]), but well, I guess that's still far fetched right now!

  • This thread on Stack Overflow sums it up nicely. Note that it took over a hundred million operations per second for any performance hit to appear, and even then we are talking nano-seconds of a difference.

  • Very interesting read! I think you guys have cured me of my worries!

  • That is an excellent blog post. Everyone should read it.

    Personally, I like to use boolean for where it makes sense. But overall, its up to personal preference and whatever speaks to you.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)